Spaces:
Sleeping
Sleeping
File size: 4,877 Bytes
5ac0c2f 0d8160c 70541d2 0d8160c 61a082a 0d8160c 70541d2 29e1084 5ac0c2f 6c74d1d 334e816 de00413 334e816 5ac0c2f 334e816 3d3bf0a 5ac0c2f de00413 c9abf24 de00413 70541d2 de00413 4045317 de00413 4045317 de00413 5ac0c2f 6c74d1d cca3395 9f12ce1 5ac0c2f 3d3bf0a 89b31eb de00413 a900cab de00413 a900cab 289e111 6c74d1d 5ac0c2f 29e1084 6c74d1d 5ac0c2f de00413 5ac0c2f de00413 89b31eb 5ac0c2f cca3395 89b31eb 61a082a 5ac0c2f de00413 5ac0c2f a900cab 5ac0c2f de00413 5ac0c2f de00413 5ac0c2f de00413 6b77f6c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | import os, requests
from datetime import datetime
from pymongo import MongoClient
TMDB_KEY = os.environ.get("TMDB_API_KEY")
client = MongoClient(os.environ.get("MONGODB_URI"))
db = client['myanmarswe_cinema']
metas_col, streams_col, users_col = db['metas'], db['streams'], db['users']
def get_tmdb_meta(tmdb_id, m_type):
tmdb_type = "movie" if m_type == "movie" else "tv"
url = f"https://api.themoviedb.org/3/{tmdb_type}/{tmdb_id}?api_key={TMDB_KEY}&append_to_response=credits,images"
try:
res = requests.get(url).json()
# Runtime Logic
runtime_str = ""
if m_type == "movie":
rt = res.get("runtime")
runtime_str = f"{rt} min" if rt else ""
else:
# TV Show α‘αα½ααΊ αα»ααΊαΈαα»αΎ αααα
αΊααα― αα°αααΊ
runtimes = res.get("episode_run_time", [])
runtime_str = f"{runtimes[0]} min" if runtimes else ""
# Logo
logo = None
logos = res.get("images", {}).get("logos", [])
if logos:
en_logos = [l for l in logos if l.get("iso_639_1") == "en"]
target_logo = en_logos[0] if en_logos else logos[0]
logo = f"https://image.tmdb.org/t/p/w500{target_logo['file_path']}"
# Cast & Director (Strict String Lists)
credits = res.get("credits", {})
cast = [str(c["name"]) for c in credits.get("cast", [])[:15]]
director = []
if m_type == "movie":
director = [str(c["name"]) for c in credits.get("crew", []) if c.get("job") == "Director"]
else:
director = [str(c["name"]) for c in res.get("created_by", [])]
if not director:
director = [str(c["name"]) for c in credits.get("crew", []) if c.get("job") == "Executive Producer"][:2]
meta = {
"id": f"tmdb:{tmdb_id}",
"type": m_type,
"name": res.get("title") or res.get("name"),
"poster": f"https://image.tmdb.org/t/p/w500{res.get('poster_path')}",
"background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
"logo": logo,
"description": res.get("overview", ""),
"cast": cast,
"director": director,
"runtime": runtime_str, # Runtime field α‘αα
αΊ
"genres": [g["name"] for g in res.get("genres", [])],
"releaseInfo": str(res.get("release_date") or res.get("first_air_date") or "0000")[:4],
"imdbRating": str(round(res.get("vote_average", 0), 1)) if res.get("vote_average") else None,
"updated_at": datetime.utcnow()
}
if m_type == "series":
videos = []
for s in res.get("seasons", []):
if s.get("season_number") == 0: continue
s_url = f"https://api.themoviedb.org/3/tv/{tmdb_id}/season/{s.get('season_number')}?api_key={TMDB_KEY}"
s_res = requests.get(s_url).json()
for ep in s_res.get("episodes", []):
videos.append({
"id": f"tmdb:{tmdb_id}:{s.get('season_number')}:{ep['episode_number']}",
"title": ep.get("name") or f"S{s.get('season_number')} E{ep['episode_number']}",
"season": s.get("season_number"), "episode": ep['episode_number'],
"thumbnail": f"https://image.tmdb.org/t/p/w500{ep['still_path']}" if ep.get('still_path') else meta["poster"]
})
meta["videos"] = videos
return meta
except: return None
def process_sync(data):
if "users" in data:
users_col.delete_many({})
users_col.insert_many(data["users"])
processed_metas = {}
for item in data.get("movies_series", []):
t_id, m_type = str(item['tmdb_id']), item['type']
meta_id = f"tmdb:{t_id}"
if meta_id not in processed_metas:
new_meta = get_tmdb_meta(t_id, m_type)
if new_meta:
# Custom overview maintain logic
existing = metas_col.find_one({"id": meta_id})
if item.get('custom_overview'):
new_meta['description'] = str(item['custom_overview'])
elif existing and existing.get('is_custom_overview'):
new_meta['description'] = existing['description']
processed_metas[meta_id] = new_meta
s_query = {"tmdb_id": t_id, "season": item.get('season'), "episode": item.get('episode')}
streams_col.update_one(s_query, {"$set": {**s_query, "url": item['stream_url']}}, upsert=True)
for m_id, m_data in processed_metas.items():
metas_col.update_one({"id": m_id}, {"$set": m_data}, upsert=True)
return {"status": "success"} |