Spaces:
Sleeping
Sleeping
Update sync.py
Browse files
sync.py
CHANGED
|
@@ -9,13 +9,12 @@ metas_col, streams_col, users_col = db['metas'], db['streams'], db['users']
|
|
| 9 |
|
| 10 |
def get_tmdb_meta(tmdb_id, m_type):
|
| 11 |
tmdb_type = "movie" if m_type == "movie" else "tv"
|
| 12 |
-
# credits ααΎαα·αΊ images ααα― append_to_response ααΌαα·αΊ αα
αΊαα«αααΊαΈ αα°αααΊ
|
| 13 |
url = f"https://api.themoviedb.org/3/{tmdb_type}/{tmdb_id}?api_key={TMDB_KEY}&append_to_response=credits,images"
|
| 14 |
|
| 15 |
try:
|
| 16 |
res = requests.get(url).json()
|
| 17 |
|
| 18 |
-
# Logo
|
| 19 |
logo = None
|
| 20 |
logos = res.get("images", {}).get("logos", [])
|
| 21 |
if logos:
|
|
@@ -23,21 +22,22 @@ def get_tmdb_meta(tmdb_id, m_type):
|
|
| 23 |
target_logo = en_logos[0] if en_logos else logos[0]
|
| 24 |
logo = f"https://image.tmdb.org/t/p/w500{target_logo['file_path']}"
|
| 25 |
|
| 26 |
-
# Cast
|
| 27 |
credits = res.get("credits", {})
|
| 28 |
-
cast = [c["name"] for c in credits.get("cast", [])[:
|
| 29 |
|
| 30 |
-
|
| 31 |
if m_type == "movie":
|
| 32 |
-
|
| 33 |
else:
|
| 34 |
-
|
| 35 |
-
if not
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
genres = [g["name"] for g in res.get("genres", [])]
|
| 39 |
-
vote_avg = res.get("vote_average")
|
| 40 |
-
|
| 41 |
meta = {
|
| 42 |
"id": f"tmdb:{tmdb_id}",
|
| 43 |
"type": m_type,
|
|
@@ -47,11 +47,10 @@ def get_tmdb_meta(tmdb_id, m_type):
|
|
| 47 |
"logo": logo,
|
| 48 |
"description": res.get("overview"),
|
| 49 |
"cast": cast,
|
| 50 |
-
"director":
|
| 51 |
-
"genres": genres,
|
| 52 |
-
"releaseInfo":
|
| 53 |
-
"imdbRating": str(round(
|
| 54 |
-
"runtime": f"{res.get('runtime')} min" if res.get('runtime') else None,
|
| 55 |
"updated_at": datetime.utcnow()
|
| 56 |
}
|
| 57 |
|
|
@@ -60,24 +59,20 @@ def get_tmdb_meta(tmdb_id, m_type):
|
|
| 60 |
for s in res.get("seasons", []):
|
| 61 |
s_num = s.get("season_number")
|
| 62 |
if s_num == 0: continue
|
| 63 |
-
|
| 64 |
s_url = f"https://api.themoviedb.org/3/tv/{tmdb_id}/season/{s_num}?api_key={TMDB_KEY}"
|
| 65 |
s_res = requests.get(s_url).json()
|
| 66 |
-
|
| 67 |
for ep in s_res.get("episodes", []):
|
| 68 |
-
e_num = ep.get("episode_number")
|
| 69 |
videos.append({
|
| 70 |
-
"id": f"tmdb:{tmdb_id}:{s_num}:{
|
| 71 |
-
"title": ep.get("name") or f"S{s_num} E{
|
| 72 |
-
"season": s_num, "episode":
|
| 73 |
-
"thumbnail": f"https://image.tmdb.org/t/p/w500{ep
|
| 74 |
"overview": ep.get("overview", "")
|
| 75 |
})
|
| 76 |
meta["videos"] = videos
|
| 77 |
-
|
| 78 |
return meta
|
| 79 |
-
except Exception as e:
|
| 80 |
-
print(f"
|
| 81 |
return None
|
| 82 |
|
| 83 |
def process_sync(data):
|
|
@@ -85,42 +80,24 @@ def process_sync(data):
|
|
| 85 |
users_col.delete_many({})
|
| 86 |
users_col.insert_many(data["users"])
|
| 87 |
|
| 88 |
-
processed_metas = {}
|
| 89 |
-
|
| 90 |
for item in data.get("movies_series", []):
|
| 91 |
-
t_id = str(item['tmdb_id'])
|
| 92 |
-
m_type = item['type']
|
| 93 |
meta_id = f"tmdb:{t_id}"
|
| 94 |
|
| 95 |
-
# Meta
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
new_meta['description'] = str(custom_ov).strip()
|
| 104 |
-
new_meta['is_custom_overview'] = True
|
| 105 |
-
else:
|
| 106 |
-
new_meta['is_custom_overview'] = False
|
| 107 |
-
|
| 108 |
-
processed_metas[meta_id] = new_meta
|
| 109 |
|
| 110 |
-
# Stream
|
| 111 |
s_query = {"tmdb_id": t_id}
|
| 112 |
if m_type == "series":
|
| 113 |
-
s_query
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
streams_col.update_one(
|
| 117 |
-
s_query,
|
| 118 |
-
{"$set": {**s_query, "url": item['stream_url'], "updated_at": datetime.utcnow()}},
|
| 119 |
-
upsert=True
|
| 120 |
-
)
|
| 121 |
|
| 122 |
-
# Meta data α‘α¬αΈαα―αΆαΈααα― MongoDB αα½ααΊ ααααΊαΈαααΊαΈαααΊ
|
| 123 |
-
for m_id, m_data in processed_metas.items():
|
| 124 |
-
metas_col.update_one({"id": m_id}, {"$set": m_data}, upsert=True)
|
| 125 |
-
|
| 126 |
return {"status": "success"}
|
|
|
|
| 9 |
|
| 10 |
def get_tmdb_meta(tmdb_id, m_type):
|
| 11 |
tmdb_type = "movie" if m_type == "movie" else "tv"
|
|
|
|
| 12 |
url = f"https://api.themoviedb.org/3/{tmdb_type}/{tmdb_id}?api_key={TMDB_KEY}&append_to_response=credits,images"
|
| 13 |
|
| 14 |
try:
|
| 15 |
res = requests.get(url).json()
|
| 16 |
|
| 17 |
+
# Logo
|
| 18 |
logo = None
|
| 19 |
logos = res.get("images", {}).get("logos", [])
|
| 20 |
if logos:
|
|
|
|
| 22 |
target_logo = en_logos[0] if en_logos else logos[0]
|
| 23 |
logo = f"https://image.tmdb.org/t/p/w500{target_logo['file_path']}"
|
| 24 |
|
| 25 |
+
# Cast & Director (Array of Strings)
|
| 26 |
credits = res.get("credits", {})
|
| 27 |
+
cast = [str(c["name"]) for c in credits.get("cast", [])[:10]] # Top 10 cast
|
| 28 |
|
| 29 |
+
directors = []
|
| 30 |
if m_type == "movie":
|
| 31 |
+
directors = [str(c["name"]) for c in credits.get("crew", []) if c.get("job") == "Director"]
|
| 32 |
else:
|
| 33 |
+
directors = [str(c["name"]) for c in res.get("created_by", [])]
|
| 34 |
+
if not directors:
|
| 35 |
+
directors = [str(c["name"]) for c in credits.get("crew", []) if c.get("job") in ["Executive Producer", "Director"]][:2]
|
| 36 |
+
|
| 37 |
+
# Year (releaseInfo)
|
| 38 |
+
raw_date = res.get("release_date") or res.get("first_air_date") or ""
|
| 39 |
+
year = str(raw_date)[:4] if raw_date else ""
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
meta = {
|
| 42 |
"id": f"tmdb:{tmdb_id}",
|
| 43 |
"type": m_type,
|
|
|
|
| 47 |
"logo": logo,
|
| 48 |
"description": res.get("overview"),
|
| 49 |
"cast": cast,
|
| 50 |
+
"director": directors,
|
| 51 |
+
"genres": [g["name"] for g in res.get("genres", [])],
|
| 52 |
+
"releaseInfo": year, # Stremio α‘αα½ααΊ Year field
|
| 53 |
+
"imdbRating": str(round(res.get("vote_average", 0), 1)) if res.get("vote_average") else None,
|
|
|
|
| 54 |
"updated_at": datetime.utcnow()
|
| 55 |
}
|
| 56 |
|
|
|
|
| 59 |
for s in res.get("seasons", []):
|
| 60 |
s_num = s.get("season_number")
|
| 61 |
if s_num == 0: continue
|
|
|
|
| 62 |
s_url = f"https://api.themoviedb.org/3/tv/{tmdb_id}/season/{s_num}?api_key={TMDB_KEY}"
|
| 63 |
s_res = requests.get(s_url).json()
|
|
|
|
| 64 |
for ep in s_res.get("episodes", []):
|
|
|
|
| 65 |
videos.append({
|
| 66 |
+
"id": f"tmdb:{tmdb_id}:{s_num}:{ep['episode_number']}",
|
| 67 |
+
"title": ep.get("name") or f"S{s_num} E{ep['episode_number']}",
|
| 68 |
+
"season": s_num, "episode": ep['episode_number'],
|
| 69 |
+
"thumbnail": f"https://image.tmdb.org/t/p/w500{ep['still_path']}" if ep.get('still_path') else meta["poster"],
|
| 70 |
"overview": ep.get("overview", "")
|
| 71 |
})
|
| 72 |
meta["videos"] = videos
|
|
|
|
| 73 |
return meta
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"Error fetching TMDB {tmdb_id}: {e}")
|
| 76 |
return None
|
| 77 |
|
| 78 |
def process_sync(data):
|
|
|
|
| 80 |
users_col.delete_many({})
|
| 81 |
users_col.insert_many(data["users"])
|
| 82 |
|
|
|
|
|
|
|
| 83 |
for item in data.get("movies_series", []):
|
| 84 |
+
t_id, m_type = str(item['tmdb_id']), item['type']
|
|
|
|
| 85 |
meta_id = f"tmdb:{t_id}"
|
| 86 |
|
| 87 |
+
# Meta ααα― α‘ααΌα² Update αα―ααΊαααΊ (Cast/Director α‘αα
αΊαααΊα
α±αααΊ)
|
| 88 |
+
new_meta = get_tmdb_meta(t_id, m_type)
|
| 89 |
+
if new_meta:
|
| 90 |
+
# Custom Overview ααΎααααΎα α
α
αΊαα±αΈααΌααΊαΈ
|
| 91 |
+
if item.get('custom_overview'):
|
| 92 |
+
new_meta['description'] = str(item['custom_overview'])
|
| 93 |
+
|
| 94 |
+
metas_col.update_one({"id": meta_id}, {"$set": new_meta}, upsert=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
# Stream update
|
| 97 |
s_query = {"tmdb_id": t_id}
|
| 98 |
if m_type == "series":
|
| 99 |
+
s_query.update({"season": int(item.get('season', 1)), "episode": int(item.get('episode', 1))})
|
| 100 |
+
|
| 101 |
+
streams_col.update_one(s_query, {"$set": {**s_query, "url": item['stream_url'], "updated_at": datetime.utcnow()}}, upsert=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return {"status": "success"}
|