123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #import redis
- import time
- #import json
- from selenium import webdriver
- from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
- import time
- import os
- import urllib.parse
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- import dataset
- import codecs
- import random
- import requests
- import time
- import sys
- import docker
- import codecs
- import random
- import os
- import time
- from datetime import datetime
- driver=None
- click_times = 0
- headers = {
- "Authorization": "Bearer " + "WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD",
- "Content-Type": "application/x-www-form-urlencoded"
- }
- # headers = {
- # "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
- # "Content-Type": "application/x-www-form-urlencoded"
- # }
- def send_msg(kw):
- params = {"message": "處理關鍵字: "+kw}
- r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
- def empty_query(q):
- global driver
- googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
- driver.get(googleurl)
- time.sleep(3)
- def process_query(qs):
- print('processing...')
- print(qs)
- q=qs[0]
- domain=qs[2]
- cnt=qs[1]
- global driver
- global click_times
- googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
- driver.get(googleurl)
- time.sleep(3)
- if cnt > 0:
- for i in range(cnt):
- elmt=driver.find_element_by_xpath("//a[@id='pnnext']")
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(2)
- elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
- idx=1
- ranking=-1
- for elmt in elmts:
- href=elmt.get_attribute('href')
- txt=elmt.text
- if len(txt)>10:
- if domain in href:
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- break
- click_times = click_times +1
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/test?charset=utf8mb4')
- table = db['click_times']
- table.update({'id':0,'time':datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'click_time':click_times}, ['id'])
- print("click times = ",click_times)
- hour = datetime.now().strftime('%H')
- if hour== 23:
- click_times = 0
- table.update({'id':0,'time':datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'click_time':0}, ['id'])
- def re_get_webdriver():
- global driver
- result=[]
- client = docker.from_env()
- ls=client.containers.list()
- print(ls)
- for l in ls:
- print(l.name)
- if 'p4444' in l.name:
- l.restart()
- print('restarted')
- time.sleep(6)
- else:
- print('name not correct')
- # options = webdriver.EdgeOptions()
- try:
- print('trying 4444....')
- options = webdriver.ChromeOptions()
- driver = webdriver.Remote(
- command_executor='http://127.0.0.1:4444/wd/hub',desired_capabilities=options.to_capabilities())
- print('4444 done')
- driver.set_window_size(1400,1000)
- print('driver is fine')
- return
- except:
- print('driver except')
- driver=None
- return None
- def run_once(url):
- global driver
- i=random.randint(0,20)
- if i<=3 or driver is None:
- re_get_webdriver()
- if driver is None:
- print('driver is none')
- return
- try:
- process_query(url)
- except:
- print('process_query exception')
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/test?charset=utf8mb4')
- cursor=db.query('select kw,page,domain from click_and_notify where category="清原"')
- lst=[]
- for c in cursor:
- lst.append((c['kw'],c['page'],c['domain']))
- while True:
- l=random.choice(lst)
- run_once( l )
- time.sleep(0.001)
|