diff --git a/app.py b/app.py index e5e7684..399dff0 100644 --- a/app.py +++ b/app.py @@ -31,7 +31,8 @@ S3_BUCKET_NAME = os.environ.get('NICONICOGAY_S3_BUCKET_NAME') S3_REGION = os.environ.get('NICONICOGAY_S3_REGION') CDN_BASE_URL = os.environ.get('NICONICOGAY_CDN_BASE_URL') MAX_CONCURRENT_DOWNLOADS = 3 -CACHE_EXPIRATION_SECONDS = 3600 # 1 hour +CACHE_EXPIRATION_HTML = 60 * 60 # 1 hour +CACHE_EXPIRATION_CDN = 60 * 60 * 24 * 7 # 1 week CACHE_SIZE_LIMIT = 100 * 1024 * 1024 # 100 MB cache = None @@ -102,7 +103,7 @@ def download_and_upload_video(video_id, url, video_quality): nndownload_args = nndownload_args[1:] nndownload.execute(*nndownload_args) - if os.path.exists(temp_path) and s3_client: + if os.path.exists(temp_path) and s3_client and S3_BUCKET_NAME: logger.info(f"Downloaded video {video_id}, uploading to CDN") try: s3_key = f"niconico/{video_id}.mp4" @@ -116,7 +117,7 @@ def download_and_upload_video(video_id, url, video_quality): logger.info(f"Successfully uploaded video {video_id} to CDN") if cache is not None: - cache.set(f"{video_id}_uploaded", True, expire=CACHE_EXPIRATION_SECONDS) + cache.set(f"{video_id}_cdn", True, expire=CACHE_EXPIRATION_CDN) # Clear HTML cache for this video to ensure next view gets updated HTML cache.delete(f"{video_id}_html") logger.debug(f"Cleared cache for video ID: {video_id}") @@ -166,14 +167,14 @@ worker_thread.start() def is_video_in_cdn(video_id): """Check if video exists in CDN""" - if cache is not None and cache.get(f"{video_id}_uploaded"): + if cache is not None and cache.get(f"{video_id}_cdn"): logger.debug(f"Video {video_id} is already uploaded to CDN (cached)") return True - if not s3_client: + if not s3_client or not S3_BUCKET_NAME: logger.warning("S3 client not configured. Cannot check if video exists in CDN.") return False - + try: s3_client.head_object(Bucket=S3_BUCKET_NAME, Key=f"niconico/{video_id}.mp4") return True @@ -294,7 +295,7 @@ def proxy(video_id): real_url = f"https://www.nicovideo.jp/watch/{video_id}" params, soup = get_data(video_id, real_url) - if not params and not soup: + if not params or not soup: logger.error(f"Failed to retrieve data for video ID '{video_id}'") return Response("Video not found", status=404) @@ -325,7 +326,7 @@ def proxy(video_id): logger.info(f"Queued video ID {video_id} for download") cdn_video_url = get_cdn_url(video_id) - og_tags = soup.find_all("meta", property=lambda x: x) + og_tags = soup.find_all("meta", attrs={"property": True}) for tag in og_tags: if 'data-server' in tag.attrs: del tag.attrs['data-server'] @@ -361,7 +362,7 @@ if you want to download videos, please consider using a tool like nndownload: ht if cache is not None: logger.info(f"Caching response for video ID: {video_id}") - cache.set(f"{video_id}_html", html_response, expire=CACHE_EXPIRATION_SECONDS) + cache.set(f"{video_id}_html", html_response, expire=CACHE_EXPIRATION_HTML) logger.info(f"Returning response for video ID: {video_id}") return Response(html_response, mimetype="text/html")