made shit faster
Browse files
app.py
CHANGED
@@ -1,144 +1,130 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
import
|
4 |
import json
|
5 |
import logging
|
6 |
|
7 |
app = FastAPI()
|
8 |
-
|
9 |
-
# Set up logging for debugging
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
|
12 |
# Allow all CORS so browser apps can call this API
|
13 |
-
app
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
-
#
|
36 |
-
def
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
48 |
return base_url
|
49 |
|
50 |
-
#
|
51 |
-
def
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return
|
60 |
-
|
61 |
-
# Fetch asset
|
62 |
-
def get_asset_urls(base_url: str, guids: list):
|
63 |
-
url = base_url + "webasseturls"
|
64 |
-
headers = {
|
65 |
-
'Origin': 'https://www.icloud.com',
|
66 |
-
'Content-Type': 'text/plain',
|
67 |
-
}
|
68 |
data = json.dumps({"photoGuids": guids})
|
69 |
-
|
70 |
-
return response.json().get("items", {})
|
71 |
|
72 |
-
# Main endpoint
|
73 |
-
|
74 |
-
def get_album(token: str):
|
75 |
try:
|
76 |
-
base_url = get_base_url(token)
|
77 |
-
base_url = get_redirected_base_url(base_url, token)
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
for
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
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')}")
|
124 |
-
else:
|
125 |
-
logging.info(f"No video derivative found for photo {photo.get('photoGuid')}")
|
126 |
-
else:
|
127 |
-
logging.info(f"Photo {photo.get('photoGuid')} is not a video. mediaAssetType: {photo.get('mediaAssetType')}")
|
128 |
-
|
129 |
-
return {"videos": video_list}
|
130 |
except Exception as e:
|
131 |
-
|
|
|
132 |
|
133 |
-
# Debug endpoint
|
134 |
-
@app.get(
|
135 |
-
def get_album_raw(token: str):
|
136 |
try:
|
137 |
-
base_url = get_base_url(token)
|
138 |
-
base_url = get_redirected_base_url(base_url, token)
|
139 |
-
metadata = get_metadata(base_url)
|
140 |
-
guids = [
|
141 |
-
|
142 |
-
return {
|
143 |
except Exception as e:
|
144 |
-
return {
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
import httpx
|
4 |
import json
|
5 |
import logging
|
6 |
|
7 |
app = FastAPI()
|
|
|
|
|
8 |
logging.basicConfig(level=logging.INFO)
|
9 |
|
10 |
# Allow all CORS so browser apps can call this API
|
11 |
+
def configure_cors(app: FastAPI):
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"],
|
15 |
+
allow_methods=["*"],
|
16 |
+
allow_headers=["*"],
|
17 |
+
)
|
18 |
+
configure_cors(app)
|
19 |
+
|
20 |
+
# Precompute Base62 index mapping for O(1) lookup\BASE_62_MAP = {c: i for i, c in enumerate("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")}
|
21 |
+
|
22 |
+
async def get_client() -> httpx.AsyncClient:
|
23 |
+
if not hasattr(app.state, 'client'):
|
24 |
+
app.state.client = httpx.AsyncClient(timeout=10.0)
|
25 |
+
return app.state.client
|
26 |
+
|
27 |
+
# Convert base62 to int using dict lookup
|
28 |
+
def base62_to_int(token: str) -> int:
|
29 |
+
result = 0
|
30 |
+
for ch in token:
|
31 |
+
result = result * 62 + BASE_62_MAP[ch]
|
32 |
+
return result
|
33 |
|
34 |
+
# Get base URL from token
|
35 |
+
async def get_base_url(token: str) -> str:
|
36 |
+
first = token[0]
|
37 |
+
n = base62_to_int(token[1]) if first == 'A' else base62_to_int(token[1:3])
|
38 |
+
return f"https://p{n:02d}-sharedstreams.icloud.com/{token}/sharedstreams/"
|
39 |
+
|
40 |
+
# Static request details
|
41 |
+
ICLOUD_HEADERS = {'Origin': 'https://www.icloud.com', 'Content-Type': 'text/plain'}
|
42 |
+
ICLOUD_PAYLOAD = '{"streamCtag":null}'
|
43 |
+
|
44 |
+
# Handle possible 330 redirect
|
45 |
+
async def get_redirected_base_url(base_url: str, token: str) -> str:
|
46 |
+
client = await get_client()
|
47 |
+
resp = await client.post(base_url + 'webstream', headers=ICLOUD_HEADERS, data=ICLOUD_PAYLOAD, follow_redirects=False)
|
48 |
+
if resp.status_code == 330:
|
49 |
+
body = resp.json()
|
50 |
+
host = body.get('X-Apple-MMe-Host')
|
51 |
+
return f"https://{host}/{token}/sharedstreams/"
|
52 |
return base_url
|
53 |
|
54 |
+
# Generic function to POST and parse JSON
|
55 |
+
async def post_json(path: str, base_url: str, payload: str) -> dict:
|
56 |
+
client = await get_client()
|
57 |
+
resp = await client.post(f"{base_url}{path}", headers=ICLOUD_HEADERS, data=payload)
|
58 |
+
resp.raise_for_status()
|
59 |
+
return resp.json()
|
60 |
+
|
61 |
+
# Fetch metadata
|
62 |
+
async def get_metadata(base_url: str) -> list:
|
63 |
+
return (await post_json('webstream', base_url, ICLOUD_PAYLOAD)).get('photos', [])
|
64 |
+
|
65 |
+
# Fetch asset URL mappings
|
66 |
+
async def get_asset_urls(base_url: str, guids: list) -> dict:
|
|
|
|
|
|
|
|
|
|
|
67 |
data = json.dumps({"photoGuids": guids})
|
68 |
+
return (await post_json('webasseturls', base_url, data)).get('items', {})
|
|
|
69 |
|
70 |
+
# Main endpoint\@app.get('/album/{token}')
|
71 |
+
async def get_album(token: str):
|
|
|
72 |
try:
|
73 |
+
base_url = await get_base_url(token)
|
74 |
+
base_url = await get_redirected_base_url(base_url, token)
|
75 |
+
|
76 |
+
metadata = await get_metadata(base_url)
|
77 |
+
guids = [p['photoGuid'] for p in metadata]
|
78 |
+
asset_map = await get_asset_urls(base_url, guids)
|
79 |
+
|
80 |
+
videos = []
|
81 |
+
for p in metadata:
|
82 |
+
if p.get('mediaAssetType', '').lower() != 'video':
|
83 |
+
continue
|
84 |
+
|
85 |
+
# select the largest derivative (exclude poster)
|
86 |
+
derivatives = p.get('derivatives', {})
|
87 |
+
best = max((d for k, d in derivatives.items() if k.lower() != 'posterframe'),
|
88 |
+
key=lambda d: int(d.get('fileSize') or 0),
|
89 |
+
default=None)
|
90 |
+
if not best:
|
91 |
+
continue
|
92 |
+
|
93 |
+
checksum = best.get('checksum')
|
94 |
+
if checksum not in asset_map:
|
95 |
+
logging.info(f"Missing asset for checksum {checksum}")
|
96 |
+
continue
|
97 |
+
|
98 |
+
info = asset_map[checksum]
|
99 |
+
video_url = f"https://{info['url_location']}{info['url_path']}"
|
100 |
+
|
101 |
+
poster = None
|
102 |
+
pf = derivatives.get('PosterFrame')
|
103 |
+
if pf and pf.get('checksum') in asset_map:
|
104 |
+
pi = asset_map[pf['checksum']]
|
105 |
+
poster = f"https://{pi['url_location']}{pi['url_path']}"
|
106 |
+
|
107 |
+
videos.append({
|
108 |
+
'caption': p.get('caption', ''),
|
109 |
+
'url': video_url,
|
110 |
+
'poster': poster
|
111 |
+
})
|
112 |
+
|
113 |
+
return {'videos': videos}
|
114 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
except Exception as e:
|
116 |
+
logging.error(f"Error fetching album {token}: {e}")
|
117 |
+
return {'error': str(e)}
|
118 |
|
119 |
+
# Debug endpoint
|
120 |
+
@app.get('/album/{token}/raw')
|
121 |
+
async def get_album_raw(token: str):
|
122 |
try:
|
123 |
+
base_url = await get_base_url(token)
|
124 |
+
base_url = await get_redirected_base_url(base_url, token)
|
125 |
+
metadata = await get_metadata(base_url)
|
126 |
+
guids = [p['photoGuid'] for p in metadata]
|
127 |
+
asset_map = await get_asset_urls(base_url, guids)
|
128 |
+
return {'metadata': metadata, 'asset_urls': asset_map}
|
129 |
except Exception as e:
|
130 |
+
return {'error': str(e)}
|