import traceback
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
import os
import datetime
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
from selenium.webdriver.common.keys import Keys
import codecs
import random
import requests
import time
import rpyc
import sys
import codecs
import sys
import time
import dataset
import os


fr=codecs.open('c:/tmp/ytfiles.csv','r','utf-8')
lines=fr.readlines()
fr.close()
fnamedict={}
for l in lines:
    elmts=l.split(',')
    if len(elmts[2])>10:
        fnamedict[elmts[0].lower().replace('\r','').replace('\n','')]=elmts[2].replace('\r','').replace('\n','').replace("http://youtu.be/",'').replace("https://youtu.be/",'')

prefix='P:\\【完整版】幸福空間節目\\#31'
fname='31video_1101.mp4'
yt=fnamedict[fname.lower()]
print(yt)

#db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@127.0.0.1:13317/xoops?charset=utf8mb4')

cursor=db.query('select * from youtube_list where youtube_video_id="'+yt+'"')
title=None
desc=None
for c in cursor:
    print(c)
    title=c['title']
    desc=c['description']
    break





def scrolling(driver,pgnum):
    ub = driver.find_element_by_css_selector('body')
    for i in range(pgnum):
        ub.send_keys(Keys.PAGE_DOWN)
        if pgnum>1:
            time.sleep(0.3)


def restart_browser():
#    os.system('docker container restart p4444')
#    time.sleep(10)

    options = webdriver.ChromeOptions()
#    options.add_argument("--proxy-server=socks5://130.61.93.198:1080")
    options.add_argument("start-maximized")
    options.add_argument('user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data')
    options.add_argument('--profile-directory=Profile 7')

    driver=webdriver.Chrome(desired_capabilities=options.to_capabilities())
    driver.set_window_size(1400,1000)
    return driver


driver=restart_browser()
url='https://www.youtube.com/upload'

driver.get(url)
time.sleep(5)
elmt=driver.find_element_by_xpath("//input[@type='file']")
elmt.send_keys(prefix+"\\"+fname)
time.sleep(6)

elmts=driver.find_elements_by_xpath("//div[@id='textbox']")
elmt=elmts[0]
elmt.click()
for i in range(20):
    elmt.send_keys(Keys.BACKSPACE)
    time.sleep(0.1)
elmt.send_keys(title)
time.sleep(1)

elmt=elmts[1]
elmt.click()
for i in range(15):
    elmt.send_keys(Keys.ARROW_UP)
    time.sleep(0.1)
elmt.send_keys(Keys.RETURN)

elmt.send_keys(desc)
time.sleep(1)

elmt=driver.find_element_by_xpath("//*[@id='next-button']")
elmt.click()
time.sleep(3)

elmt=driver.find_element_by_xpath("//*[@id='next-button']")
elmt.click()
time.sleep(3)
elmt=driver.find_element_by_xpath("//*[@id='next-button']")
elmt.click()
time.sleep(3)
elmt=driver.find_element_by_xpath("//*[@id='done-button']")
elmt.click()
time.sleep(3)

while True:
    try:
        elmt=driver.find_element_by_xpath("//span[contains(text(),'檢查完畢,未發現任何問題)]")
        break
    except:
        print('not yet')
        time.sleep(5)
#檢查完畢,未發現任何問題。

time.sleep(9999)