Remove extra tags, make cache optional
This commit is contained in:
parent
e9bbe61eba
commit
91355d7ff1
13
app.py
13
app.py
|
@ -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,6 +32,7 @@ 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}")
|
||||||
|
|
||||||
|
if cache:
|
||||||
cached_html = cache.get(video_id)
|
cached_html = cache.get(video_id)
|
||||||
if cached_html is not None:
|
if cached_html is not None:
|
||||||
logger.info(f"Using cached response for video ID: {video_id}")
|
logger.info(f"Using cached response for video ID: {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,6 +83,7 @@ def proxy(video_id):
|
||||||
</html>
|
</html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if cache:
|
||||||
logging.info(f"Caching response for video ID: {video_id}")
|
logging.info(f"Caching response for video ID: {video_id}")
|
||||||
cache.set(video_id, html_response, expire=CACHE_EXPIRATION_SECONDS)
|
cache.set(video_id, html_response, expire=CACHE_EXPIRATION_SECONDS)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user