sel_yt.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import traceback
  2. from selenium import webdriver
  3. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  4. import time
  5. import os
  6. import datetime
  7. import urllib.parse
  8. from selenium.webdriver.support.ui import WebDriverWait
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.support import expected_conditions as EC
  11. from selenium.webdriver.common.keys import Keys
  12. import codecs
  13. import random
  14. import requests
  15. import time
  16. import rpyc
  17. import sys
  18. import codecs
  19. import sys
  20. import time
  21. import dataset
  22. import os
  23. fr=codecs.open('c:/tmp/ytfiles.csv','r','utf-8')
  24. lines=fr.readlines()
  25. fr.close()
  26. fnamedict={}
  27. for l in lines:
  28. elmts=l.split(',')
  29. if len(elmts[2])>10:
  30. fnamedict[elmts[0].lower().replace('\r','').replace('\n','')]=elmts[2].replace('\r','').replace('\n','').replace("http://youtu.be/",'').replace("https://youtu.be/",'')
  31. prefix='P:\\【完整版】幸福空間節目\\#31'
  32. fname='31video_1101.mp4'
  33. yt=fnamedict[fname.lower()]
  34. print(yt)
  35. #db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57.cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  36. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@127.0.0.1:13317/xoops?charset=utf8mb4')
  37. cursor=db.query('select * from youtube_list where youtube_video_id="'+yt+'"')
  38. title=None
  39. desc=None
  40. for c in cursor:
  41. print(c)
  42. title=c['title']
  43. desc=c['description']
  44. break
  45. def scrolling(driver,pgnum):
  46. ub = driver.find_element_by_css_selector('body')
  47. for i in range(pgnum):
  48. ub.send_keys(Keys.PAGE_DOWN)
  49. if pgnum>1:
  50. time.sleep(0.3)
  51. def restart_browser():
  52. # os.system('docker container restart p4444')
  53. # time.sleep(10)
  54. options = webdriver.ChromeOptions()
  55. # options.add_argument("--proxy-server=socks5://130.61.93.198:1080")
  56. options.add_argument("start-maximized")
  57. options.add_argument('user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data')
  58. options.add_argument('--profile-directory=Profile 7')
  59. driver=webdriver.Chrome(desired_capabilities=options.to_capabilities())
  60. driver.set_window_size(1400,1000)
  61. return driver
  62. driver=restart_browser()
  63. url='https://www.youtube.com/upload'
  64. driver.get(url)
  65. time.sleep(5)
  66. elmt=driver.find_element_by_xpath("//input[@type='file']")
  67. elmt.send_keys(prefix+"\\"+fname)
  68. time.sleep(6)
  69. elmts=driver.find_elements_by_xpath("//div[@id='textbox']")
  70. elmt=elmts[0]
  71. elmt.click()
  72. for i in range(20):
  73. elmt.send_keys(Keys.BACKSPACE)
  74. time.sleep(0.1)
  75. elmt.send_keys(title)
  76. time.sleep(1)
  77. elmt=elmts[1]
  78. elmt.click()
  79. for i in range(15):
  80. elmt.send_keys(Keys.ARROW_UP)
  81. time.sleep(0.1)
  82. elmt.send_keys(Keys.RETURN)
  83. elmt.send_keys(desc)
  84. time.sleep(1)
  85. elmt=driver.find_element_by_xpath("//*[@id='next-button']")
  86. elmt.click()
  87. time.sleep(3)
  88. elmt=driver.find_element_by_xpath("//*[@id='next-button']")
  89. elmt.click()
  90. time.sleep(3)
  91. elmt=driver.find_element_by_xpath("//*[@id='next-button']")
  92. elmt.click()
  93. time.sleep(3)
  94. elmt=driver.find_element_by_xpath("//*[@id='done-button']")
  95. elmt.click()
  96. time.sleep(3)
  97. while True:
  98. try:
  99. elmt=driver.find_element_by_xpath("//span[contains(text(),'檢查完畢,未發現任何問題)]")
  100. break
  101. except:
  102. print('not yet')
  103. time.sleep(5)
  104. #檢查完畢,未發現任何問題。
  105. time.sleep(9999)