Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,33 +1,93 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
@app.get("/{user_key}/catalog/{m_type}/{cat_id}.json")
|
| 4 |
def catalog(user_key: str, m_type: str, cat_id: str):
|
| 5 |
if not is_user_valid(user_key)[0]: return {"metas": []}
|
| 6 |
|
| 7 |
if cat_id == "mswe_col":
|
| 8 |
-
# Collection
|
| 9 |
-
cursor = metas_col.find({"id": {"$regex": "^tmdb_col:"}}).sort("updated_at",
|
| 10 |
return {"metas": [{"id": m["id"], "type": "series", "name": m["name"], "poster": m["poster"]} for m in cursor]}
|
| 11 |
|
| 12 |
query = {"type": m_type, "id": {"$regex": "^tmdb:"}}
|
| 13 |
-
cursor = metas_col.find(query).sort("updated_at",
|
| 14 |
return {"metas": [{"id": m["id"], "type": m["type"], "name": m["name"], "poster": m["poster"]} for m in cursor]}
|
| 15 |
|
| 16 |
@app.get("/{user_key}/meta/{m_type}/{meta_id}.json")
|
| 17 |
def meta(user_key: str, m_type: str, meta_id: str):
|
| 18 |
if not is_user_valid(user_key)[0]: return {"meta": {}}
|
| 19 |
-
|
| 20 |
m = metas_col.find_one({"id": meta_id})
|
| 21 |
if not m: return {"meta": {}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
# UI
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
return {"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from pymongo import MongoClient, DESCENDING
|
| 6 |
+
from sync import process_sync
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"])
|
| 10 |
+
|
| 11 |
+
client = MongoClient(os.environ.get("MONGODB_URI"))
|
| 12 |
+
db = client['myanmarswe_cinema']
|
| 13 |
+
metas_col, streams_col, users_col = db['metas'], db['streams'], db['users']
|
| 14 |
+
|
| 15 |
+
def is_user_valid(key):
|
| 16 |
+
user = users_col.find_one({"key": key})
|
| 17 |
+
if not user or user.get("status") != "active": return False, "Invalid User"
|
| 18 |
+
expiry_str = user.get("expiry")
|
| 19 |
+
if expiry_str:
|
| 20 |
+
try:
|
| 21 |
+
exp_date = datetime.strptime(expiry_str.split('T')[0], "%Y-%m-%d")
|
| 22 |
+
if datetime.now() > exp_date: return False, "Expired"
|
| 23 |
+
except: pass
|
| 24 |
+
return True, "OK"
|
| 25 |
+
|
| 26 |
+
@app.get("/{user_key}/manifest.json")
|
| 27 |
+
def manifest(user_key: str):
|
| 28 |
+
valid, msg = is_user_valid(user_key)
|
| 29 |
+
return {
|
| 30 |
+
"id": "com.myanmarswe.pro", "version": "2.5.0",
|
| 31 |
+
"name": f"MSwe Cinema {'⚠️' if not valid else '💎'}",
|
| 32 |
+
"resources": ["catalog", "meta", "stream"],
|
| 33 |
+
"types": ["movie", "series"],
|
| 34 |
+
"idPrefixes": ["tmdb:"],
|
| 35 |
+
"catalogs": [
|
| 36 |
+
{"type": "movie", "id": "mswe_m", "name": "Latest Movies"},
|
| 37 |
+
{"type": "series", "id": "mswe_s", "name": "Latest TV Shows"},
|
| 38 |
+
{"type": "series", "id": "mswe_col", "name": "Collections"}
|
| 39 |
+
]
|
| 40 |
+
}
|
| 41 |
|
| 42 |
@app.get("/{user_key}/catalog/{m_type}/{cat_id}.json")
|
| 43 |
def catalog(user_key: str, m_type: str, cat_id: str):
|
| 44 |
if not is_user_valid(user_key)[0]: return {"metas": []}
|
| 45 |
|
| 46 |
if cat_id == "mswe_col":
|
| 47 |
+
# Collection IDs သီးသန့်ထုတ်ပေးခြင်း
|
| 48 |
+
cursor = metas_col.find({"id": {"$regex": "^tmdb_col:"}}).sort("updated_at", DESCENDING)
|
| 49 |
return {"metas": [{"id": m["id"], "type": "series", "name": m["name"], "poster": m["poster"]} for m in cursor]}
|
| 50 |
|
| 51 |
query = {"type": m_type, "id": {"$regex": "^tmdb:"}}
|
| 52 |
+
cursor = metas_col.find(query).sort("updated_at", DESCENDING).limit(100)
|
| 53 |
return {"metas": [{"id": m["id"], "type": m["type"], "name": m["name"], "poster": m["poster"]} for m in cursor]}
|
| 54 |
|
| 55 |
@app.get("/{user_key}/meta/{m_type}/{meta_id}.json")
|
| 56 |
def meta(user_key: str, m_type: str, meta_id: str):
|
| 57 |
if not is_user_valid(user_key)[0]: return {"meta": {}}
|
|
|
|
| 58 |
m = metas_col.find_one({"id": meta_id})
|
| 59 |
if not m: return {"meta": {}}
|
| 60 |
+
|
| 61 |
+
# Recommendation Logic
|
| 62 |
+
related = []
|
| 63 |
+
if m.get("recommendations"):
|
| 64 |
+
rec_cursor = metas_col.find({"id": {"$in": [f"tmdb:{rid}" for rid in m["recommendations"]]}})
|
| 65 |
+
related = [{"id": r["id"], "type": r["type"], "name": r["name"], "poster": r["poster"]} for r in rec_cursor]
|
| 66 |
|
| 67 |
+
# UI Optimization
|
| 68 |
+
m.pop("_id", None)
|
| 69 |
+
m["related"] = related
|
| 70 |
+
# Collection ဆိုလျှင် type ကို series အဖြစ်ပြမှ videos ပေါ်မည်
|
| 71 |
+
if meta_id.startswith("tmdb_col:"): m["type"] = "series"
|
| 72 |
+
|
| 73 |
+
return {"meta": m}
|
| 74 |
+
|
| 75 |
+
@app.get("/{user_key}/stream/{m_type}/{id}.json")
|
| 76 |
+
def stream(user_key: str, m_type: str, id: str):
|
| 77 |
+
if not is_user_valid(user_key)[0]: return {"streams": []}
|
| 78 |
+
parts = id.split(":")
|
| 79 |
+
# id format - tmdb:123 or tmdb:123:1:1
|
| 80 |
+
tid = parts[1]
|
| 81 |
+
query = {"tmdb_id": tid}
|
| 82 |
+
if len(parts) == 4: # Season/Episode ပါလျှင်
|
| 83 |
+
query["season"], query["episode"] = int(parts[2]), int(parts[3])
|
| 84 |
+
|
| 85 |
+
streams = list(streams_col.find(query))
|
| 86 |
+
return {"streams": [{"name": "MyanmarSwe 💎", "title": "🚀 High Speed Server", "url": s["url"]} for s in streams]}
|
| 87 |
+
|
| 88 |
+
@app.post("/sync-data")
|
| 89 |
+
async def sync_route(req: Request):
|
| 90 |
+
return process_sync(await req.json())
|
| 91 |
+
|
| 92 |
+
@app.get("/ping")
|
| 93 |
+
def ping(): return {"status": "awake"}
|