niconico-gay/app.py
2025-01-31 14:26:14 -05:00

76 lines
2.4 KiB
Python

import http.cookiejar
import json
import requests
from bs4 import BeautifulSoup
from flask import Flask, Response
from diskcache import Cache
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)
cookie_jar = http.cookiejar.MozillaCookieJar('cookies.txt')
try:
cookie_jar.load(ignore_discard=True, ignore_expires=True)
except FileNotFoundError:
print("cookies.txt not found, starting with empty cookie jar")
s = requests.Session()
s.headers.update({
"User-Agent": "Twitterbot/1.0"
})
s.cookies = cookie_jar # type: ignore
@app.route("/watch/<video_id>")
def proxy(video_id):
cached_html = cache.get(video_id)
if cached_html is not None:
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}"
try:
r = s.get(real_url, timeout=10)
except requests.RequestException as e:
return Response(f"Error fetching the page: {e}", status=500)
soup = BeautifulSoup(r.text, "html.parser")
thumbnail_url = None
try:
server_response = soup.find("meta", {"name": "server-response"})
if server_response:
params = json.loads(server_response["content"])["data"]["response"] # type: ignore
thumbnail_url = (
params["video"]["thumbnail"].get("ogp") or
params["video"]["thumbnail"].get("player") or
params["video"]["thumbnail"].get("largeUrl") or
params["video"]["thumbnail"].get("middleUrl") or
params["video"]["thumbnail"].get("url")
)
except (KeyError, json.JSONDecodeError):
pass
og_tags = soup.find_all("meta", property=lambda x: x) # type: ignore
for tag in og_tags:
if tag.get("property") == "og:image" and thumbnail_url:
tag["content"] = thumbnail_url
og_tags_str = "\n".join(str(tag) for tag in og_tags)
html_response = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
{og_tags_str}
</head>
<body>
</body>
</html>
"""
cache.set(video_id, html_response, expire=CACHE_EXPIRATION_SECONDS)
return Response(html_response, mimetype="text/html")