ChandimaPrabath commited on
Commit
5580f57
·
1 Parent(s): f3aab4f

metadata routes

Browse files
Files changed (2) hide show
  1. app.py +39 -2
  2. utils.py +11 -0
app.py CHANGED
@@ -1,6 +1,9 @@
1
- from fastapi import FastAPI
 
2
  from LoadBalancer import LoadBalancer
3
  import os
 
 
4
 
5
  CACHE_DIR = os.getenv("CACHE_DIR")
6
  TOKEN = os.getenv("TOKEN")
@@ -37,4 +40,38 @@ async def get_all_films_api():
37
 
38
  @app.get("/api/get/tv/all")
39
  async def get_all_tvshows_api():
40
- return load_balancer.get_all_tv_shows()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI,HTTPException
2
+ from fastapi.responses import JSONResponse
3
  from LoadBalancer import LoadBalancer
4
  import os
5
+ import urllib.parse
6
+ from utils import read_json_file
7
 
8
  CACHE_DIR = os.getenv("CACHE_DIR")
9
  TOKEN = os.getenv("TOKEN")
 
40
 
41
  @app.get("/api/get/tv/all")
42
  async def get_all_tvshows_api():
43
+ return load_balancer.get_all_tv_shows()
44
+
45
+ @app.get("/api/get/film/metadata/{title}")
46
+ async def get_film_metadata_api(title: str):
47
+ """Endpoint to get the film metadata by title."""
48
+ if not title:
49
+ raise HTTPException(status_code=400, detail="No title provided")
50
+
51
+ json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
52
+
53
+ if os.path.exists(json_cache_path):
54
+ data = await read_json_file(json_cache_path)
55
+ return JSONResponse(content=data)
56
+
57
+ raise HTTPException(status_code=404, detail="Metadata not found")
58
+
59
+ @app.get("/api/get/tv/metadata/{title}")
60
+ async def get_tv_metadata_api(title: str):
61
+ """Endpoint to get the TV show metadata by title."""
62
+ if not title:
63
+ raise HTTPException(status_code=400, detail="No title provided")
64
+
65
+ json_cache_path = os.path.join(CACHE_DIR, f"{urllib.parse.quote(title)}.json")
66
+
67
+ if os.path.exists(json_cache_path):
68
+ data = await read_json_file(json_cache_path)
69
+
70
+ # Add the file structure to the metadata
71
+ tv_structure_data = await load_balancer.get_tv_structure(title)
72
+ if tv_structure_data:
73
+ data['file_structure'] = tv_structure_data
74
+
75
+ return JSONResponse(content=data)
76
+
77
+ raise HTTPException(status_code=404, detail="Metadata not found")
utils.py CHANGED
@@ -1,4 +1,6 @@
1
  import re
 
 
2
 
3
  def is_valid_url(url):
4
  """
@@ -62,3 +64,12 @@ def encode_episodeid(title, season, episode):
62
  str: The encoded episode ID.
63
  """
64
  return f"{title}_{season}_{episode}"
 
 
 
 
 
 
 
 
 
 
1
  import re
2
+ import aiofiles
3
+ import json
4
 
5
  def is_valid_url(url):
6
  """
 
64
  str: The encoded episode ID.
65
  """
66
  return f"{title}_{season}_{episode}"
67
+
68
+ async def read_json_file(file_path: str):
69
+ """Helper function to read JSON data from a file asynchronously."""
70
+ try:
71
+ async with aiofiles.open(file_path, 'r') as f:
72
+ data = await f.read()
73
+ return json.loads(data)
74
+ except Exception as e:
75
+ raise e