MyanmarSwe commited on
Commit
cca3395
·
verified ·
1 Parent(s): 7f54fa0

Update sync.py

Browse files
Files changed (1) hide show
  1. sync.py +28 -43
sync.py CHANGED
@@ -1,16 +1,5 @@
1
- import os, json, requests
2
- from datetime import datetime
3
- from pymongo import MongoClient
4
-
5
- MONGO_URI = os.environ.get("MONGODB_URI")
6
- TMDB_KEY = os.environ.get("TMDB_API_KEY")
7
-
8
- client = MongoClient(MONGO_URI)
9
- db = client['myanmarswe_cinema']
10
- metas_col, streams_col, users_col = db['metas'], db['streams'], db['users']
11
-
12
  def get_tmdb_data(tmdb_id, m_type):
13
- # m_type က sheet ထဲမှာ series ဖြစ်နေရင် tmdb အတွက် tv လို့ ပြောင်းပေးရမယ်
14
  tmdb_type = "movie" if m_type == "movie" else "tv"
15
  url = f"https://api.themoviedb.org/3/{tmdb_type}/{tmdb_id}?api_key={TMDB_KEY}&append_to_response=credits,videos,images,recommendations,belongs_to_collection"
16
 
@@ -18,26 +7,23 @@ def get_tmdb_data(tmdb_id, m_type):
18
  res = requests.get(url).json()
19
  if "id" not in res: return None
20
 
21
- # --- Smart Logo Finder ---
22
  logo_url = None
23
- images = res.get("images", {})
24
- logos = images.get("logos", [])
25
  if logos:
26
- # အင်္ဂလိပ်စာတန်းထိုးပါတဲ့ logo ကို အရင်ရှာမယ်
27
  en_logos = [l for l in logos if l.get("iso_639_1") == "en"]
28
  logo_path = en_logos[0]["file_path"] if en_logos else logos[0]["file_path"]
29
  logo_url = f"https://image.tmdb.org/t/p/w500{logo_path}"
30
 
31
- # --- Cast & Director (For Details Screen) ---
32
  credits = res.get("credits", {})
33
- cast = [c["name"] for c in credits.get("cast", [])[:10]]
34
- # Stremio UI မှာ Cast ပေါ်ဖို့ director ပါရှိတာ ပိုစိတ်ချရပါတယ်
35
  directors = [c["name"] for c in credits.get("crew", []) if c["job"] == "Director"]
36
 
37
  meta = {
38
  "_id": f"tmdb:{tmdb_id}",
39
  "id": f"tmdb:{tmdb_id}",
40
- "type": m_type, # 'movie' or 'series'
41
  "name": res.get("title") or res.get("name"),
42
  "poster": f"https://image.tmdb.org/t/p/w500{res.get('poster_path')}",
43
  "background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
@@ -55,11 +41,12 @@ def get_tmdb_data(tmdb_id, m_type):
55
  "updated_at": datetime.utcnow()
56
  }
57
 
 
58
  if m_type == "series":
59
  videos = []
60
  for s in res.get("seasons", []):
61
  s_num = s.get("season_number")
62
- if s_num == 0: continue
63
  for e_num in range(1, s.get("episode_count", 0) + 1):
64
  videos.append({
65
  "id": f"tmdb:{tmdb_id}:{s_num}:{e_num}",
@@ -69,26 +56,24 @@ def get_tmdb_data(tmdb_id, m_type):
69
  })
70
  meta["videos"] = videos
71
 
72
- return meta
73
- except Exception as e:
74
- print(f"Sync Error: {e}")
75
- return None
76
-
77
- def process_sync(data):
78
- if data.get("users"):
79
- users_col.delete_many({})
80
- users_col.insert_many(data["users"])
 
 
 
 
 
 
 
 
 
81
 
82
- items = data.get("movies_series", [])
83
- for item in items:
84
- tid, m_type = str(item['tmdb_id']), item['type']
85
- full_meta = get_tmdb_data(tid, m_type)
86
- if full_meta:
87
- if item.get('custom_overview'): full_meta['description'] = item['custom_overview']
88
- metas_col.update_one({"id": full_meta["id"]}, {"$set": full_meta}, upsert=True)
89
-
90
- # Stream URL Update
91
- stream_query = {"tmdb_id": tid, "season": item.get('season'), "episode": item.get('episode')}
92
- stream_data = {**stream_query, "type": m_type, "url": item['stream_url'], "updated_at": datetime.utcnow()}
93
- streams_col.update_one(stream_query, {"$set": stream_data}, upsert=True)
94
- return {"status": "success"}
 
1
+ # sync.py ထဲက get_tmdb_data function ကို အခုလို အစားထိုးပါ
 
 
 
 
 
 
 
 
 
 
2
  def get_tmdb_data(tmdb_id, m_type):
 
3
  tmdb_type = "movie" if m_type == "movie" else "tv"
4
  url = f"https://api.themoviedb.org/3/{tmdb_type}/{tmdb_id}?api_key={TMDB_KEY}&append_to_response=credits,videos,images,recommendations,belongs_to_collection"
5
 
 
7
  res = requests.get(url).json()
8
  if "id" not in res: return None
9
 
10
+ # --- Logo Search ---
11
  logo_url = None
12
+ logos = res.get("images", {}).get("logos", [])
 
13
  if logos:
 
14
  en_logos = [l for l in logos if l.get("iso_639_1") == "en"]
15
  logo_path = en_logos[0]["file_path"] if en_logos else logos[0]["file_path"]
16
  logo_url = f"https://image.tmdb.org/t/p/w500{logo_path}"
17
 
18
+ # --- Cast & Crew (Stremio UI အတွက် အရေးကြီးသည်) ---
19
  credits = res.get("credits", {})
20
+ cast = [c["name"] for c in credits.get("cast", [])[:15]] # ပထမ ၁၅ ယောက်
 
21
  directors = [c["name"] for c in credits.get("crew", []) if c["job"] == "Director"]
22
 
23
  meta = {
24
  "_id": f"tmdb:{tmdb_id}",
25
  "id": f"tmdb:{tmdb_id}",
26
+ "type": m_type,
27
  "name": res.get("title") or res.get("name"),
28
  "poster": f"https://image.tmdb.org/t/p/w500{res.get('poster_path')}",
29
  "background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
 
41
  "updated_at": datetime.utcnow()
42
  }
43
 
44
+ # --- TV Shows Episodes Logic (Fix for Detail Screen) ---
45
  if m_type == "series":
46
  videos = []
47
  for s in res.get("seasons", []):
48
  s_num = s.get("season_number")
49
+ if s_num == 0: continue # Specials ကို ကျော်မယ်
50
  for e_num in range(1, s.get("episode_count", 0) + 1):
51
  videos.append({
52
  "id": f"tmdb:{tmdb_id}:{s_num}:{e_num}",
 
56
  })
57
  meta["videos"] = videos
58
 
59
+ # --- Smart Collection Processing (New) ---
60
+ if meta.get("collection_info"):
61
+ col_id = meta["collection_info"]["id"]
62
+ col_url = f"https://api.themoviedb.org/3/collection/{col_id}?api_key={TMDB_KEY}"
63
+ col_res = requests.get(col_url).json()
64
+ if "id" in col_res:
65
+ col_meta = {
66
+ "_id": f"tmdb_col:{col_id}",
67
+ "id": f"tmdb_col:{col_id}",
68
+ "type": "movie",
69
+ "name": col_res.get("name"),
70
+ "poster": f"https://image.tmdb.org/t/p/w500{col_res.get('poster_path')}",
71
+ "background": f"https://image.tmdb.org/t/p/original{col_res.get('backdrop_path')}",
72
+ "description": col_res.get("overview"),
73
+ "videos": [{"id": f"tmdb:{p['id']}", "title": p.get("title")} for p in col_res.get("parts", [])],
74
+ "updated_at": datetime.utcnow()
75
+ }
76
+ metas_col.update_one({"id": col_meta["id"]}, {"$set": col_meta}, upsert=True)
77
 
78
+ return meta
79
+ except: return None