add basic logging
Andronaco Marco marco.andronaco@olivetti.com
Mon, 11 Dec 2023 11:24:06 +0100
3 files changed,
22 insertions(+),
7 deletions(-)
M
fixyoutube/api.py
→
fixyoutube/api.py
@@ -1,26 +1,30 @@
from requests import get from requests.exceptions import JSONDecodeError import fixyoutube.constants as c +import logging +logger = logging.getLogger(__name__) def get_url(video: str): - return c.INVIDIOUS_ENDPOINT.format(instance=c.INVIDIOUS_INSTANCE, video=video) + req_url = c.INVIDIOUS_ENDPOINT.format(instance=c.INVIDIOUS_INSTANCE, video=video) + logger.debug("GET: " + req_url) + return req_url def get_info_from_api(video): try: res = get(get_url(video)) - except Exception: - print("Bad instance.") + except Exception as e: + logger.warn("GET error: " + str(e)) 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.") + logger.warn("JSON decode failed for the following video: " + video) return None try: - format = [ x for x in parsed["formatStreams"] if x["container"] == "mp4"][-1] + format = [ x for x in parsed["formatStreams"] if x["container"] == "mp4" ][-1] except KeyError: return None
M
fixyoutube/constants.py
→
fixyoutube/constants.py
@@ -1,18 +1,23 @@
from dotenv import load_dotenv from os import getenv from requests import get +import logging +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) +logger = logging.getLogger(__name__) 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 + logger.info("Using new Invidious instance: " + 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", new_instance()) +INVIDIOUS_INSTANCE = getenv("INVIDIOUS_INSTANCE") +if INVIDIOUS_INSTANCE is None: + new_instance() REPO_URL = getenv("REPO_URL", "https://github.com/BiRabittoh/FixYouTube") 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"
M
fixyoutube/db.py
→
fixyoutube/db.py
@@ -3,6 +3,8 @@ from playhouse.sqliteq import SqliteQueueDatabase
from datetime import datetime, timedelta import fixyoutube.constants as c from fixyoutube.api import get_info_from_api +import logging +logger = logging.getLogger(__name__) db = SqliteQueueDatabase(c.DB_URL)@@ -43,13 +45,17 @@
return temp def get_info(video): + + logger.info("Video " + str(video) + " was requested.") info = get_video_from_cache(video) if info is not None: + logger.info("Retrieved from cache.") return info info = get_info_from_api(video) + logger.info("Retrieved from API.") return cache_video(info) def clear_cache():