click_and_not.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #import redis
  2. import time
  3. #import json
  4. from selenium import webdriver
  5. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  6. import time
  7. import os
  8. import urllib.parse
  9. from selenium.webdriver.support.ui import WebDriverWait
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. import dataset
  13. import codecs
  14. import random
  15. import requests
  16. import time
  17. import sys
  18. import docker
  19. import codecs
  20. import random
  21. import os
  22. import time
  23. from datetime import datetime
  24. driver=None
  25. click_times = 0
  26. headers = {
  27. "Authorization": "Bearer " + "WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD",
  28. "Content-Type": "application/x-www-form-urlencoded"
  29. }
  30. # headers = {
  31. # "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  32. # "Content-Type": "application/x-www-form-urlencoded"
  33. # }
  34. def send_msg(kw):
  35. params = {"message": "處理關鍵字: "+kw}
  36. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  37. def empty_query(q):
  38. global driver
  39. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  40. driver.get(googleurl)
  41. time.sleep(3)
  42. def process_query(qs):
  43. print('processing...')
  44. print(qs)
  45. q=qs[0]
  46. domain=qs[2]
  47. cnt=qs[1]
  48. global driver
  49. global click_times
  50. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  51. driver.get(googleurl)
  52. time.sleep(3)
  53. if cnt > 0:
  54. for i in range(cnt):
  55. elmt=driver.find_element_by_xpath("//a[@id='pnnext']")
  56. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  57. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  58. time.sleep(2)
  59. elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
  60. idx=1
  61. ranking=-1
  62. for elmt in elmts:
  63. href=elmt.get_attribute('href')
  64. txt=elmt.text
  65. if len(txt)>10:
  66. if domain in href:
  67. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  68. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  69. break
  70. click_times = click_times +1
  71. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/test?charset=utf8mb4')
  72. table = db['click_times']
  73. table.update({'id':0,'time':datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'click_time':click_times}, ['id'])
  74. print("click times = ",click_times)
  75. hour = datetime.now().strftime('%H')
  76. if hour== 23:
  77. click_times = 0
  78. table.update({'id':0,'time':datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'click_time':0}, ['id'])
  79. def re_get_webdriver():
  80. global driver
  81. result=[]
  82. client = docker.from_env()
  83. ls=client.containers.list()
  84. print(ls)
  85. for l in ls:
  86. print(l.name)
  87. if 'p4444' in l.name:
  88. l.restart()
  89. print('restarted')
  90. time.sleep(6)
  91. else:
  92. print('name not correct')
  93. # options = webdriver.EdgeOptions()
  94. try:
  95. print('trying 4444....')
  96. options = webdriver.ChromeOptions()
  97. driver = webdriver.Remote(
  98. command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=options.to_capabilities())
  99. print('4444 done')
  100. driver.set_window_size(1400,1000)
  101. print('driver is fine')
  102. return
  103. except:
  104. print('driver except')
  105. driver=None
  106. return None
  107. def run_once(url):
  108. global driver
  109. i=random.randint(0,20)
  110. if i<=3 or driver is None:
  111. re_get_webdriver()
  112. if driver is None:
  113. print('driver is none')
  114. return
  115. try:
  116. process_query(url)
  117. except:
  118. print('process_query exception')
  119. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/test?charset=utf8mb4')
  120. cursor=db.query('select kw,page,domain from click_and_notify where category="清原"')
  121. lst=[]
  122. for c in cursor:
  123. lst.append((c['kw'],c['page'],c['domain']))
  124. while True:
  125. l=random.choice(lst)
  126. run_once( l )
  127. time.sleep(0.001)