niconico-gay/app.py

48 lines
1.2 KiB
Python
Raw Normal View History

2025-01-31 12:57:32 -05:00
import http.cookiejar
2025-01-31 12:37:28 -05:00
from flask import Flask, Response
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
2025-01-31 12:57:32 -05:00
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")
2025-01-31 12:37:28 -05:00
s = requests.Session()
s.headers.update({
"User-Agent": "Twitterbot/1.0"
})
2025-01-31 12:57:32 -05:00
s.cookies = cookie_jar # type: ignore
2025-01-31 12:37:28 -05:00
@app.route("/watch/<video_id>")
def proxy(video_id):
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")
og_tags = soup.find_all("meta", property=lambda x: x)
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>
"""
return Response(html_response, mimetype="text/html")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)