yt_looping.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. def scrolling(driver,pgnum):
  24. ub = driver.find_element_by_css_selector('body')
  25. for i in range(pgnum):
  26. ub.send_keys(Keys.PAGE_DOWN)
  27. if pgnum>1:
  28. time.sleep(0.3)
  29. def restart_browser():
  30. # os.system('docker container restart p4444')
  31. # time.sleep(10)
  32. options = webdriver.ChromeOptions()
  33. # options.add_argument("--proxy-server=socks5://130.61.93.198:1080")
  34. options.add_argument("start-maximized")
  35. options.add_argument('user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data')
  36. options.add_argument('--profile-directory=Profile 7')
  37. driver=webdriver.Chrome(desired_capabilities=options.to_capabilities())
  38. driver.set_window_size(1400,1000)
  39. return driver
  40. db2 = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  41. #cursor=db2.query('select * from yt_tasks where yid not in (SELECT yid FROM hhh.yt_hhh_progress)')
  42. cursor=db2.query('select * from yt_tasks2 where yid not in (SELECT yid FROM hhh.yt_hhh_progress)')
  43. table=db2['yt_hhh_progress']
  44. yid=spath=title=desc=None
  45. for c in cursor:
  46. print(c)
  47. yid=c['yid']
  48. spath=c['path']
  49. print(spath)
  50. title=c['title']
  51. desc=c['desc']
  52. break
  53. driver=restart_browser()
  54. url='https://www.youtube.com/upload'
  55. driver.get(url)
  56. print(spath)
  57. time.sleep(5)
  58. elmt=driver.find_element_by_xpath("//input[@type='file']")
  59. elmt.send_keys(spath)
  60. time.sleep(6)
  61. elmts=driver.find_elements_by_xpath("//div[@id='textbox']")
  62. elmt=elmts[0]
  63. elmt.click()
  64. for i in range(20):
  65. elmt.send_keys(Keys.BACKSPACE)
  66. time.sleep(0.1)
  67. elmt.send_keys(title)
  68. time.sleep(1)
  69. elmt=elmts[1]
  70. elmt.click()
  71. for i in range(15):
  72. elmt.send_keys(Keys.ARROW_UP)
  73. time.sleep(0.1)
  74. elmt.send_keys(Keys.RETURN)
  75. elmt.send_keys(Keys.ARROW_UP)
  76. elmt.send_keys(desc)
  77. elmt.send_keys(Keys.RETURN)
  78. time.sleep(1)
  79. elmt=driver.find_element_by_xpath("//*[@id='next-button']")
  80. elmt.click()
  81. time.sleep(3)
  82. elmt=driver.find_element_by_xpath("//*[@id='next-button']")
  83. elmt.click()
  84. time.sleep(3)
  85. elmt=driver.find_element_by_xpath("//*[@id='next-button']")
  86. elmt.click()
  87. elmt=driver.find_element_by_xpath("//div[@class='value style-scope ytcp-video-info']")
  88. new_yid=elmt.text
  89. table.insert({'yid':yid,'new_yid':new_yid,'dt':datetime.datetime.now()})
  90. #time.sleep(9999)
  91. time.sleep(3)
  92. elmt=driver.find_element_by_xpath("//*[@id='done-button']")
  93. elmt.click()
  94. time.sleep(3)
  95. cnt=0
  96. while True:
  97. try:
  98. cnt+=1
  99. # elmt=driver.find_element_by_xpath("//span[contains(text(),'檢查完畢,未發現任何問題')]")
  100. # if cnt >=20:
  101. # break
  102. elmt=driver.find_element_by_xpath("//span[contains(text(),'檢查完畢,未發現任何問題')]")
  103. break
  104. except:
  105. print('not yet')
  106. time.sleep(5)
  107. driver.quit()
  108. #檢查完畢,未發現任何問題。
  109. sys.exit()
  110. #time.sleep(9999)