Remove extra tags, make cache optional

This commit is contained in:
MMaker 2025-01-31 20:00:14 -05:00
parent e9bbe61eba
commit 91355d7ff1
Signed by: mmaker
GPG Key ID: CCE79B8FEDA40FB2

25
app.py
View File

@ -1,3 +1,4 @@
import os
import http.cookiejar import http.cookiejar
import json import json
import requests import requests
@ -13,7 +14,7 @@ app = Flask(__name__)
CACHE_EXPIRATION_SECONDS = 3600 # 1 hour CACHE_EXPIRATION_SECONDS = 3600 # 1 hour
CACHE_SIZE_LIMIT = 100 * 1024 * 1024 # 100 MB 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') cookie_jar = http.cookiejar.MozillaCookieJar('cookies.txt')
try: try:
@ -31,10 +32,11 @@ s.cookies = cookie_jar # type: ignore
def proxy(video_id): def proxy(video_id):
logger.info(f"Received request for video ID: {video_id}") logger.info(f"Received request for video ID: {video_id}")
cached_html = cache.get(video_id) if cache:
if cached_html is not None: cached_html = cache.get(video_id)
logger.info(f"Using cached response for video ID: {video_id}") if cached_html is not None:
return Response(cached_html, mimetype="text/html") # type: ignore 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 # Not in cache or cache expired; fetch from nicovideo.jp
real_url = f"https://www.nicovideo.jp/watch/{video_id}" real_url = f"https://www.nicovideo.jp/watch/{video_id}"
@ -67,14 +69,6 @@ def proxy(video_id):
# Fix thumbnail # Fix thumbnail
if tag.get("property") == "og:image" and thumbnail_url: if tag.get("property") == "og:image" and thumbnail_url:
tag["content"] = 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) og_tags_str = "\n".join(str(tag) for tag in og_tags)
html_response = f""" html_response = f"""
@ -89,7 +83,8 @@ def proxy(video_id):
</html> </html>
""" """
logging.info(f"Caching response for video ID: {video_id}") if cache:
cache.set(video_id, html_response, expire=CACHE_EXPIRATION_SECONDS) 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") return Response(html_response, mimetype="text/html")