Spaces:
Sleeping
Sleeping
| 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"} |