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

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):
    q=qs[0]
    domain=qs[2]
    cnt=qs[1]
    global driver
    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(3)



    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 'hhh.com.tw' in href:
#            if 'hhh.com.tw' in href:
#            if 'ai.choozmo.com' in href:
            if domain in href:
#            if 'searchome.net' in href:
                webdriver.ActionChains(driver).move_to_element(elmt).perform()
                webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
                break
result=[]


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

time.sleep(10)
options = webdriver.ChromeOptions()

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)

#qlist=['大進設計 583','大進設計 免費專線','大進設計 林函青','大進設計 hhh','大進設計 林函青設計師 hhh','大進空間設計 hhh','林函青 hhh']
#qlist=['AI Spokesgirl','choozmo ai spokesgirl','choozmo ai 主播','choozmo 國際代言人','choozmo 國際主播','choozmo ai短影片','choozmo 短影片','AI虛擬主播 技術 choozmo','choozmo ai短影片','choozmo 虛擬人','choozmo 虛擬播報員','choozmo ai video','choozmo virtual presenter','choozmo virtual avatar','choozmo ai spokesperson']
#qlist+=['ai seowriter choozmo','gpt3 choozmo','蝦皮 seo site:ai.choozmo.com','vtuber choozmo','seo site:ai.choozmo.com','台灣第一位ai主播','ai主播 2021..2021','AI主播韓小夏 choozmo','ai spokesgirl','ai主播 韓小夏 choozmo','主播 韓小夏 choozmo','ai spokesgirl 集仕多','ai 合成 主播 choozmo','台灣AI主播 choozmo','台灣 ai主播  choozmo']
#qlist+=['ai seowriter choozmo','gpt3 choozmo','蝦皮 seo site:ai.choozmo.com','vtuber choozmo','seo site:ai.choozmo.com','youtube seo choozmo','video marketing choozmo','deepmind choozmo']
#qlist=['ppt to mp4 choozmo','slides to MP4 choozmo','Convert Your Slide Into Video choozmo','ppt video choozmo','PowerPoint Into a Video choozmo']
#qlist=['ai seowriter choozmo','gpt3 choozmo','youtube seo choozmo','video marketing choozmo','人工智慧 choozmo','text to video choozmo','contentfly choozmo','Uberduck choozmo','文本 choozmo','slide video choozmo','unicorn choozmo']
#qlist+=[('local seo choozmo',0),('video production choozmo',0),('Google Slides choozmo',0),('video production choozmo',0),('絆愛 vtuber choozmo',0),('vtuber 鯊魚 choozmo',0),('ai unicorn choozmo',0),('韓國 ai 主播 choozmo',0)]
#qlist+=[('seo 推薦 choozmo',0),('choozmo seo 推薦',0),('台灣 vtuber choozmo',0),('vtuber choozmo',0),('台灣 choozmo',0),('Anthony Bourdain choozmo',0),('角巻 choozmo',0),('竹北 seo choozmo',0)]

qlist=[('ai 主播',2,'choozmo.com'),('ai主播',2,'choozmo.com'),('台灣 vtuber choozmo',0,'choozmo.com'),('vtuber choozmo',0,'choozmo.com'),('vtuber 是什麼 choozmo',0,'choozmo.com'),('vtuber site:choozmo.com',0,'choozmo.com')]
qlist+=[('seo 推薦 choozmo',0,'choozmo.com'),('蝦皮 seo site:ai.choozmo.com',0,'choozmo.com'),('ppt to mp4 choozmo',0,'choozmo.com')]
qlist+=[('預售屋客變教學',0,'hhh.com.tw'),('預售屋客變',0,'hhh.com.tw'),('預售屋客變流程',0,'hhh.com.tw'),('預售屋客變省錢',0,'hhh.com.tw'),('所有的地方都能進行客變嗎',0,'hhh.com.tw'),('什麼是客變',0,'hhh.com.tw')]

#qlist=[('居家風水',0),('風水',2)]

#qlist+=[('DHIA 黃靜文',0,'hhh.com.tw'),('藏風空間設計',1,'hhh.com.tw'),('天花板設計',1,'hhh.com.tw'),('陽台佈置',0,'hhh.com.tw'),('設計師',0,'hhh.com.tw'),('玄關設計',0,'hhh.com.tw'),('廚房設計',0,'hhh.com.tw'),('風水',2,'hhh.com.tw'),('住宅風水',1,'hhh.com.tw'),('居家風水',0,'hhh.com.tw'),('居家 風水',0,'hhh.com.tw'),('客廳連廚房',0,'hhh.com.tw'),('前陽台玄關',1,'hhh.com.tw')]


#qlist=[('大欣室內',1,'hhh.com.tw'),('雲頂室內',1,'hhh.com.tw'),('沐熙空間',1,'hhh.com.tw'),('丸美製作',1,'hhh.com.tw'),('藏風空間設計',1,'hhh.com.tw'),('浤淇室內裝修',1,'hhh.com.tw'),('澄羿空間',1,'hhh.com.tw')]

#qlist=[('預售屋客變',0,'hhh.com.tw'),('DHIA 黃靜文',0,'hhh.com.tw'),('DHIA hhh',0,'hhh.com.tw'),('DHIA 設計',0,'hhh.com.tw'),('DHIA 室內設計',0,'hhh.com.tw'),('DHIA 設計師',0,'hhh.com.tw')]

##AR/VR Focused On-demand Micro Exhibition

#while True:
for i in range(80):
    q=random.choice(qlist)
    print(q)
#    prob=random.randint(0,4)
    prob=random.randint(0,2)

#    if prob <=1:
    if True:

    #    q=qlist[0]
        print(q[0])
        process_query(q)
        send_msg(q[0])
    else:
        empty_query(q)

    intsleep=random.randint(5,12)

    time.sleep(intsleep)

