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 codecs
import random
from bs4 import BeautifulSoup
import requests
import time
import rpyc
import sys
import docker
import dataset
import re


db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')


headers = {
        "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
        "Content-Type": "application/x-www-form-urlencoded"
}


def process_query(q):
    global driver
    googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
    driver.get(googleurl)
    time.sleep(3)
    try:
        elmt=driver.find_element_by_xpath("//div[@class='HlosJb bOkdDe']")
        print(elmt.text)
        return 0
    except:
        print('not found')
    try:
        elmt=driver.find_element_by_xpath("//div[@id='result-stats']")
        print(elmt.text)
        m=re.search('找到约 ([\d,]+) 条结果',elmt.text)
        if m:
            txt=m.group(1).replace(',','')
            return int(txt)
    except:
        print('not found')
        return 0
#    idx=1

client = docker.from_env()
ls=client.containers.list()
print(ls)
ls[0].restart()
time.sleep(12)

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server=socks5://172.104.67.159:8180")


driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444/wd/hub',
#command_executor='http://192.53.174.202:4444/wd/hub',
#command_executor='http://172.104.93.163:4444/wd/hub',
 
#command_executor='http://dev2.choozmo.com:14444/wd/hub',
desired_capabilities=options.to_capabilities())
#desired_capabilities=DesiredCapabilities.CHROME)
driver.set_window_size(1400,1000)

name=None
designers=[]
#cursor=db.query('select name,vip from customer_list order by updated asc limit 3')
cursor=db.query('select name,vip from customer_list where name not in (select designer from designer_social)')

for c in cursor:
    name=c['name']
    designers.append(name)
    print(name)
#    break

table=db['designer_social']

for d in designers:
    m01=process_query('"'+d+'" site:mobile01.com')
    ptt=process_query('"'+d+'" site:ptt.cc')
    pix=process_query('"'+d+'" site:pixnet.net')
    table.insert({'designer':d,'m01':m01,'ptt':ptt,'pix':pix})

db.commit()

#num=process_query('"'+name+'"')
#process_query('彙禾設計')

time.sleep(9999)
#https://whatismyipaddress.com/ip/61.230.75.30
#driver.get('https://whatismyipaddress.com/')
#fw=codecs.open('c:/tmp/gg.html','w','utf-8')
#fw.write(driver.page_source)
#fw.close()
#import sys
#sys.exit()


#### qlist=get_list()
