redis_clickbot.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 codecs
  13. import random
  14. from bs4 import BeautifulSoup
  15. import requests
  16. import time
  17. import rpyc
  18. import sys
  19. import docker
  20. driver=None
  21. headers = {
  22. "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  23. "Content-Type": "application/x-www-form-urlencoded"
  24. }
  25. def send_msg(kw):
  26. params = {"message": "處理關鍵字: "+kw}
  27. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  28. def empty_query(q):
  29. global driver
  30. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  31. driver.get(googleurl)
  32. time.sleep(3)
  33. def process_query(qs):
  34. q=qs[0]
  35. domain=qs[2]
  36. cnt=qs[1]
  37. global driver
  38. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  39. driver.get(googleurl)
  40. time.sleep(3)
  41. if cnt > 0:
  42. for i in range(cnt):
  43. elmt=driver.find_element_by_xpath("//a[@id='pnnext']")
  44. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  45. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  46. time.sleep(3)
  47. elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
  48. idx=1
  49. ranking=-1
  50. for elmt in elmts:
  51. href=elmt.get_attribute('href')
  52. txt=elmt.text
  53. if len(txt)>10:
  54. # if 'hhh.com.tw' in href:
  55. # if 'hhh.com.tw' in href:
  56. # if 'ai.choozmo.com' in href:
  57. if domain in href:
  58. # if 'searchome.net' in href:
  59. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  60. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  61. break
  62. def run_once(q):
  63. global driver
  64. result=[]
  65. client = docker.from_env()
  66. ls=client.containers.list()
  67. print(ls)
  68. ls[0].restart()
  69. time.sleep(10)
  70. options = webdriver.ChromeOptions()
  71. driver = webdriver.Remote(
  72. command_executor='http://127.0.0.1:4444/wd/hub',
  73. desired_capabilities=options.to_capabilities())
  74. driver.set_window_size(1400,1000)
  75. print(q)
  76. print(q[0])
  77. process_query(q)
  78. send_msg(q[0])
  79. # empty_query(q)
  80. intsleep=random.randint(5,12)
  81. time.sleep(intsleep)
  82. r = redis.Redis(host='db.ptt.cx', port=6379, db=1,password='choozmo9')
  83. #r = redis.Redis(host='db.ptt.cx', port=6379, db=1)
  84. p = r.pubsub(ignore_subscribe_messages=True)
  85. p.subscribe('q_clickbot')
  86. while True:
  87. message = p.get_message()
  88. if message:
  89. jsobj=json.loads(message['data'].decode('utf-8'))
  90. print(jsobj)
  91. run_once(jsobj)
  92. time.sleep(0.001) # be nice to the system :)