Update backend/main.py
Browse files- backend/main.py +32 -5
backend/main.py
CHANGED
|
@@ -3,6 +3,7 @@ import time
|
|
| 3 |
import asyncio
|
| 4 |
import json
|
| 5 |
import re
|
|
|
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
from fastapi import FastAPI, HTTPException, Request, Depends, Query
|
| 8 |
from fastapi.responses import Response, FileResponse
|
|
@@ -16,6 +17,9 @@ from backend.recorder import start_local_record, check_local_status, stream_file
|
|
| 16 |
from backend.sync import upload_to_dataset
|
| 17 |
from backend.config import load_sources, load_datasets, get_admin_credentials, generate_token, verify_token
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
app = FastAPI()
|
| 20 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 21 |
app.mount("/static", StaticFiles(directory="/app/frontend", html=True), name="static")
|
|
@@ -30,6 +34,7 @@ ADMIN_USER, ADMIN_PASS = get_admin_credentials()
|
|
| 30 |
async def startup():
|
| 31 |
await init_db()
|
| 32 |
await sync_sources(load_sources())
|
|
|
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
async def root():
|
|
@@ -63,14 +68,34 @@ async def logout(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
|
| 63 |
async def get_sources_ep(current_user: dict = Depends(get_current_user)):
|
| 64 |
return await get_sources()
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
@app.get("/api/epg/{source_id}")
|
| 67 |
async def get_epg_ep(source_id: str, current_user: dict = Depends(get_current_user)):
|
| 68 |
src = next((s for s in await get_sources() if s["id"]==source_id), None)
|
| 69 |
-
if not src:
|
|
|
|
|
|
|
| 70 |
adapter = get_adapter(src["type"], src["url"], src.get("api_key",""))
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
@app.post("/api/record")
|
| 76 |
async def schedule_record( data: dict, current_user: dict = Depends(get_current_user)):
|
|
@@ -172,4 +197,6 @@ async def monitor_remote(rec_id, source, adapter, rec_data):
|
|
| 172 |
token = DATASETS.get(rec_data["dataset_repo"])
|
| 173 |
asyncio.create_task(upload_to_dataset(local, rec_data["md5_name"], rec_data["original_name"], rec_data["dataset_repo"], token))
|
| 174 |
break
|
| 175 |
-
except
|
|
|
|
|
|
|
|
|
| 3 |
import asyncio
|
| 4 |
import json
|
| 5 |
import re
|
| 6 |
+
import logging
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
from fastapi import FastAPI, HTTPException, Request, Depends, Query
|
| 9 |
from fastapi.responses import Response, FileResponse
|
|
|
|
| 17 |
from backend.sync import upload_to_dataset
|
| 18 |
from backend.config import load_sources, load_datasets, get_admin_credentials, generate_token, verify_token
|
| 19 |
|
| 20 |
+
logging.basicConfig(level=logging.INFO)
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
app = FastAPI()
|
| 24 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 25 |
app.mount("/static", StaticFiles(directory="/app/frontend", html=True), name="static")
|
|
|
|
| 34 |
async def startup():
|
| 35 |
await init_db()
|
| 36 |
await sync_sources(load_sources())
|
| 37 |
+
logger.info("Application started")
|
| 38 |
|
| 39 |
@app.get("/")
|
| 40 |
async def root():
|
|
|
|
| 68 |
async def get_sources_ep(current_user: dict = Depends(get_current_user)):
|
| 69 |
return await get_sources()
|
| 70 |
|
| 71 |
+
@app.get("/api/test-connection/{source_id}")
|
| 72 |
+
async def test_connection(source_id: str, current_user: dict = Depends(get_current_user)):
|
| 73 |
+
src = next((s for s in await get_sources() if s["id"]==source_id), None)
|
| 74 |
+
if not src:
|
| 75 |
+
raise HTTPException(404, "Source not found")
|
| 76 |
+
try:
|
| 77 |
+
async with httpx.AsyncClient(timeout=10.0, verify=False) as client:
|
| 78 |
+
res = await client.get(src["url"])
|
| 79 |
+
return {"status": "ok", "status_code": res.status_code, "url": src["url"]}
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logger.error(f"Connection test failed: {str(e)}")
|
| 82 |
+
raise HTTPException(500, f"Connection failed: {str(e)}")
|
| 83 |
+
|
| 84 |
@app.get("/api/epg/{source_id}")
|
| 85 |
async def get_epg_ep(source_id: str, current_user: dict = Depends(get_current_user)):
|
| 86 |
src = next((s for s in await get_sources() if s["id"]==source_id), None)
|
| 87 |
+
if not src:
|
| 88 |
+
raise HTTPException(404, "Source not found")
|
| 89 |
+
logger.info(f"Fetching EPG from {src['type']} at {src['url']}")
|
| 90 |
adapter = get_adapter(src["type"], src["url"], src.get("api_key",""))
|
| 91 |
+
try:
|
| 92 |
+
data = await adapter.get_epg()
|
| 93 |
+
logger.info(f"EPG fetched: {len(data)} programs")
|
| 94 |
+
await cache_epg(source_id, json.dumps(data), time.time())
|
| 95 |
+
return data
|
| 96 |
+
except Exception as e:
|
| 97 |
+
logger.error(f"EPG fetch failed: {str(e)}")
|
| 98 |
+
raise HTTPException(500, f"Failed to fetch EPG: {str(e)}")
|
| 99 |
|
| 100 |
@app.post("/api/record")
|
| 101 |
async def schedule_record( data: dict, current_user: dict = Depends(get_current_user)):
|
|
|
|
| 197 |
token = DATASETS.get(rec_data["dataset_repo"])
|
| 198 |
asyncio.create_task(upload_to_dataset(local, rec_data["md5_name"], rec_data["original_name"], rec_data["dataset_repo"], token))
|
| 199 |
break
|
| 200 |
+
except Exception as e:
|
| 201 |
+
logger.error(f"Monitor error: {str(e)}")
|
| 202 |
+
pass
|