get new instance if api call fails
Bi-Rabittoh andronacomarco@gmail.com
Fri, 29 Sep 2023 14:57:12 +0200
3 files changed,
51 insertions(+),
31 deletions(-)
A
fixyoutube/api.py
@@ -0,0 +1,38 @@
+from requests import get +from requests.exceptions import JSONDecodeError +import fixyoutube.constants as c + +def get_url(video: str): + return c.INVIDIOUS_ENDPOINT.format(instance=c.INVIDIOUS_INSTANCE, video=video) + +def get_info_from_api(video): + try: + res = get(get_url(video)) + except Exception: + print("Bad instance.") + c.new_instance() + return get_info_from_api(video) + + try: + parsed = res.json() + except JSONDecodeError: + print("JSON decode error. Bad instance or video does not exist.") + return None + + try: + format = [ x for x in parsed["formatStreams"] if x["container"] == "mp4"][-1] + except KeyError: + return None + + width, height = format["size"].split("x") + + return { + "videoId": parsed["videoId"], + "title": parsed["title"], + "description": parsed["description"], + "uploader": parsed["author"], + "duration": parsed["lengthSeconds"], + "height": height, + "width": width, + "url": format["url"] + }
M
fixyoutube/constants.py
→
fixyoutube/constants.py
@@ -3,21 +3,23 @@ from os import getenv
from requests import get load_dotenv() +def new_instance(): + global INVIDIOUS_INSTANCE + instances = get("https://api.invidious.io/instances.json?pretty=1&sort_by=api,type").json() + INVIDIOUS_INSTANCE = instances[0][0] + return INVIDIOUS_INSTANCE + MAX_SIZE_MB = getenv("MAX_SIZE_MB", "50") YT_TTL_MINUTES = int(getenv("YT_TTL_MINUTES", 60 * 6)) DB_URL = getenv("DB_URL", "cache.db") -INVIDIOUS_INSTANCE = getenv("INVIDIOUS_INSTANCE", None) +INVIDIOUS_INSTANCE = getenv("INVIDIOUS_INSTANCE", new_instance()) REPO_URL = getenv("REPO_URL", "https://github.com/BiRabittoh/FixYouTube") -if INVIDIOUS_INSTANCE is None: - instances = get("https://api.invidious.io/instances.json?pretty=1&sort_by=api,type").json() - INVIDIOUS_INSTANCE = instances[0][0] - UA_REGEX = r"bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node" BASE_URL = "https://www.youtube.com/watch?v=" PROXY_HEADERS_REQUEST = { "Range": f"bytes=0-{MAX_SIZE_MB}000000" } PROXY_HEADERS_RESPONSE = { "Content-Type": "video/mp4" } -INVIDIOUS_ENDPOINT = "https://" + INVIDIOUS_INSTANCE + "/api/v1/videos/{}?fields=videoId,title,description,author,lengthSeconds,size,formatStreams" +INVIDIOUS_ENDPOINT = "https://{instance}/api/v1/videos/{video}?fields=videoId,title,description,author,lengthSeconds,size,formatStreams" # test stuff TELEGRAM_USER_AGENT = "TelegramBot (like TwitterBot)"
M
fixyoutube/db.py
→
fixyoutube/db.py
@@ -1,9 +1,8 @@
from peewee import Model, CharField, TextField, IntegerField, DateTimeField, DoesNotExist from playhouse.sqliteq import SqliteQueueDatabase -from requests import get -from requests.exceptions import JSONDecodeError from datetime import datetime, timedelta import fixyoutube.constants as c +from fixyoutube.api import get_info_from_api db = SqliteQueueDatabase(c.DB_URL)@@ -23,6 +22,8 @@ url = TextField()
timestamp = DateTimeField(default=datetime.now) def cache_video(info): + if info is None: + return try: Video.delete().where(Video.videoId == info['videoId']).execute() except DoesNotExist:@@ -38,7 +39,7 @@
delta = datetime.now() - temp.timestamp if delta > timedelta(minutes=c.YT_TTL_MINUTES): return None - + return temp def get_info(video):@@ -46,29 +47,8 @@ info = get_video_from_cache(video)
if info is not None: return info - try: - res = get(c.INVIDIOUS_ENDPOINT.format(video)).json() - except JSONDecodeError: - print("JSON decode error. Bad instance or video does not exist.") - return None - try: - format = [ x for x in res["formatStreams"] if x["container"] == "mp4"][-1] - except KeyError: - return None - - width, height = format["size"].split("x") - - info = { - "videoId": res["videoId"], - "title": res["title"], - "description": res["description"], - "uploader": res["author"], - "duration": res["lengthSeconds"], - "height": height, - "width": width, - "url": format["url"] - } + info = get_info_from_api(video) return cache_video(info)