diff --git a/config.default.toml b/config.default.toml index 79cfb78..14b0bde 100644 --- a/config.default.toml +++ b/config.default.toml @@ -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"] diff --git a/ythdd_api_v1.py b/ythdd_api_v1.py index 4ab5992..62ab976 100644 --- a/ythdd_api_v1.py +++ b/ythdd_api_v1.py @@ -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() diff --git a/ythdd_extractor.py b/ythdd_extractor.py index 7ad2ed9..642ddae 100644 --- a/ythdd_extractor.py +++ b/ythdd_extractor.py @@ -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']