fast_1999.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #import redis
  2. import time
  3. import traceback
  4. #import json
  5. from selenium import webdriver
  6. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  7. import time
  8. import os
  9. import urllib.parse
  10. from selenium.webdriver.support.ui import WebDriverWait
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support import expected_conditions as EC
  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. import dataset
  24. driver=None
  25. headers = {
  26. "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
  27. "Content-Type": "application/x-www-form-urlencoded"
  28. }
  29. def send_msg(kw):
  30. params = {"message": "處理關鍵字: "+kw}
  31. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
  32. def empty_query(q):
  33. global driver
  34. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  35. driver.get(googleurl)
  36. time.sleep(3)
  37. def process_query(qs):
  38. q=qs[0]
  39. domain=qs[2]
  40. cnt=qs[1]
  41. global driver
  42. googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
  43. driver.get(googleurl)
  44. time.sleep(3)
  45. if cnt > 0:
  46. for i in range(cnt):
  47. elmt=driver.find_element_by_xpath("//a[@id='pnnext']")
  48. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  49. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  50. time.sleep(3)
  51. elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
  52. idx=1
  53. ranking=-1
  54. for elmt in elmts:
  55. href=elmt.get_attribute('href')
  56. txt=elmt.text
  57. if len(txt)>10:
  58. # if 'hhh.com.tw' in href:
  59. # if 'hhh.com.tw' in href:
  60. # if 'ai.choozmo.com' in href:
  61. if domain in href:
  62. # if 'searchome.net' in href:
  63. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  64. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  65. break
  66. def re_get_webdriver():
  67. global driver
  68. global portnum
  69. result=[]
  70. # client = docker.from_env()
  71. # ls=client.containers.list()
  72. # print(ls)
  73. # for l in ls:
  74. # if 'p17777' in l.name:
  75. # ls[0].restart()
  76. # time.sleep(4)
  77. if driver is not None:
  78. print('closing....')
  79. # driver.quit()
  80. os.system('docker container restart p'+portnum)
  81. time.sleep(11)
  82. # options = webdriver.EdgeOptions()
  83. try:
  84. driver = webdriver.Remote(
  85. command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub')
  86. driver.set_window_size(1400,1000)
  87. return
  88. except:
  89. import traceback
  90. traceback.print_exc()
  91. driver=None
  92. return None
  93. driver=None
  94. def run_once(url):
  95. global driver
  96. i=random.randint(0,25)
  97. if i==0 or driver is None:
  98. # if True:
  99. re_get_webdriver()
  100. if driver is None:
  101. return
  102. try:
  103. driver.execute_script('window.open("'+url+'","_blank");')
  104. driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
  105. time.sleep(0.5)
  106. except:
  107. print('exception')
  108. #client = docker.from_env()
  109. #ls=client.containers.list()
  110. #print(ls)
  111. #for l in ls:
  112. # if 'p18888' in l.name:
  113. # ls[0].restart()
  114. #time.sleep(4)
  115. portnum=sys.argv[1]
  116. os.system('docker restart p'+str(portnum))
  117. time.sleep(4)
  118. lst=[]
  119. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  120. cursor=db.query('SELECT * FROM columnids order by rand()')
  121. for c in cursor:
  122. lst.append('https://www.hhh.com.tw/columns/detail/'+str(c['cid'])+'/')
  123. #lst=['https://www.hhh.com.tw/columns/detail/3427/index.php']
  124. #for i in range(20):
  125. while True:
  126. l=random.choice(lst)
  127. print(l)
  128. # driver.get(l)
  129. run_once(l)
  130. # time.sleep(2)