File size: 4,094 Bytes
61c1719
5580f57
836f75b
61c1719
836f75b
5580f57
61c1719
836f75b
 
 
 
 
15d97c1
 
a46919c
 
 
 
 
15d97c1
 
e02fa6d
836f75b
f3aab4f
61c1719
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
836f75b
 
62cd5c9
 
 
4c930f9
 
62cd5c9
 
4c930f9
 
 
 
f3aab4f
4c930f9
 
 
f3aab4f
4c930f9
5580f57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1685898
5580f57
 
 
 
 
 
3d4bf49
37d2ac3
31d5c3d
 
3d4bf49
 
 
 
37d2ac3
 
31d5c3d
3d4bf49
 
 
 
 
61c1719
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from fastapi import FastAPI,HTTPException, Request
from fastapi.responses import JSONResponse
from LoadBalancer import LoadBalancer
import logging
import os
import urllib.parse
from utils import read_json_file, is_valid_url

CACHE_DIR = os.getenv("CACHE_DIR")
TOKEN = os.getenv("TOKEN")
REPO = os.getenv("REPO")

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    global load_balancer
    load_balancer = LoadBalancer(cache_dir=CACHE_DIR, token=TOKEN, repo=REPO)

@app.get("/")
def greet_json():
    return {"Version": load_balancer.version}

@app.post("/api/post/register")
async def register_instance(request: Request):
    try:
        data = await request.json()
        if not data or "url" not in data:
            return JSONResponse(content={"error": "No URL provided"}, status_code=400)
        
        url = data["url"]
        if not is_valid_url(url):
            return JSONResponse(content={"error": "Invalid URL"}, status_code=400)

        # Register the instance
        load_balancer.register_instance(url)
        logging.info(f"Instance registered: {url}")

        return JSONResponse(content={"message": f"Instance {url} registered successfully"}, status_code=200)

    except Exception as e:
        logging.error(f"Error registering instance: {e}")
        return JSONResponse(content={"error": "Failed to register instance"}, status_code=500)

@app.get("/api/get/file_structure")
async def get_file_structure():
    return  load_balancer.file_structure

@app.get("/api/get/film/store")
async def get_film_store():
    return  load_balancer.FILM_STORE

@app.get("/api/get/tv/store")
async def get_tv_store():
    return  load_balancer.TV_STORE

@app.get("/api/get/film/all")
async def get_all_films_api():
    return load_balancer.get_all_films()

@app.get("/api/get/tv/all")
async def get_all_tvshows_api():
    return load_balancer.get_all_tv_shows()

@app.get("/api/get/film/metadata/{title}")
async def get_film_metadata_api(title: str):
    """Endpoint to get the film metadata by title."""
    if not title:
        raise HTTPException(status_code=400, detail="No title provided")
    
    json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
    
    if os.path.exists(json_cache_path):
        data = await read_json_file(json_cache_path)
        return JSONResponse(content=data)
    
    raise HTTPException(status_code=404, detail="Metadata not found")

@app.get("/api/get/tv/metadata/{title}")
async def get_tv_metadata_api(title: str):
    """Endpoint to get the TV show metadata by title."""
    if not title:
        raise HTTPException(status_code=400, detail="No title provided")
    
    json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
    
    if os.path.exists(json_cache_path):
        data = await read_json_file(json_cache_path)
        
        # Add the file structure to the metadata
        tv_structure_data = load_balancer.get_tv_structure(title)
        if tv_structure_data:
            data['file_structure'] = tv_structure_data
        
        return JSONResponse(content=data)
    
    raise HTTPException(status_code=404, detail="Metadata not found")


@app.get("/api/get/tv/metadata/{series_id}/{season}")
async def get_season_metadata_api(series_id: int, season: str):
    """Endpoint to get the TV show season metadata by id and season."""
    if not season:
        raise HTTPException(status_code=400, detail="Season must be provided and cannot be empty")
    
    # Convert series_id to string before joining the path
    json_cache_path = os.path.join(CACHE_DIR, "metadata", str(series_id), f"{season}.json")
    print(json_cache_path)
    
    if os.path.exists(json_cache_path):
        data = await read_json_file(json_cache_path)
        return JSONResponse(content=data)
    
    raise HTTPException(status_code=404, detail="Metadata not found")

@app.get('/api/get/instances')
def get_instances():
    return load_balancer.instances

@app.get('/api/get/instances/health')
def get_instances_health():
    return load_balancer.instances_health