Spaces:
Sleeping
Sleeping
Download main.py from OY-CODES/oy-stream-intelligence: direct link, hf CLI and curl.
- Browser
- Download file 6.37 kB
-
https://huggingface.co/spaces/OY-CODES/oy-stream-intelligence/resolve/main/main.py
- Command line
-
hf download hf://spaces/OY-CODES/oy-stream-intelligence/main.py
-
curl -L -o main.py https://huggingface.co/spaces/OY-CODES/oy-stream-intelligence/resolve/main/main.py
6.37 kB
| from fastapi import FastAPI, Query, Depends, Request, Form, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse | |
| from fastapi.templating import Jinja2Templates | |
| import asyncio | |
| import time | |
| import os | |
| import json | |
| from datetime import datetime | |
| from cachetools import TTLCache | |
| from core.config import settings | |
| from core.database import ( | |
| create_key, verify_key, get_all_keys, get_key_stats, | |
| get_system_stats, increment_usage, revoke_key, extend_key, edit_key_nodes | |
| ) | |
| from nodes.oy_nodes import ( | |
| OY_NetNode, OY_BoxNode, OY_BollyNode, OY_ZillaNode, OY_LookNode, | |
| OY_ArchiveNode, OY_VegaNode, OY_FzMovieNode, OY_FzSeriesNode, | |
| OY_YtsNode, OY_GogoNode, OY_SearchNode | |
| ) | |
| from tools.oy_downloader import OYDownloader | |
| app = FastAPI(title=settings.APP_NAME, version=settings.VERSION) | |
| base_dir = os.path.dirname(os.path.abspath(__file__)) | |
| templates = Jinja2Templates(directory=os.path.join(base_dir, "templates")) | |
| # Custom filter for Jinja2 | |
| def from_json(value): | |
| return json.loads(value) | |
| templates.env.filters['from_json'] = from_json | |
| # Global State | |
| startup_time = datetime.now() | |
| total_requests_counter = 0 | |
| search_cache = TTLCache(maxsize=1000, ttl=settings.CACHE_TTL) | |
| downloader = OYDownloader() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"] | |
| ) | |
| # Initialize Nodes | |
| all_nodes = { | |
| "node_alpha": OY_NetNode(), | |
| "node_beta": OY_BoxNode(), | |
| "node_gamma": OY_BollyNode(), | |
| "node_delta": OY_ZillaNode(), | |
| "node_epsilon": OY_LookNode(), | |
| "node_zeta": OY_ArchiveNode(), | |
| "node_eta": OY_VegaNode(), | |
| "node_theta": OY_FzMovieNode(), | |
| "node_iota": OY_FzSeriesNode(), | |
| "node_kappa": OY_YtsNode(), | |
| "node_lambda": OY_GogoNode(), | |
| "node_sigma": OY_SearchNode() | |
| } | |
| async def dashboard(request: Request): | |
| return templates.TemplateResponse("dashboard.html", { | |
| "request": request, | |
| "keys": get_all_keys(), | |
| "master_key": settings.API_KEY, | |
| "version": settings.VERSION, | |
| "app_name": settings.APP_NAME, | |
| "nodes": list(all_nodes.keys()) | |
| }) | |
| async def handle_create_key(owner: str = Form(...), days: int = Form(...), plan: str = Form(...), allowed_nodes: list = Form(...)): | |
| create_key(owner, allowed_nodes, days, plan) | |
| return RedirectResponse(url="/", status_code=303) | |
| async def handle_revoke_key(key: str = Form(...)): | |
| revoke_key(key) | |
| return RedirectResponse(url="/", status_code=303) | |
| async def handle_extend_key(key: str = Form(...), extra_days: int = Form(...)): | |
| extend_key(key, extra_days) | |
| return RedirectResponse(url="/", status_code=303) | |
| async def handle_edit_nodes(key: str = Form(...), allowed_nodes: list = Form(...)): | |
| edit_key_nodes(key, allowed_nodes) | |
| return RedirectResponse(url="/", status_code=303) | |
| async def list_keys(): | |
| return get_all_keys() | |
| async def key_stats(key: str): | |
| return get_key_stats(key) | |
| async def system_stats(): | |
| stats = get_system_stats() | |
| uptime = datetime.now() - startup_time | |
| stats["uptime"] = str(uptime).split('.')[0] | |
| return stats | |
| async def nodes_status(): | |
| return [node.status_dict for node in all_nodes.values()] | |
| async def deep_retrieve(request: Request, q: str = Query(...), key: str = None): | |
| global total_requests_counter | |
| total_requests_counter += 1 | |
| if key == settings.API_KEY: | |
| allowed_node_ids = list(all_nodes.keys()) | |
| else: | |
| allowed_node_ids = verify_key(key) | |
| if not allowed_node_ids: | |
| return JSONResponse({"error": "Invalid or Expired API Key"}, status_code=401) | |
| increment_usage(key) | |
| # Cache Check | |
| cache_key = f"{q}:{','.join(sorted(allowed_node_ids))}" | |
| if cache_key in search_cache: | |
| return search_cache[cache_key] | |
| start_time = time.time() | |
| tasks = [all_nodes[node_id].search(q) for node_id in allowed_node_ids if node_id in all_nodes] | |
| try: | |
| raw_results = await asyncio.wait_for(asyncio.gather(*tasks, return_exceptions=True), timeout=settings.SEARCH_TIMEOUT) | |
| except asyncio.TimeoutError: | |
| return {"error": "Search timed out"} | |
| sanitized_results = [] | |
| for r in raw_results: | |
| if isinstance(r, list): | |
| for item in r: | |
| sanitized_results.append({ | |
| "title": item.get("title"), | |
| "id": item.get("id"), | |
| "poster": item.get("poster"), | |
| "year": item.get("year"), | |
| "type": item.get("type", "content"), | |
| "source": item.get("source", "OY-Node"), | |
| "quality": "Ultra-HD" | |
| }) | |
| response = { | |
| "status": "success", | |
| "latency": f"{round((time.time() - start_time) * 1000)}ms", | |
| "data": sanitized_results, | |
| "engine": "OY-INTELLIGENCE-CORE" | |
| } | |
| search_cache[cache_key] = response | |
| return response | |
| async def get_stream_sources(q: str = Query(...), source: str = Query(...), key: str = None): | |
| if key != settings.API_KEY and not verify_key(key): | |
| return JSONResponse({"error": "Unauthorized"}, status_code=401) | |
| if source not in all_nodes: | |
| return {"error": "Invalid source node"} | |
| return await all_nodes[source].get_stream(q) | |
| async def handle_download(url: str = Query(...), key: str = None): | |
| if not verify_key(key) and key != settings.API_KEY: | |
| return JSONResponse({"error": "Unauthorized"}, status_code=401) | |
| return await downloader.get_download_link(url) | |
| async def web_search(q: str = Query(...), key: str = None): | |
| if key != settings.API_KEY and not verify_key(key): | |
| return JSONResponse({"error": "Unauthorized"}, status_code=401) | |
| return await all_nodes["node_sigma"].search(q) | |
| async def health(): | |
| return {"status": "online", "version": settings.VERSION} |