selwire_click.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import time
  2. import traceback
  3. import dataset
  4. from selenium import webdriver
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. from celery import Celery
  10. #import redis
  11. from celery.schedules import crontab
  12. import celery
  13. from celery.signals import celeryd_init
  14. from celery import signals
  15. import random
  16. from click import Option
  17. from selenium import webdriver
  18. import time
  19. import sys
  20. import os
  21. import zipfile
  22. from seleniumwire import webdriver
  23. import requests
  24. from ga4mp import GtagMP
  25. from ga4mp import FirebaseMP
  26. from ga4mp.store import DictStore
  27. import os
  28. #from selenium import webdriver
  29. PROXY_PORT = 8080 # port
  30. PROXY_USER = 'choozmo' # username
  31. PROXY_PASS = 'Chuz83102968' # password
  32. ups=str(os.urandom(5).hex()).upper()[0:5]
  33. print(ups)
  34. gtag_tracker = FirebaseMP(api_secret='HoQdcKyKS9SQsjIyphWKQA', firebase_app_id='1:589953727312:android:6923bc306ae616a1da8a55', app_instance_id="8F1C7C2C9A004DECAF27DB6AEFF"+ups)
  35. purchase_event = gtag_tracker.create_new_event(name="seo_click")
  36. custname=None
  37. param=sys.argv[1]
  38. rnd=True
  39. if len(sys.argv)>=3:
  40. rnd=False
  41. r = requests.get('http://db.ptt.cx:3001/api/push/'+param+'?status=up&msg=OK&ping=')
  42. #U1cGPOJAQD
  43. #http://db.ptt.cx:3001/api/push/hzmMjOaHpl?status=up&msg=OK&ping=
  44. def selenium_jared_click():
  45. global custname
  46. global rnd
  47. r=random.randint(0,5)
  48. options = {}
  49. if r==0:
  50. options['proxy']={'http': 'http://choozmo:Chuz83102968@172.105.205.52:8080'}
  51. if r==1:
  52. options['proxy']={'http': 'http://choozmo:Chuz83102968@139.144.120.184:8080'}
  53. if r==2:
  54. options['proxy']={'http': 'http://choozmo:Chuz83102968@172.104.92.245:8080'}
  55. if r==3:
  56. options['proxy']={'http': 'http://choozmo:Chuz83102968@172.104.67.159:8080'}
  57. if r==4:
  58. options['proxy']={'http': 'http://choozmo:Chuz83102968@139.162.71.115:8080'}
  59. print(options)
  60. # options = {
  61. # 'proxy': {
  62. # 'http': 'http://choozmo:Chuz83102968@172.105.205.52:8080'
  63. # 'http': 'http://choozmo:Chuz83102968@139.144.120.184:8080'
  64. # 'http': 'http://choozmo:Chuz83102968@172.104.92.245:8080'
  65. # 'http': 'http://choozmo:Chuz83102968@172.104.67.159:8080'
  66. # }
  67. # }
  68. c_options = webdriver.ChromeOptions()
  69. c_options.add_argument("--no-sandbox")
  70. c_options.add_argument("--headless")
  71. c_options.add_argument("--incognito")
  72. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  73. mobile_emulation = {
  74. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  75. "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
  76. try:
  77. if rnd:
  78. driver = webdriver.Chrome(seleniumwire_options=options,options=c_options)
  79. else:
  80. driver = webdriver.Chrome(options=c_options)
  81. except:
  82. traceback.print_exc()
  83. #kw='真理大學國際生'
  84. #domain='cia.au.edu.tw'
  85. if custname is None:
  86. custname='真理'
  87. cursor=db.query("SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs where cust='"+custname+"' order by random() limit 1")
  88. kw=None
  89. domain=None
  90. for c in cursor:
  91. cust=c['cust']
  92. kw=c['kw']
  93. plan=c['plan']
  94. prefix=c['prefix']
  95. # domain=eval(c['domain'])[0]
  96. domains=eval(c['domain'])
  97. positive=eval(c['positive'])
  98. break
  99. #kw='真理大學教堂'
  100. #domain='udn.com'
  101. print(kw)
  102. print(domain)
  103. #kw='真理大學校友'
  104. #kw='真理大學國際生'
  105. #kw='真理大學張聰聯'
  106. #domain='au.edu.tw'
  107. #domain='pronews.tw'
  108. driver.get('https://www.google.com?num=100')
  109. time.sleep(3)
  110. print(driver.current_url)
  111. elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
  112. time.sleep(1)
  113. elmt.send_keys(kw)
  114. elmt.send_keys(Keys.ENTER)
  115. time.sleep(4)
  116. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  117. numresults=len(elmts)
  118. print('搜尋結果數量',numresults)
  119. if numresults<=0:
  120. print(elmts)
  121. table=db['seo_jobs_ranking_jared']
  122. cnt=1
  123. for elmt in elmts:
  124. href=elmt.get_attribute('href')
  125. txt=elmt.text
  126. # print(txt)
  127. # print(href)
  128. # if domain in href:
  129. for domain in domains:
  130. if domain in href:
  131. purchase_event.set_event_param(name="query", value=kw)
  132. event_list = [purchase_event]
  133. gtag_tracker.send(events=event_list)
  134. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  135. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  136. print(domain)
  137. print(href)
  138. print('ranking'+str(cnt))
  139. table.insert({'ranking':cnt,'kw':kw,'results':numresults,'url':href,'title':txt})
  140. driver.quit()
  141. return txt
  142. break
  143. cnt+=1
  144. table.insert({'ranking':-1,'kw':kw,'results':numresults,'url':None,'title':'未收錄'})
  145. return '{empty}'
  146. # time.sleep(5)
  147. selenium_jared_click()