introduced support for cookies.txt file and user-agent

This commit is contained in:
2024-10-29 15:02:42 +01:00
parent 79da502944
commit c6734c3d99
3 changed files with 21 additions and 6 deletions

View File

@@ -8,6 +8,10 @@ is_proxied = false
api_key = ""
api_key_admin = ""
[extractor]
user-agent = "" # leave empty for default
cookies_path = "" # leave empty for none
[admin]
# List of users with admin priviledges.
admins = ["admin"]

View File

@@ -66,23 +66,23 @@ def hot(data):
getcomments = False
try:
started = int(time.time())
started = time.time()
extracted_dict = ythdd_extractor.extract(url_lookup[data[1]] + videoId, getcomments=getcomments, maxcomments=comment_count)
extracted_dict["took"] = int(time.time()) - started
extracted_dict["took"] = time.time() - started
return 200, "OK", extracted_dict
except Exception as e:
incrementBadRequests()
return 400, f'error: failed to get "{videoId}" ({data[2]}). {e}', []
return 400, f'error: failed to get "{videoId}" ({data[2]})', {'error_msg': str(e)}
case "related":
videoId = data[2]
if len(videoId) != 11: # videoId sanity check
incrementBadRequests()
return 400, f'error: bad request. wrong videoId: {videoId} is {len(videoId)} characters long, but should be 11.', []
started = int(time.time())
started = time.time()
try:
extracted_related = ythdd_extractor.related('https://www.youtube.com/watch?v=' + videoId)
extracted_related['took'] = int(time.time()) - started
extracted_related['took'] = time.time() - started
return 200, "OK", extracted_related
except Exception as e:
incrementBadRequests()

View File

@@ -16,6 +16,14 @@ ytdl_opts = {
}
def extract(url: str, getcomments=False, maxcomments=""):
# TODO: check user-agent and cookiefile
if ythdd_globals.config['extractor']['user-agent']:
yt_dlp.utils.std_headers['User-Agent'] = ythdd_globals.config['extractor']['user-agent']
if ythdd_globals.config['extractor']['cookies_path']:
ytdl_opts['cookiefile'] = ythdd_globals.config['extractor']['cookies_path']
if len(url) == 11:
url = "https://www.youtube.com/watch?v=" + url
if getcomments:
@@ -37,7 +45,10 @@ def related(url: str):
videoId = url[32:44]
params = {'v': videoId}
user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:130.0) Gecko/20100101 Firefox/130.0'
# NOTE: use ESR user-agent
# user_agent = 'Mozilla/5.0 (Windows NT 10.0; rv:130.0) Gecko/20100101 Firefox/130.0'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0'
if ythdd_globals.config['extractor']['user-agent']:
user_agent = ythdd_globals.config['extractor']['user-agent']