MyanmarSwe commited on
Commit
70541d2
·
verified ·
1 Parent(s): 85eca92

Update sync.py

Browse files
Files changed (1) hide show
  1. sync.py +39 -52
sync.py CHANGED
@@ -2,99 +2,86 @@ import os, 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 = db['metas']
11
 
12
- def get_tmdb_data(tmdb_id, m_type):
13
  tmdb_type = "movie" if m_type == "movie" else "tv"
14
- # Episode Poster အတွက် images ပါ ထပ်တောင်းထားပါတယ်
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
 
17
  try:
18
  res = requests.get(url).json()
19
- if "id" not in res: return None
20
-
21
- # Cast List - String သီးသန့်ဖြစ်ရပါမယ်
22
- credits = res.get("credits", {})
23
- cast = [str(c.get("name")) for c in credits.get("cast", [])[:15]]
24
- directors = [str(c.get("name")) for c in credits.get("crew", []) if c.get("job") == "Director"]
25
-
26
  meta = {
27
- "_id": f"tmdb:{tmdb_id}",
28
  "id": f"tmdb:{tmdb_id}",
29
  "type": m_type,
30
  "name": res.get("title") or res.get("name"),
31
  "poster": f"https://image.tmdb.org/t/p/w500{res.get('poster_path')}",
32
  "background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
33
- "description": res.get("overview") or "",
34
- "releaseInfo": str((res.get("release_date") or res.get("first_air_date", "0000"))[:4]),
35
- "imdbRating": str(res.get("vote_average", "N/A")),
36
- "cast": cast if cast else ["N/A"],
37
- "director": directors if directors else ["N/A"],
38
- "genres": [g["name"] for g in res.get("genres", [])],
39
  "updated_at": datetime.utcnow()
40
  }
41
 
42
- # Series Episode logic with Posters
43
  if m_type == "series":
44
  videos = []
45
  for s in res.get("seasons", []):
46
  s_num = s.get("season_number")
47
  if s_num == 0: continue
48
- # Episode list fetching for thumbnails
49
- ep_url = f"https://api.themoviedb.org/3/tv/{tmdb_id}/season/{s_num}?api_key={TMDB_KEY}"
50
- ep_res = requests.get(ep_url).json()
51
- for ep in ep_res.get("episodes", []):
52
  videos.append({
53
- "id": f"tmdb:{tmdb_id}:{s_num}:{ep['episode_number']}",
54
- "title": ep.get("name") or f"Episode {ep['episode_number']}",
55
- "season": s_num,
56
- "episode": ep['episode_number'],
57
- "released": f"{ep.get('air_date')}T00:00:00.000Z",
58
- "thumbnail": f"https://image.tmdb.org/t/p/w500{ep.get('still_path')}" if ep.get("still_path") else meta["background"]
59
  })
60
  meta["videos"] = videos
61
 
62
- # Collection Logic - နှစ်အလိုက် Sort စီခြင်း
63
  if res.get("belongs_to_collection"):
64
  col_id = res["belongs_to_collection"]["id"]
65
- c_url = f"https://api.themoviedb.org/3/collection/{col_id}?api_key={TMDB_KEY}"
66
- c_res = requests.get(c_url).json()
67
 
68
- # Release Date အလိုက် Sort စီပါမယ
69
- sorted_parts = sorted(c_res.get("parts", []), key=lambda x: x.get("release_date", "9999-99-99"))
70
 
71
  col_videos = []
72
  for i, p in enumerate(sorted_parts):
73
  col_videos.append({
74
  "id": f"tmdb_col:{col_id}:1:{i+1}",
75
- "movie_id": str(p['id']), # Link ချိတ်ရန် ID သိမ်းထားခြင်း
76
- "title": f"({p.get('release_date', '')[:4]}) {p.get('title')}",
77
- "season": 1,
78
- "episode": i+1,
79
- "released": f"{p.get('release_date')}T00:00:00.000Z",
80
- "thumbnail": f"https://image.tmdb.org/t/p/w500{p.get('backdrop_path')}" if p.get('backdrop_path') else None
81
  })
82
 
83
  col_meta = {
84
- "_id": f"tmdb_col:{col_id}",
85
  "id": f"tmdb_col:{col_id}",
86
  "type": "series",
87
- "name": c_res.get("name"),
88
- "poster": f"https://image.tmdb.org/t/p/w500{c_res.get('poster_path')}",
89
- "background": f"https://image.tmdb.org/t/p/original{c_res.get('backdrop_path')}",
90
- "description": c_res.get("overview") or f"All movies in {c_res.get('name')}",
91
- "genres": ["Collection"],
92
  "videos": col_videos,
93
  "updated_at": datetime.utcnow()
94
  }
95
  metas_col.update_one({"id": col_meta["id"]}, {"$set": col_meta}, upsert=True)
96
 
97
  return meta
98
- except Exception as e:
99
- print(f"Sync Error: {e}")
100
- return None
 
 
 
 
 
 
 
 
2
  from datetime import datetime
3
  from pymongo import MongoClient
4
 
 
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,videos"
 
13
 
14
  try:
15
  res = requests.get(url).json()
16
+ cast = [c["name"] for c in res.get("credits", {}).get("cast", [])[:15]]
17
+
 
 
 
 
 
18
  meta = {
 
19
  "id": f"tmdb:{tmdb_id}",
20
  "type": m_type,
21
  "name": res.get("title") or res.get("name"),
22
  "poster": f"https://image.tmdb.org/t/p/w500{res.get('poster_path')}",
23
  "background": f"https://image.tmdb.org/t/p/original{res.get('backdrop_path')}",
24
+ "description": res.get("overview"),
25
+ "runtime": f"{res.get('runtime')} min" if m_type == "movie" else "N/A",
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
+ # Episode Poster (Thumbnail) ထည့်ခြင်း
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
+ for e_num in range(1, s.get("episode_count", 0) + 1):
 
 
 
39
  videos.append({
40
+ "id": f"tmdb:{tmdb_id}:{s_num}:{e_num}",
41
+ "title": f"S{s_num} E{e_num}",
42
+ "season": s_num, "episode": e_num,
43
+ "thumbnail": f"https://image.tmdb.org/t/p/w500{s.get('poster_path')}" # Episode Poster
 
 
44
  })
45
  meta["videos"] = videos
46
 
47
+ # --- Collection Processing (နှစ်အလိုက်စီခြင်းနှင့် Backdrop ထည့်ခြင်း) ---
48
  if res.get("belongs_to_collection"):
49
  col_id = res["belongs_to_collection"]["id"]
50
+ col_res = requests.get(f"https://api.themoviedb.org/3/collection/{col_id}?api_key={TMDB_KEY}").json()
 
51
 
52
+ # နှစ်အလိုက် Sort စီခြင
53
+ sorted_parts = sorted(col_res.get("parts", []), key=lambda x: x.get("release_date", "0000-00-00"))
54
 
55
  col_videos = []
56
  for i, p in enumerate(sorted_parts):
57
  col_videos.append({
58
  "id": f"tmdb_col:{col_id}:1:{i+1}",
59
+ "movie_id": str(p['id']),
60
+ "title": f"{p.get('title')} ({p.get('release_date', '')[:4]})",
61
+ "season": 1, "episode": i+1,
62
+ "thumbnail": f"https://image.tmdb.org/t/p/w500{p.get('backdrop_path') or p.get('poster_path')}" # Movie Backdrop
 
 
63
  })
64
 
65
  col_meta = {
 
66
  "id": f"tmdb_col:{col_id}",
67
  "type": "series",
68
+ "name": col_res.get("name"),
69
+ "poster": f"https://image.tmdb.org/t/p/w500{col_res.get('poster_path')}",
70
+ "background": f"https://image.tmdb.org/t/p/original{col_res.get('backdrop_path')}",
71
+ "description": col_res.get("overview"),
 
72
  "videos": col_videos,
73
  "updated_at": datetime.utcnow()
74
  }
75
  metas_col.update_one({"id": col_meta["id"]}, {"$set": col_meta}, upsert=True)
76
 
77
  return meta
78
+ except: return None
79
+
80
+ def process_sync(data):
81
+ # (User Sync Logic as before)
82
+ for item in data.get("movies_series", []):
83
+ meta = get_tmdb_meta(item['tmdb_id'], item['type'])
84
+ if meta:
85
+ metas_col.update_one({"id": meta["id"]}, {"$set": meta}, upsert=True)
86
+ # (Stream Update Logic as before)
87
+ return {"status": "success"}