Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pymongo import MongoClient, DESCENDING
|
|
@@ -14,36 +15,54 @@ metas_col, streams_col, users_col = db['metas'], db['streams'], db['users']
|
|
| 14 |
def is_user_valid(key):
|
| 15 |
user = users_col.find_one({"key": key})
|
| 16 |
if not user or user.get("status") != "active": return False, "Invalid User"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return True, "OK"
|
| 21 |
|
| 22 |
@app.get("/{user_key}/manifest.json")
|
| 23 |
def manifest(user_key: str):
|
| 24 |
valid, msg = is_user_valid(user_key)
|
| 25 |
return {
|
| 26 |
-
"id": "com.myanmarswe.pro", "version": "2.
|
| 27 |
"name": f"MSwe Cinema {'โ ๏ธ' if not valid else '๐'}",
|
| 28 |
-
"description": msg if not valid else "
|
| 29 |
"resources": ["catalog", "meta", "stream"],
|
| 30 |
"types": ["movie", "series"],
|
| 31 |
"idPrefixes": ["tmdb:"],
|
| 32 |
"catalogs": [
|
| 33 |
-
{"type": "movie", "id": "
|
| 34 |
-
{"type": "series", "id": "
|
| 35 |
-
{"type": "movie", "id": "
|
| 36 |
]
|
| 37 |
}
|
| 38 |
|
| 39 |
-
@app.get("/{user_key}/catalog/{m_type}/{
|
| 40 |
-
def catalog(user_key: str, m_type: str,
|
| 41 |
if not is_user_valid(user_key)[0]: return {"metas": []}
|
| 42 |
|
| 43 |
-
|
| 44 |
-
if
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
cursor = metas_col.find(query).sort("updated_at", DESCENDING).limit(100)
|
| 48 |
metas = [{"id": m["id"], "type": m["type"], "name": m["name"], "poster": m["poster"]} for m in cursor]
|
| 49 |
return {"metas": metas}
|
|
@@ -51,28 +70,23 @@ def catalog(user_key: str, m_type: str, id: str):
|
|
| 51 |
@app.get("/{user_key}/meta/{m_type}/{id}.json")
|
| 52 |
def meta(user_key: str, m_type: str, id: str):
|
| 53 |
if not is_user_valid(user_key)[0]: return {"meta": {}}
|
| 54 |
-
|
| 55 |
m = metas_col.find_one({"id": id})
|
| 56 |
if not m: return {"meta": {}}
|
| 57 |
|
| 58 |
-
#
|
| 59 |
related = []
|
| 60 |
if m.get("recommendations"):
|
| 61 |
rec_cursor = metas_col.find({"id": {"$in": [f"tmdb:{rid}" for rid in m["recommendations"]]}})
|
| 62 |
related = [{"id": r["id"], "type": r["type"], "name": r["name"], "poster": r["poster"]} for r in rec_cursor]
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"genres": m["genres"], "cast": m.get("cast", []), # Cast list strings
|
| 72 |
-
"trailers": m.get("trailers", []), "videos": m.get("videos", []),
|
| 73 |
-
"related": related
|
| 74 |
}
|
| 75 |
-
return {"meta": res_meta}
|
| 76 |
|
| 77 |
@app.get("/{user_key}/stream/{m_type}/{id}.json")
|
| 78 |
def stream(user_key: str, m_type: str, id: str):
|
|
@@ -82,7 +96,7 @@ def stream(user_key: str, m_type: str, id: str):
|
|
| 82 |
if m_type == "series" and len(parts) == 4:
|
| 83 |
query["season"], query["episode"] = int(parts[2]), int(parts[3])
|
| 84 |
|
| 85 |
-
streams = 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")
|
|
|
|
| 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
|
|
|
|
| 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 |
+
# Expiry date parsing fix
|
| 22 |
+
expiry_date = datetime.strptime(expiry_str.split('T')[0], "%Y-%m-%d")
|
| 23 |
+
if datetime.now() > expiry_date: return False, "Expired"
|
| 24 |
+
except: pass
|
| 25 |
return True, "OK"
|
| 26 |
|
| 27 |
@app.get("/{user_key}/manifest.json")
|
| 28 |
def manifest(user_key: str):
|
| 29 |
valid, msg = is_user_valid(user_key)
|
| 30 |
return {
|
| 31 |
+
"id": "com.myanmarswe.pro", "version": "2.2.0",
|
| 32 |
"name": f"MSwe Cinema {'โ ๏ธ' if not valid else '๐'}",
|
| 33 |
+
"description": msg if not valid else "Premium Cinema Experience",
|
| 34 |
"resources": ["catalog", "meta", "stream"],
|
| 35 |
"types": ["movie", "series"],
|
| 36 |
"idPrefixes": ["tmdb:"],
|
| 37 |
"catalogs": [
|
| 38 |
+
{"type": "movie", "id": "mswe_m", "name": "Latest Movies"},
|
| 39 |
+
{"type": "series", "id": "mswe_s", "name": "Latest TV Shows"},
|
| 40 |
+
{"type": "movie", "id": "mswe_col", "name": "Collections"}
|
| 41 |
]
|
| 42 |
}
|
| 43 |
|
| 44 |
+
@app.get("/{user_key}/catalog/{m_type}/{cat_id}.json")
|
| 45 |
+
def catalog(user_key: str, m_type: str, cat_id: str):
|
| 46 |
if not is_user_valid(user_key)[0]: return {"metas": []}
|
| 47 |
|
| 48 |
+
# --- Collection Grouping Logic ---
|
| 49 |
+
if cat_id == "mswe_col":
|
| 50 |
+
pipeline = [
|
| 51 |
+
{"$match": {"collection_info": {"$ne": None}}},
|
| 52 |
+
{"$group": {
|
| 53 |
+
"_id": "$collection_info.id",
|
| 54 |
+
"name": {"$first": "$collection_info.name"},
|
| 55 |
+
"poster": {"$first": "$collection_info.poster_path"},
|
| 56 |
+
"updated_at": {"$max": "$updated_at"}
|
| 57 |
+
}},
|
| 58 |
+
{"$sort": {"updated_at": -1}}
|
| 59 |
+
]
|
| 60 |
+
results = list(metas_col.aggregate(pipeline))
|
| 61 |
+
return {"metas": [{"id": f"tmdb:{r['_id']}", "type": "movie", "name": r["name"], "poster": f"https://image.tmdb.org/t/p/w500{r['poster']}"} for r in results]}
|
| 62 |
|
| 63 |
+
# --- Normal Movie/Series Logic ---
|
| 64 |
+
# Sheet แแฒแท type แแฒแท catalog type แแญแฏแแบแแฎแแแซแแแบ
|
| 65 |
+
query = {"type": m_type}
|
| 66 |
cursor = metas_col.find(query).sort("updated_at", DESCENDING).limit(100)
|
| 67 |
metas = [{"id": m["id"], "type": m["type"], "name": m["name"], "poster": m["poster"]} for m in cursor]
|
| 68 |
return {"metas": metas}
|
|
|
|
| 70 |
@app.get("/{user_key}/meta/{m_type}/{id}.json")
|
| 71 |
def meta(user_key: str, m_type: str, id: str):
|
| 72 |
if not is_user_valid(user_key)[0]: return {"meta": {}}
|
|
|
|
| 73 |
m = metas_col.find_one({"id": id})
|
| 74 |
if not m: return {"meta": {}}
|
| 75 |
|
| 76 |
+
# Recommendation Logic
|
| 77 |
related = []
|
| 78 |
if m.get("recommendations"):
|
| 79 |
rec_cursor = metas_col.find({"id": {"$in": [f"tmdb:{rid}" for rid in m["recommendations"]]}})
|
| 80 |
related = [{"id": r["id"], "type": r["type"], "name": r["name"], "poster": r["poster"]} for r in rec_cursor]
|
| 81 |
|
| 82 |
+
return {
|
| 83 |
+
"meta": {
|
| 84 |
+
**m,
|
| 85 |
+
"related": related,
|
| 86 |
+
"director": m.get("director", []), # Cast list แกแแผแแบ director แแซแแญแฏแทแแฑแธแแผแแบแธ
|
| 87 |
+
"logo": m.get("logo") # Transparent logo link
|
| 88 |
+
}
|
|
|
|
|
|
|
|
|
|
| 89 |
}
|
|
|
|
| 90 |
|
| 91 |
@app.get("/{user_key}/stream/{m_type}/{id}.json")
|
| 92 |
def stream(user_key: str, m_type: str, id: str):
|
|
|
|
| 96 |
if m_type == "series" and len(parts) == 4:
|
| 97 |
query["season"], query["episode"] = int(parts[2]), int(parts[3])
|
| 98 |
|
| 99 |
+
streams = list(streams_col.find(query))
|
| 100 |
return {"streams": [{"name": "MyanmarSwe ๐", "title": "๐ High Speed Server", "url": s["url"]} for s in streams]}
|
| 101 |
|
| 102 |
@app.post("/sync-data")
|