Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,13 @@ from fastapi import FastAPI
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Allow all CORS so browser apps can call this API
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
|
@@ -65,7 +69,7 @@ def get_asset_urls(base_url: str, guids: list):
|
|
| 65 |
response = requests.post(url, headers=headers, data=data)
|
| 66 |
return response.json().get("items", {})
|
| 67 |
|
| 68 |
-
# Main endpoint to extract videos with
|
| 69 |
@app.get("/album/{token}")
|
| 70 |
def get_album(token: str):
|
| 71 |
try:
|
|
@@ -80,40 +84,64 @@ def get_album(token: str):
|
|
| 80 |
for photo in metadata:
|
| 81 |
best_video = None
|
| 82 |
best_size = 0
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
derivative.get("type", "").startswith("video") or
|
| 88 |
-
derivative.get("filename", "").endswith(".mp4")
|
| 89 |
-
):
|
| 90 |
try:
|
| 91 |
file_size = int(derivative.get("fileSize", 0))
|
| 92 |
-
except ValueError:
|
| 93 |
file_size = 0
|
| 94 |
if file_size > best_size:
|
| 95 |
best_size = file_size
|
| 96 |
best_video = derivative
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
if not best_video and "movies" in photo:
|
| 99 |
for movie in photo["movies"]:
|
| 100 |
-
|
|
|
|
| 101 |
try:
|
| 102 |
file_size = int(movie.get("fileSize", 0))
|
| 103 |
-
except ValueError:
|
| 104 |
file_size = 0
|
| 105 |
if file_size > best_size:
|
| 106 |
best_size = file_size
|
| 107 |
best_video = movie
|
| 108 |
-
# Build the video URL if found and if its checksum is in the asset_urls
|
| 109 |
-
if best_video and best_video.get("checksum") in asset_urls:
|
| 110 |
-
url_info = asset_urls[best_video["checksum"]]
|
| 111 |
-
video_url = f"https://{url_info['url_location']}{url_info['url_path']}"
|
| 112 |
-
video_list.append({
|
| 113 |
-
"caption": photo.get("caption", ""),
|
| 114 |
-
"url": video_url
|
| 115 |
-
})
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return {"videos": video_list}
|
| 118 |
except Exception as e:
|
| 119 |
return {"error": str(e)}
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
import logging
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# Set up basic logging
|
| 10 |
+
logging.basicConfig(level=logging.INFO)
|
| 11 |
+
|
| 12 |
# Allow all CORS so browser apps can call this API
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
|
|
|
| 69 |
response = requests.post(url, headers=headers, data=data)
|
| 70 |
return response.json().get("items", {})
|
| 71 |
|
| 72 |
+
# Main endpoint to extract videos with extensive fallback checks
|
| 73 |
@app.get("/album/{token}")
|
| 74 |
def get_album(token: str):
|
| 75 |
try:
|
|
|
|
| 84 |
for photo in metadata:
|
| 85 |
best_video = None
|
| 86 |
best_size = 0
|
| 87 |
+
|
| 88 |
+
# Check derivatives first
|
| 89 |
+
derivatives = photo.get("derivatives", {})
|
| 90 |
+
for key, derivative in derivatives.items():
|
| 91 |
+
# Log the derivative keys for debugging
|
| 92 |
+
logging.info(f"Photo {photo.get('photoGuid')} derivative key: {key} | content: {list(derivative.keys())}")
|
| 93 |
+
|
| 94 |
+
# Standard checks
|
| 95 |
+
if (derivative.get("mediaAssetType") == "video" or
|
| 96 |
derivative.get("type", "").startswith("video") or
|
| 97 |
+
derivative.get("filename", "").lower().endswith(".mp4")):
|
|
|
|
| 98 |
try:
|
| 99 |
file_size = int(derivative.get("fileSize", 0))
|
| 100 |
+
except (ValueError, TypeError):
|
| 101 |
file_size = 0
|
| 102 |
if file_size > best_size:
|
| 103 |
best_size = file_size
|
| 104 |
best_video = derivative
|
| 105 |
+
else:
|
| 106 |
+
# Fallback: Check if any value in the derivative JSON contains "mp4"
|
| 107 |
+
derivative_str = json.dumps(derivative).lower()
|
| 108 |
+
if "mp4" in derivative_str:
|
| 109 |
+
try:
|
| 110 |
+
file_size = int(derivative.get("fileSize", 0))
|
| 111 |
+
except (ValueError, TypeError):
|
| 112 |
+
file_size = 0
|
| 113 |
+
if file_size > best_size:
|
| 114 |
+
best_size = file_size
|
| 115 |
+
best_video = derivative
|
| 116 |
+
|
| 117 |
+
# Fallback to 'movies' field if present
|
| 118 |
if not best_video and "movies" in photo:
|
| 119 |
for movie in photo["movies"]:
|
| 120 |
+
logging.info(f"Photo {photo.get('photoGuid')} movie entry keys: {list(movie.keys())}")
|
| 121 |
+
if movie.get("filename", "").lower().endswith(".mp4"):
|
| 122 |
try:
|
| 123 |
file_size = int(movie.get("fileSize", 0))
|
| 124 |
+
except (ValueError, TypeError):
|
| 125 |
file_size = 0
|
| 126 |
if file_size > best_size:
|
| 127 |
best_size = file_size
|
| 128 |
best_video = movie
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Build the video URL if found
|
| 131 |
+
if best_video:
|
| 132 |
+
checksum = best_video.get("checksum")
|
| 133 |
+
if checksum in asset_urls:
|
| 134 |
+
url_info = asset_urls[checksum]
|
| 135 |
+
video_url = f"https://{url_info['url_location']}{url_info['url_path']}"
|
| 136 |
+
video_list.append({
|
| 137 |
+
"caption": photo.get("caption", ""),
|
| 138 |
+
"url": video_url
|
| 139 |
+
})
|
| 140 |
+
else:
|
| 141 |
+
logging.info(f"Checksum {checksum} not found in asset URLs for photo {photo.get('photoGuid')}")
|
| 142 |
+
else:
|
| 143 |
+
logging.info(f"No video derivative found for photo {photo.get('photoGuid')}")
|
| 144 |
+
|
| 145 |
return {"videos": video_list}
|
| 146 |
except Exception as e:
|
| 147 |
return {"error": str(e)}
|