Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,94 +1,127 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
app.add_middleware(CORSMiddleware, allow_origins=["*"])
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@app.get("/{user_key}/manifest.json")
|
| 15 |
def manifest(user_key: str):
|
| 16 |
-
|
| 17 |
-
raise HTTPException(status_code=401)
|
| 18 |
return {
|
| 19 |
-
"id": "com.myanmarswe.
|
| 20 |
-
"version": "
|
| 21 |
-
"name": "
|
| 22 |
-
"
|
|
|
|
| 23 |
"types": ["movie", "series"],
|
| 24 |
"idPrefixes": ["tmdb:"],
|
| 25 |
"catalogs": [
|
| 26 |
-
{"type": "movie", "id": "mswe_m", "name": "Movies"},
|
| 27 |
-
{"type": "series", "id": "mswe_s", "name": "Series"}
|
| 28 |
]
|
| 29 |
}
|
| 30 |
|
| 31 |
@app.get("/{user_key}/catalog/{type}/{id}.json")
|
| 32 |
def catalog(user_key: str, type: str):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
items = get_data(tab)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
seen_ids = set()
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"name": i.get("Title") or m.get("name"),
|
| 50 |
-
"poster": m.get("poster")
|
| 51 |
-
})
|
| 52 |
-
seen_ids.add(tid)
|
| 53 |
return {"metas": metas}
|
| 54 |
|
| 55 |
@app.get("/{user_key}/meta/{type}/{id}.json")
|
| 56 |
def meta(user_key: str, type: str, id: str):
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
@app.get("/{user_key}/stream/{type}/{id}.json")
|
| 63 |
def stream(user_key: str, type: str, id: str):
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
# ID ခွဲထုတ်ခြင်း (Movie: tmdb:123, Series: tmdb:123:1:5)
|
| 67 |
parts = id.split(":")
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
streams =
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
})
|
| 83 |
-
# Series ဖြစ်လျှင် Season နှင့် Episode တိုက်စစ်မည်
|
| 84 |
-
elif len(parts) == 4:
|
| 85 |
-
season = parts[2]
|
| 86 |
-
episode = parts[3]
|
| 87 |
-
if str(i.get("Season")) == season and str(i.get("Episode")) == episode:
|
| 88 |
-
streams.append({
|
| 89 |
-
"name": "MyanmarSwe 💎",
|
| 90 |
-
"title": f"🚀 S{season} E{episode} - Premium Link",
|
| 91 |
-
"url": i['Stream_URL']
|
| 92 |
-
})
|
| 93 |
-
|
| 94 |
-
return {"streams": streams}
|
|
|
|
| 1 |
+
import os, json
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from pymongo import MongoClient, DESCENDING
|
| 6 |
+
from sync import process_sync # အပေါ်က sync logic ကို ခေါ်သုံးခြင်း
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
app.add_middleware(CORSMiddleware, allow_origins=["*"])
|
| 10 |
|
| 11 |
+
# MongoDB Setup
|
| 12 |
+
MONGO_URI = os.environ.get("MONGODB_URI")
|
| 13 |
+
client = MongoClient(MONGO_URI)
|
| 14 |
+
db = client['myanmarswe_cinema']
|
| 15 |
+
metas_col = db['metas']
|
| 16 |
+
streams_col = db['streams']
|
| 17 |
+
users_col = db['users']
|
| 18 |
+
|
| 19 |
+
# --- Helper Functions ---
|
| 20 |
+
|
| 21 |
+
def is_user_valid(user_key: str):
|
| 22 |
+
user = users_col.find_one({"key": user_key})
|
| 23 |
+
if not user or user.get("status") != "active":
|
| 24 |
+
return False, "Invalid or Inactive User"
|
| 25 |
+
|
| 26 |
+
# Expiry Date Check
|
| 27 |
+
expiry_str = user.get("expiry")
|
| 28 |
+
if expiry_str:
|
| 29 |
+
try:
|
| 30 |
+
expiry_date = datetime.strptime(expiry_str, "%Y-%m-%d")
|
| 31 |
+
if datetime.now() > expiry_date:
|
| 32 |
+
return False, "Account Expired"
|
| 33 |
+
except: pass
|
| 34 |
+
return True, "OK"
|
| 35 |
+
|
| 36 |
+
# --- API Endpoints ---
|
| 37 |
+
|
| 38 |
+
@app.post("/sync-data")
|
| 39 |
+
async def sync_data(request: Request):
|
| 40 |
+
# Google Sheet မှ လာသော data ကို လက်ခံခြင်း
|
| 41 |
+
data = await request.json()
|
| 42 |
+
result = process_sync(data)
|
| 43 |
+
return result
|
| 44 |
|
| 45 |
@app.get("/{user_key}/manifest.json")
|
| 46 |
def manifest(user_key: str):
|
| 47 |
+
valid, msg = is_user_valid(user_key)
|
|
|
|
| 48 |
return {
|
| 49 |
+
"id": "com.myanmarswe.pro",
|
| 50 |
+
"version": "2.0.0",
|
| 51 |
+
"name": f"MSwe Cinema {'⚠️' if not valid else '💎'}",
|
| 52 |
+
"description": msg if not valid else "Ultra-Detail Private Cinema",
|
| 53 |
+
"resources": ["catalog", "meta", "stream"],
|
| 54 |
"types": ["movie", "series"],
|
| 55 |
"idPrefixes": ["tmdb:"],
|
| 56 |
"catalogs": [
|
| 57 |
+
{"type": "movie", "id": "mswe_m", "name": "Latest Movies"},
|
| 58 |
+
{"type": "series", "id": "mswe_s", "name": "Latest Series"}
|
| 59 |
]
|
| 60 |
}
|
| 61 |
|
| 62 |
@app.get("/{user_key}/catalog/{type}/{id}.json")
|
| 63 |
def catalog(user_key: str, type: str):
|
| 64 |
+
valid, _ = is_user_valid(user_key)
|
| 65 |
+
if not valid: return {"metas": []}
|
|
|
|
| 66 |
|
| 67 |
+
# နောက်ဆုံးထည့်ထားတာကို ရှေ့ဆုံးမှာပြဖို့ Sort လုပ်ခြင်း (DESCENDING)
|
| 68 |
+
cursor = metas_col.find({"type": type}).sort("updated_at", DESCENDING).limit(100)
|
|
|
|
| 69 |
|
| 70 |
+
metas = []
|
| 71 |
+
for m in cursor:
|
| 72 |
+
metas.append({
|
| 73 |
+
"id": m["id"],
|
| 74 |
+
"type": m["type"],
|
| 75 |
+
"name": m["name"],
|
| 76 |
+
"poster": m["poster"]
|
| 77 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return {"metas": metas}
|
| 79 |
|
| 80 |
@app.get("/{user_key}/meta/{type}/{id}.json")
|
| 81 |
def meta(user_key: str, type: str, id: str):
|
| 82 |
+
valid, _ = is_user_valid(user_key)
|
| 83 |
+
if not valid: raise HTTPException(status_code=401)
|
| 84 |
+
|
| 85 |
+
meta_data = metas_col.find_one({"id": id})
|
| 86 |
+
if not meta_data: return {"meta": {}}
|
| 87 |
+
|
| 88 |
+
# Recommended Movies Logic
|
| 89 |
+
# (Metas ထဲမှာရှိနေတဲ့ ကားတွေကိုပဲ Recommendation အဖြစ် ပြန်ပြပေးခြင်း)
|
| 90 |
+
rec_ids = meta_data.get("recommendations", [])
|
| 91 |
+
related = []
|
| 92 |
+
if rec_ids:
|
| 93 |
+
# DB ထဲမှာ တကယ်ရှိတဲ့ ကားတွေကိုပဲ ရှာယ���မယ်
|
| 94 |
+
rec_cursor = metas_col.find({"id": {"$in": [f"tmdb:{rid}" for rid in rec_ids]}})
|
| 95 |
+
for r in rec_cursor:
|
| 96 |
+
related.append({
|
| 97 |
+
"id": r["id"],
|
| 98 |
+
"type": r["type"],
|
| 99 |
+
"name": r["name"],
|
| 100 |
+
"poster": r["poster"]
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
meta_data["related"] = related # Stremio UI တွင် ပြသရန်
|
| 104 |
+
return {"meta": meta_data}
|
| 105 |
|
| 106 |
@app.get("/{user_key}/stream/{type}/{id}.json")
|
| 107 |
def stream(user_key: str, type: str, id: str):
|
| 108 |
+
valid, _ = is_user_valid(user_key)
|
| 109 |
+
if not valid: return {"streams": []}
|
| 110 |
|
|
|
|
| 111 |
parts = id.split(":")
|
| 112 |
+
tid = parts[1]
|
| 113 |
|
| 114 |
+
query = {"tmdb_id": tid}
|
| 115 |
+
if type == "series" and len(parts) == 4:
|
| 116 |
+
query["season"] = int(parts[2])
|
| 117 |
+
query["episode"] = int(parts[3])
|
| 118 |
|
| 119 |
+
streams = streams_col.find(query)
|
| 120 |
+
results = []
|
| 121 |
+
for s in streams:
|
| 122 |
+
results.append({
|
| 123 |
+
"name": "MyanmarSwe 💎",
|
| 124 |
+
"title": "🚀 High Speed Server",
|
| 125 |
+
"url": s["url"]
|
| 126 |
+
})
|
| 127 |
+
return {"streams": results}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|