diff --git a/export.py b/export.py index cb764a7..6b87618 100644 --- a/export.py +++ b/export.py @@ -71,7 +71,7 @@ class MyHTMLParser(HTMLParser): elif 'id="metadata-description"' in self.get_starttag_text(): self.description += data -def subprrun(jobs, headers): +def subprrun(jobs, mysession): while not jobs.empty(): collect() #cleanup memory langcode, vid = jobs.get() @@ -87,7 +87,7 @@ def subprrun(jobs, headers): ("o", "U") ) - page = requests.get("https://www.youtube.com/timedtext_editor", headers=headers, params=pparams) + page = mysession.get("https://www.youtube.com/timedtext_editor", params=pparams) assert not "accounts.google.com" in page.url, "Please supply authentication cookie information in config.json. See README.md for more information." diff --git a/worker.py b/worker.py index a037b5b..26dc9a2 100644 --- a/worker.py +++ b/worker.py @@ -1,8 +1,8 @@ from threading import Thread import requests from time import sleep -from os import mkdir, rmdir, listdir -from os.path import isdir +from os import mkdir, rmdir, listdir, environ +from os.path import isdir, isfile from json import dumps, loads from shutil import make_archive, rmtree @@ -40,9 +40,19 @@ recmixes = set() recplayl = set() #HSID, SSID, SID cookies required -cookies = loads(open("config.json").read()) - -headers = {"cookie": "HSID="+cookies["HSID"]+"; SSID="+cookies["SSID"]+"; SID="+cookies["SID"], "Accept-Language": "en-US",} +if "HSID" in environ.keys() and "SSID" in environ.keys() and "SID" in environ.keys(): + cookies = {"HSID": environ["HSID"], "SSID": environ["SSID"], "SID": environ["SID"]} +elif isfile("config.json"): + cookies = loads(open("config.json").read()) +else: + print("HSID, SSID, and SID cookies from youtube.com are required. Specify in config.json or as environment variables.") + assert False +if not (cookies["HSID"] and cookies["SSID"] and cookies["SID"]): + print("HSID, SSID, and SID cookies from youtube.com are required. Specify in config.json or as environment variables.") + assert False + +mysession = requests.session() +mysession.headers.update({"cookie": "HSID="+cookies["HSID"]+"; SSID="+cookies["SSID"]+"; SID="+cookies["SID"], "Accept-Language": "en-US",}) del cookies def prrun(): @@ -166,7 +176,7 @@ while True: subthreads = [] for r in range(50): - subrunthread = Thread(target=subprrun, args=(subtjobs,headers)) + subrunthread = Thread(target=subprrun, args=(subtjobs,mysession)) subrunthread.start() subthreads.append(subrunthread) del subrunthread