archiving community contributions on YouTube: unpublished captions, title and description translations and caption credits
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

252 lines
12 KiB

  1. from threading import Thread
  2. import requests
  3. from time import sleep
  4. from os import mkdir, rmdir, listdir, system, environ
  5. from os.path import isdir, isfile, getsize
  6. from json import dumps, loads
  7. import signal
  8. import tracker
  9. from youtube_dl import YoutubeDL
  10. from shutil import rmtree, which
  11. from queue import Queue
  12. from gc import collect
  13. from discovery import getmetadata
  14. from export import subprrun
  15. #useful Queue example: https://stackoverflow.com/a/54658363
  16. jobs = Queue()
  17. langcnt = {}
  18. try:
  19. mkdir("out")
  20. except:
  21. pass
  22. try:
  23. mkdir("directory")
  24. except:
  25. pass
  26. HEROKU = False
  27. if isfile("../Procfile"):
  28. HEROKU = True
  29. langs = ['ab', 'aa', 'af', 'sq', 'ase', 'am', 'ar', 'arc', 'hy', 'as', 'ay', 'az', 'bn', 'ba', 'eu', 'be', 'bh', 'bi', 'bs', 'br',
  30. 'bg', 'yue', 'yue-HK', 'ca', 'chr', 'zh-CN', 'zh-HK', 'zh-Hans', 'zh-SG', 'zh-TW', 'zh-Hant', 'cho', 'co', 'hr', 'cs', 'da', 'nl',
  31. 'nl-BE', 'nl-NL', 'dz', 'en', 'en-CA', 'en-IN', 'en-IE', 'en-GB', 'en-US', 'eo', 'et', 'fo', 'fj', 'fil', 'fi', 'fr', 'fr-BE',
  32. 'fr-CA', 'fr-FR', 'fr-CH', 'ff', 'gl', 'ka', 'de', 'de-AT', 'de-DE', 'de-CH', 'el', 'kl', 'gn', 'gu', 'ht', 'hak', 'hak-TW', 'ha',
  33. 'iw', 'hi', 'hi-Latn', 'ho', 'hu', 'is', 'ig', 'id', 'ia', 'ie', 'iu', 'ik', 'ga', 'it', 'ja', 'jv', 'kn', 'ks', 'kk', 'km', 'rw',
  34. 'tlh', 'ko', 'ku', 'ky', 'lo', 'la', 'lv', 'ln', 'lt', 'lb', 'mk', 'mg', 'ms', 'ml', 'mt', 'mni', 'mi', 'mr', 'mas', 'nan',
  35. 'nan-TW', 'lus', 'mo', 'mn', 'my', 'na', 'nv', 'ne', 'no', 'oc', 'or', 'om', 'ps', 'fa', 'fa-AF', 'fa-IR', 'pl', 'pt', 'pt-BR',
  36. 'pt-PT', 'pa', 'qu', 'ro', 'rm', 'rn', 'ru', 'ru-Latn', 'sm', 'sg', 'sa', 'sc', 'gd', 'sr', 'sr-Cyrl', 'sr-Latn', 'sh', 'sdp', 'sn',
  37. 'scn', 'sd', 'si', 'sk', 'sl', 'so', 'st', 'es', 'es-419', 'es-MX', 'es-ES', 'es-US', 'su', 'sw', 'ss', 'sv', 'tl', 'tg', 'ta',
  38. 'tt', 'te', 'th', 'bo', 'ti', 'tpi', 'to', 'ts', 'tn', 'tr', 'tk', 'tw', 'uk', 'ur', 'uz', 'vi', 'vo', 'vor', 'cy', 'fy', 'wo',
  39. 'xh', 'yi', 'yo', 'zu']
  40. assert which("zip") and which("rsync") and which("curl"), "Please ensure the zip, rsync, and curl commands are installed on your system."
  41. #HSID, SSID, SID cookies required
  42. if "HSID" in environ.keys() and "SSID" in environ.keys() and "SID" in environ.keys():
  43. cookies = {"HSID": environ["HSID"], "SSID": environ["SSID"], "SID": environ["SID"]}
  44. elif isfile("config.json"):
  45. cookies = loads(open("config.json").read())
  46. else:
  47. print("HSID, SSID, and SID cookies from youtube.com are required. Specify in config.json or as environment variables.")
  48. assert False
  49. if not (cookies["HSID"] and cookies["SSID"] and cookies["SID"]):
  50. print("HSID, SSID, and SID cookies from youtube.com are required. Specify in config.json or as environment variables.")
  51. assert False
  52. mysession = requests.session()
  53. mysession.headers.update({"cookie": "HSID="+cookies["HSID"]+"; SSID="+cookies["SSID"]+"; SID="+cookies["SID"], "Accept-Language": "en-US",})
  54. validationtest = mysession.get("https://www.youtube.com/timedtext_editor?action_mde_edit_form=1&v=1iNTtHUwvq4&lang=en&bl=vmp&ui=hd&ref=player&tab=captions&o=U")
  55. assert not "accounts.google.com" in validationtest.url, "Please ensure you have correctly specified account cookies."
  56. assert """<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-default yt-uix-button-has-icon" type="button" onclick=";return false;" id="yt-picker-language-button" data-button-action="yt.www.picker.load" data-button-menu-id="arrow-display" data-picker-key="language" data-picker-position="footer" data-button-toggle="true"><span class="yt-uix-button-icon-wrapper"><span class="yt-uix-button-icon yt-uix-button-icon-footer-language yt-sprite"></span></span><span class="yt-uix-button-content"> <span class="yt-picker-button-label">
  57. Language:
  58. </span>
  59. English
  60. </span><span class="yt-uix-button-arrow yt-sprite"></span></button>""" in validationtest.text, "Please make sure your YouTube and Google account language is set to English (United States)"
  61. del validationtest
  62. open("cookies.txt", "w").write("""# HTTP Cookie File
  63. .youtube.com TRUE / FALSE 1663793455 SID [SID]
  64. .youtube.com TRUE / FALSE 1663793455 HSID [HSID]
  65. .youtube.com TRUE / TRUE 1663793455 SSID [SSID]""".replace("[SID]", cookies["SID"]).replace("[HSID]", cookies["HSID"]).replace("[SSID]", cookies["SSID"]))
  66. del cookies
  67. #Graceful Shutdown
  68. class GracefulKiller:
  69. kill_now = False
  70. def __init__(self):
  71. signal.signal(signal.SIGINT, self.exit_gracefully)
  72. signal.signal(signal.SIGTERM, self.exit_gracefully)
  73. def exit_gracefully(self, signum, frame):
  74. print("Graceful exit process initiated, no longer accepting new tasks but finishing existing ones...")
  75. self.kill_now = True
  76. gkiller = GracefulKiller()
  77. #microtasks
  78. def threadrunner(jobs: Queue):
  79. global langcnt
  80. ydl = YoutubeDL({"extract_flat": "in_playlist", "simulate": True, "skip_download": True, "quiet": True, "cookiefile": "cookies.txt", "source_address": "0.0.0.0", "call_home": False})
  81. while True:
  82. if not jobs.empty():
  83. task, vid, args = jobs.get()
  84. if task == "submitdiscovery":
  85. tracker.add_item_to_tracker(args, vid)
  86. elif task == "discovery":
  87. while True:
  88. try:
  89. info = getmetadata(mysession, str(vid).strip())
  90. break
  91. except BaseException as e:
  92. print(e)
  93. print("Error in retrieving information, waiting 30 seconds")
  94. sleep(30)
  95. if info[0] or info[1]: # ccenabled or creditdata
  96. if not isdir("out/"+str(vid).strip()):
  97. mkdir("out/"+str(vid).strip())
  98. if info[1]:
  99. open("out/"+str(vid).strip()+"/"+str(vid).strip()+"_published_credits.json", "w").write(dumps(info[1]))
  100. if info[0]:
  101. langcnt[vid] = 0
  102. for langcode in langs:
  103. jobs.put(("subtitles", vid, langcode))
  104. else:
  105. jobs.put(("complete", None, "video:"+vid))
  106. for videodisc in info[2]:
  107. jobs.put(("submitdiscovery", videodisc, tracker.ItemType.Video))
  108. for channeldisc in info[3]:
  109. jobs.put(("submitdiscovery", channeldisc, tracker.ItemType.Channel))
  110. for mixdisc in info[4]:
  111. jobs.put(("submitdiscovery", mixdisc, tracker.ItemType.MixPlaylist))
  112. for playldisc in info[5]:
  113. jobs.put(("submitdiscovery", playldisc, tracker.ItemType.Playlist))
  114. elif task == "subtitles":
  115. retval = subprrun(jobs, mysession, args, vid, "default")
  116. langcnt[vid] += retval
  117. if langcnt[vid] >= 585:
  118. jobs.put(("complete", None, "video:"+vid))
  119. elif task == "subtitles-forceedit-captions":
  120. subprrun(jobs, mysession, args, vid, "forceedit-captions")
  121. elif task == "subtitles-forceedit-metadata":
  122. subprrun(jobs, mysession, args, vid, "forceedit-metadata")
  123. elif task == "channel":
  124. try:
  125. y = ydl.extract_info("https://www.youtube.com/channel/"+desit.split(":", 1)[1], download=False)
  126. for itemyv in y["entries"]:
  127. jobs.put(("submitdiscovery", itemyv["id"], tracker.ItemType.Video))
  128. jobs.put(("complete", None, "channel:"+args))
  129. except:
  130. print("YouTube-DL error, ignoring but not marking as complete...", "https://www.youtube.com/channel/"+desit.split(":", 1)[1])
  131. elif task == "playlist":
  132. try:
  133. y = ydl.extract_info("https://www.youtube.com/playlist?list="+desit.split(":", 1)[1], download=False)
  134. for itemyvp in y["entries"]:
  135. jobs.put(("submitdiscovery", itemyvp["id"], tracker.ItemType.Video))
  136. jobs.put(("complete", None, "playlist:"+args))
  137. except:
  138. print("YouTube-DL error, ignoring but not marking as complete...", "https://www.youtube.com/playlist?list="+desit.split(":", 1)[1])
  139. elif task == "complete":
  140. size = 0
  141. if ":" in args:
  142. if args.split(":", 1)[0] == "video":
  143. #check if dir is empty, make zip if needed
  144. if isdir("out/"+args.split(":", 1)[1]):
  145. if not listdir("out/"+args.split(":", 1)[1]):
  146. rmdir("out/"+args.split(":", 1)[1])
  147. else:
  148. #zip it up
  149. if not isdir("directory/"+args.split(":", 1)[1]):
  150. mkdir("directory/"+args.split(":", 1)[1])
  151. while not isfile("directory/"+args.split(":", 1)[1]+"/"+args.split(":", 1)[1]+".zip"):
  152. print("Attempting to zip item...")
  153. system("zip -9 -r -j directory/"+args.split(":", 1)[1]+"/"+args.split(":", 1)[1]+".zip out/"+args.split(":", 1)[1])
  154. #get a target
  155. targetloc = None
  156. while not targetloc:
  157. targetloc = tracker.request_upload_target()
  158. if targetloc:
  159. break
  160. else:
  161. print("Waiting 5 minutes...")
  162. sleep(300)
  163. if targetloc.startswith("rsync"):
  164. system("rsync -rltv --timeout=300 --contimeout=300 --progress --bwlimit 0 --recursive --partial --partial-dir .rsync-tmp --min-size 1 --no-compress --compress-level 0 directory/"+args.split(":", 1)[1]+"/ "+targetloc)
  165. elif targetloc.startswith("http"):
  166. system("curl -F "+args.split(":", 1)[1]+".zip=@directory/"+args.split(":", 1)[1]+"/"+args.split(":", 1)[1]+".zip "+targetloc)
  167. size = getsize("directory/"+args.split(":", 1)[1]+"/"+args.split(":", 1)[1]+".zip")
  168. #cleanup
  169. try:
  170. del langcnt[args.split(":", 1)[1]]
  171. rmtree("directory/"+args.split(":", 1)[1]+"/")
  172. rmdir("directory/"+args.split(":", 1)[1]+"/")
  173. rmtree("out/"+args.split(":", 1)[1]+"/")
  174. rmdir("out/"+args.split(":", 1)[1]+"/")
  175. except:
  176. pass
  177. tracker.mark_item_as_done(args, size)
  178. jobs.task_done()
  179. else:
  180. if not gkiller.kill_now:
  181. # get a new task from tracker
  182. collect() #cleanup
  183. desit = tracker.request_item_from_tracker()
  184. print("New task:", desit)
  185. if desit:
  186. if desit.split(":", 1)[0] == "video":
  187. jobs.put(("discovery", desit.split(":", 1)[1], None))
  188. elif desit.split(":", 1)[0] == "channel":
  189. jobs.put(("channel", None, desit.split(":", 1)[1]))
  190. elif desit.split(":", 1)[0] == "playlist":
  191. jobs.put(("playlist", None, desit.split(":", 1)[1]))
  192. else:
  193. print("Ignoring item for now", desit)
  194. else:
  195. print("Ignoring item for now", desit)
  196. else:
  197. break
  198. threads = []
  199. THREADCNT = 50
  200. if HEROKU:
  201. THREADCNT = 20
  202. #now create the rest of the threads
  203. for i in range(THREADCNT):
  204. runthread = Thread(target=threadrunner, args=(jobs,))
  205. runthread.start()
  206. threads.append(runthread)
  207. del runthread
  208. #https://stackoverflow.com/a/11968881
  209. for x in threads:
  210. x.join()
  211. threads.remove(x)
  212. del x
  213. print("Exiting...")