Просмотр исходного кода

Allow specifying cookies as environment variables, use requests session

pull/4/head
tech234a 3 лет назад
Родитель
Сommit
ed55a53cdb
2 измененных файлов: 18 добавлений и 8 удалений
  1. +2
    -2
      export.py
  2. +16
    -6
      worker.py

+ 2
- 2
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."



+ 16
- 6
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


Загрузка…
Отмена
Сохранить