Spaces:
Sleeping
Sleeping
Update sync.py
Browse files
sync.py
CHANGED
|
@@ -5,15 +5,15 @@ from pymongo import MongoClient
|
|
| 5 |
TMDB_KEY = os.environ.get("TMDB_API_KEY")
|
| 6 |
client = MongoClient(os.environ.get("MONGODB_URI"))
|
| 7 |
db = client['myanmarswe_cinema']
|
| 8 |
-
metas_col, streams_col = db['metas'], db['streams']
|
| 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
|
| 13 |
|
| 14 |
try:
|
| 15 |
res = requests.get(url).json()
|
| 16 |
-
# Cast ကို String list အဖြစ်
|
| 17 |
cast = [c["name"] for c in res.get("credits", {}).get("cast", [])[:15]]
|
| 18 |
|
| 19 |
meta = {
|
|
@@ -24,18 +24,16 @@ def get_tmdb_meta(tmdb_id, m_type):
|
|
| 24 |
"background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
|
| 25 |
"description": res.get("overview"),
|
| 26 |
"cast": cast,
|
| 27 |
-
"director": [c["name"] for c in res.get("credits", {}).get("crew", []) if c["job"] == "Director"],
|
| 28 |
"year": (res.get("release_date") or res.get("first_air_date") or "0000")[:4],
|
| 29 |
"updated_at": datetime.utcnow()
|
| 30 |
}
|
| 31 |
|
| 32 |
-
# TV Show Episode
|
| 33 |
if m_type == "series":
|
| 34 |
videos = []
|
| 35 |
for s in res.get("seasons", []):
|
| 36 |
s_num = s.get("season_number")
|
| 37 |
if s_num == 0: continue
|
| 38 |
-
# Episode တစ်ခုချင်းစီအတွက် Poster ယူရန်
|
| 39 |
for e_num in range(1, s.get("episode_count", 0) + 1):
|
| 40 |
videos.append({
|
| 41 |
"id": f"tmdb:{tmdb_id}:{s_num}:{e_num}",
|
|
@@ -45,13 +43,11 @@ def get_tmdb_meta(tmdb_id, m_type):
|
|
| 45 |
})
|
| 46 |
meta["videos"] = videos
|
| 47 |
|
| 48 |
-
# Collection
|
| 49 |
if res.get("belongs_to_collection"):
|
| 50 |
col_id = res["belongs_to_collection"]["id"]
|
| 51 |
col_res = requests.get(f"https://api.themoviedb.org/3/collection/{col_id}?api_key={TMDB_KEY}").json()
|
| 52 |
-
|
| 53 |
-
# Release Date အလိုက် အစီအစဉ်တကျဖြစ်အောင် စီခြင်း
|
| 54 |
-
sorted_parts = sorted(col_res.get("parts", []), key=lambda x: x.get("release_date", "9999-99-99"))
|
| 55 |
|
| 56 |
col_videos = []
|
| 57 |
for i, p in enumerate(sorted_parts):
|
|
@@ -72,4 +68,22 @@ def get_tmdb_meta(tmdb_id, m_type):
|
|
| 72 |
metas_col.update_one({"id": col_meta["id"]}, {"$set": col_meta}, upsert=True)
|
| 73 |
|
| 74 |
return meta
|
| 75 |
-
except: return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
TMDB_KEY = os.environ.get("TMDB_API_KEY")
|
| 6 |
client = MongoClient(os.environ.get("MONGODB_URI"))
|
| 7 |
db = client['myanmarswe_cinema']
|
| 8 |
+
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 |
+
url = f"https://api.themoviedb.org/3/{tmdb_type}/{tmdb_id}?api_key={TMDB_KEY}&append_to_response=credits"
|
| 13 |
|
| 14 |
try:
|
| 15 |
res = requests.get(url).json()
|
| 16 |
+
# Cast ကို String list အဖြစ်ယူခြင်း (Detail Screen အတွက်)
|
| 17 |
cast = [c["name"] for c in res.get("credits", {}).get("cast", [])[:15]]
|
| 18 |
|
| 19 |
meta = {
|
|
|
|
| 24 |
"background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
|
| 25 |
"description": res.get("overview"),
|
| 26 |
"cast": cast,
|
|
|
|
| 27 |
"year": (res.get("release_date") or res.get("first_air_date") or "0000")[:4],
|
| 28 |
"updated_at": datetime.utcnow()
|
| 29 |
}
|
| 30 |
|
| 31 |
+
# TV Show Episode Poster Fix
|
| 32 |
if m_type == "series":
|
| 33 |
videos = []
|
| 34 |
for s in res.get("seasons", []):
|
| 35 |
s_num = s.get("season_number")
|
| 36 |
if s_num == 0: continue
|
|
|
|
| 37 |
for e_num in range(1, s.get("episode_count", 0) + 1):
|
| 38 |
videos.append({
|
| 39 |
"id": f"tmdb:{tmdb_id}:{s_num}:{e_num}",
|
|
|
|
| 43 |
})
|
| 44 |
meta["videos"] = videos
|
| 45 |
|
| 46 |
+
# Collection Logic (Backdrop and Sorting)
|
| 47 |
if res.get("belongs_to_collection"):
|
| 48 |
col_id = res["belongs_to_collection"]["id"]
|
| 49 |
col_res = requests.get(f"https://api.themoviedb.org/3/collection/{col_id}?api_key={TMDB_KEY}").json()
|
| 50 |
+
sorted_parts = sorted(col_res.get("parts", []), key=lambda x: x.get("release_date", "9999"))
|
|
|
|
|
|
|
| 51 |
|
| 52 |
col_videos = []
|
| 53 |
for i, p in enumerate(sorted_parts):
|
|
|
|
| 68 |
metas_col.update_one({"id": col_meta["id"]}, {"$set": col_meta}, upsert=True)
|
| 69 |
|
| 70 |
return meta
|
| 71 |
+
except: return None
|
| 72 |
+
|
| 73 |
+
def process_sync(data):
|
| 74 |
+
# User data update
|
| 75 |
+
if "users" in data:
|
| 76 |
+
users_col.delete_many({})
|
| 77 |
+
users_col.insert_many(data["users"])
|
| 78 |
+
|
| 79 |
+
# Movies/Series sync
|
| 80 |
+
for item in data.get("movies_series", []):
|
| 81 |
+
meta = get_tmdb_meta(item['tmdb_id'], item['type'])
|
| 82 |
+
if meta:
|
| 83 |
+
if item.get('custom_overview'): meta['description'] = item['custom_overview']
|
| 84 |
+
metas_col.update_one({"id": meta["id"]}, {"$set": meta}, upsert=True)
|
| 85 |
+
|
| 86 |
+
s_query = {"tmdb_id": str(item['tmdb_id']), "season": item.get('season'), "episode": item.get('episode')}
|
| 87 |
+
streams_col.update_one(s_query, {"$set": {**s_query, "url": item['stream_url']}}, upsert=True)
|
| 88 |
+
|
| 89 |
+
return {"status": "success"}
|