diff --git a/app.py b/app.py index 94793ad..4161969 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +import os import http.cookiejar import json import requests @@ -13,7 +14,7 @@ app = Flask(__name__) CACHE_EXPIRATION_SECONDS = 3600 # 1 hour CACHE_SIZE_LIMIT = 100 * 1024 * 1024 # 100 MB -cache = Cache("disk_cache", size_limit=CACHE_SIZE_LIMIT) +cache = None if os.environ.get('NICONICOGAY_DISABLE_CACHE', '') != '' else Cache("disk_cache", size_limit=CACHE_SIZE_LIMIT) cookie_jar = http.cookiejar.MozillaCookieJar('cookies.txt') try: @@ -31,10 +32,11 @@ s.cookies = cookie_jar # type: ignore def proxy(video_id): logger.info(f"Received request for video ID: {video_id}") - cached_html = cache.get(video_id) - if cached_html is not None: - logger.info(f"Using cached response for video ID: {video_id}") - return Response(cached_html, mimetype="text/html") # type: ignore + if cache: + cached_html = cache.get(video_id) + if cached_html is not None: + logger.info(f"Using cached response for video ID: {video_id}") + return Response(cached_html, mimetype="text/html") # type: ignore # Not in cache or cache expired; fetch from nicovideo.jp real_url = f"https://www.nicovideo.jp/watch/{video_id}" @@ -67,14 +69,6 @@ def proxy(video_id): # Fix thumbnail if tag.get("property") == "og:image" and thumbnail_url: tag["content"] = thumbnail_url - break - - og_tags.remove(soup.find("meta", property="og:video:type")) - og_tags.append(soup.new_tag("meta", property="og:video:type", content="text/html")) - og_tags.append(soup.new_tag("meta", property="twitter:card", content="player")) - og_tags.append(soup.new_tag("meta", property="twitter:image", content="0")) - og_tags.append(soup.new_tag("meta", property="twitter:player:stream:content_type", content="text/html")) - og_tags.append(soup.new_tag("meta", property="theme-color", content="#f7f7f7")) og_tags_str = "\n".join(str(tag) for tag in og_tags) html_response = f""" @@ -89,7 +83,8 @@ def proxy(video_id): """ - logging.info(f"Caching response for video ID: {video_id}") - cache.set(video_id, html_response, expire=CACHE_EXPIRATION_SECONDS) + if cache: + logging.info(f"Caching response for video ID: {video_id}") + cache.set(video_id, html_response, expire=CACHE_EXPIRATION_SECONDS) return Response(html_response, mimetype="text/html")