Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,6 @@ def base62_to_int(e):
|
|
| 24 |
# Construct the base URL for the shared album
|
| 25 |
def get_base_url(token: str) -> str:
|
| 26 |
first_char = token[0]
|
| 27 |
-
# If first char is "A", use only the next character; otherwise use the next two characters
|
| 28 |
n = base62_to_int(token[1]) if first_char == "A" else base62_to_int(token[1:3])
|
| 29 |
base_url = f"https://p{n:02d}-sharedstreams.icloud.com/{token}/sharedstreams/"
|
| 30 |
return base_url
|
|
@@ -66,7 +65,7 @@ def get_asset_urls(base_url: str, guids: list):
|
|
| 66 |
response = requests.post(url, headers=headers, data=data)
|
| 67 |
return response.json().get("items", {})
|
| 68 |
|
| 69 |
-
# Main endpoint to extract videos
|
| 70 |
@app.get("/album/{token}")
|
| 71 |
def get_album(token: str):
|
| 72 |
try:
|
|
@@ -81,10 +80,13 @@ def get_album(token: str):
|
|
| 81 |
for photo in metadata:
|
| 82 |
best_video = None
|
| 83 |
best_size = 0
|
| 84 |
-
#
|
| 85 |
for derivative in photo.get("derivatives", {}).values():
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
try:
|
| 89 |
file_size = int(derivative.get("fileSize", 0))
|
| 90 |
except ValueError:
|
|
@@ -92,7 +94,18 @@ def get_album(token: str):
|
|
| 92 |
if file_size > best_size:
|
| 93 |
best_size = file_size
|
| 94 |
best_video = derivative
|
| 95 |
-
# If
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if best_video and best_video.get("checksum") in asset_urls:
|
| 97 |
url_info = asset_urls[best_video["checksum"]]
|
| 98 |
video_url = f"https://{url_info['url_location']}{url_info['url_path']}"
|
|
|
|
| 24 |
# Construct the base URL for the shared album
|
| 25 |
def get_base_url(token: str) -> str:
|
| 26 |
first_char = token[0]
|
|
|
|
| 27 |
n = base62_to_int(token[1]) if first_char == "A" else base62_to_int(token[1:3])
|
| 28 |
base_url = f"https://p{n:02d}-sharedstreams.icloud.com/{token}/sharedstreams/"
|
| 29 |
return base_url
|
|
|
|
| 65 |
response = requests.post(url, headers=headers, data=data)
|
| 66 |
return response.json().get("items", {})
|
| 67 |
|
| 68 |
+
# Main endpoint to extract videos with enhanced checks
|
| 69 |
@app.get("/album/{token}")
|
| 70 |
def get_album(token: str):
|
| 71 |
try:
|
|
|
|
| 80 |
for photo in metadata:
|
| 81 |
best_video = None
|
| 82 |
best_size = 0
|
| 83 |
+
# First, check the derivatives field
|
| 84 |
for derivative in photo.get("derivatives", {}).values():
|
| 85 |
+
if (
|
| 86 |
+
derivative.get("mediaAssetType") == "video" or
|
| 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:
|
|
|
|
| 94 |
if file_size > best_size:
|
| 95 |
best_size = file_size
|
| 96 |
best_video = derivative
|
| 97 |
+
# If no derivative qualifies, check a potential 'movies' field
|
| 98 |
+
if not best_video and "movies" in photo:
|
| 99 |
+
for movie in photo["movies"]:
|
| 100 |
+
if movie.get("filename", "").endswith(".mp4"):
|
| 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']}"
|