Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,7 @@ def get_asset_urls(base_url: str, guids: list):
|
|
| 69 |
response = requests.post(url, headers=headers, data=data)
|
| 70 |
return response.json().get("items", {})
|
| 71 |
|
| 72 |
-
# Main endpoint to extract video URLs
|
| 73 |
@app.get("/album/{token}")
|
| 74 |
def get_album(token: str):
|
| 75 |
try:
|
|
@@ -82,26 +82,33 @@ def get_album(token: str):
|
|
| 82 |
video_list = []
|
| 83 |
# Process each photo in the album
|
| 84 |
for photo in metadata:
|
| 85 |
-
#
|
| 86 |
if photo.get("mediaAssetType", "").lower() == "video":
|
| 87 |
best_video = None
|
| 88 |
best_size = 0
|
| 89 |
-
# Look at each derivative
|
| 90 |
derivatives = photo.get("derivatives", {})
|
| 91 |
for key, derivative in derivatives.items():
|
| 92 |
-
# Skip the poster frame (which is clearly an image)
|
| 93 |
if key.lower() == "posterframe":
|
| 94 |
continue
|
| 95 |
-
# Try to read the fileSize as integer
|
| 96 |
try:
|
| 97 |
file_size = int(derivative.get("fileSize", 0))
|
| 98 |
except (ValueError, TypeError):
|
| 99 |
file_size = 0
|
| 100 |
-
# Choose the derivative with the highest file size
|
| 101 |
if file_size > best_size:
|
| 102 |
best_size = file_size
|
| 103 |
best_video = derivative
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
if best_video:
|
| 106 |
checksum = best_video.get("checksum")
|
| 107 |
if checksum in asset_urls:
|
|
@@ -109,7 +116,8 @@ def get_album(token: str):
|
|
| 109 |
video_url = f"https://{url_info['url_location']}{url_info['url_path']}"
|
| 110 |
video_list.append({
|
| 111 |
"caption": photo.get("caption", ""),
|
| 112 |
-
"url": video_url
|
|
|
|
| 113 |
})
|
| 114 |
else:
|
| 115 |
logging.info(f"Checksum {checksum} not found in asset_urls for photo {photo.get('photoGuid')}")
|
|
|
|
| 69 |
response = requests.post(url, headers=headers, data=data)
|
| 70 |
return response.json().get("items", {})
|
| 71 |
|
| 72 |
+
# Main endpoint to extract video URLs and include the poster (first frame)
|
| 73 |
@app.get("/album/{token}")
|
| 74 |
def get_album(token: str):
|
| 75 |
try:
|
|
|
|
| 82 |
video_list = []
|
| 83 |
# Process each photo in the album
|
| 84 |
for photo in metadata:
|
| 85 |
+
# Check if the overall photo is a video
|
| 86 |
if photo.get("mediaAssetType", "").lower() == "video":
|
| 87 |
best_video = None
|
| 88 |
best_size = 0
|
| 89 |
+
# Look at each derivative except the poster frame
|
| 90 |
derivatives = photo.get("derivatives", {})
|
| 91 |
for key, derivative in derivatives.items():
|
|
|
|
| 92 |
if key.lower() == "posterframe":
|
| 93 |
continue
|
|
|
|
| 94 |
try:
|
| 95 |
file_size = int(derivative.get("fileSize", 0))
|
| 96 |
except (ValueError, TypeError):
|
| 97 |
file_size = 0
|
|
|
|
| 98 |
if file_size > best_size:
|
| 99 |
best_size = file_size
|
| 100 |
best_video = derivative
|
| 101 |
+
|
| 102 |
+
# Also get the poster frame derivative if available
|
| 103 |
+
poster_url = None
|
| 104 |
+
poster_derivative = photo.get("derivatives", {}).get("PosterFrame")
|
| 105 |
+
if poster_derivative:
|
| 106 |
+
poster_checksum = poster_derivative.get("checksum")
|
| 107 |
+
if poster_checksum in asset_urls:
|
| 108 |
+
poster_info = asset_urls[poster_checksum]
|
| 109 |
+
poster_url = f"https://{poster_info['url_location']}{poster_info['url_path']}"
|
| 110 |
+
|
| 111 |
+
# Build the video URL if found
|
| 112 |
if best_video:
|
| 113 |
checksum = best_video.get("checksum")
|
| 114 |
if checksum in asset_urls:
|
|
|
|
| 116 |
video_url = f"https://{url_info['url_location']}{url_info['url_path']}"
|
| 117 |
video_list.append({
|
| 118 |
"caption": photo.get("caption", ""),
|
| 119 |
+
"url": video_url,
|
| 120 |
+
"poster": poster_url
|
| 121 |
})
|
| 122 |
else:
|
| 123 |
logging.info(f"Checksum {checksum} not found in asset_urls for photo {photo.get('photoGuid')}")
|