r3hab commited on
Commit
86dbab6
·
verified ·
1 Parent(s): 665108a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -42
app.py CHANGED
@@ -1,50 +1,38 @@
1
- from fastapi import FastAPI, Query
 
2
  import json
3
- from typing import Optional
 
4
 
5
  app = FastAPI()
6
 
7
- DATA_FILE = "data.json"
 
 
 
 
 
 
 
8
 
9
- def load_data():
10
- """Loads data from the JSON file."""
11
- try:
12
- with open(DATA_FILE, "r") as f:
13
- data = json.load(f)
14
- return data
15
- except FileNotFoundError:
16
- return {}
17
 
18
- @app.get("/search")
19
- async def search_links(
20
- query: str = Query(..., description="Search term to find relevant links."),
21
- quality: Optional[str] = Query(None, description="Optional: Filter links by quality (e.g., 1080P, 720P).")
22
- ):
23
  """
24
- Searches for links based on the provided query and optional quality.
25
-
26
- Args:
27
- query: The search term.
28
- quality: Optional quality filter (e.g., "1080P", "720P").
29
-
30
- Returns:
31
- A list of matching links with their titles and source URLs.
32
  """
33
- data = load_data()
34
- results = []
35
- query_lower = query.lower()
36
-
37
- for url, links in data.items():
38
- if query_lower in url.lower():
39
- # Add the main URL itself as a potential match, regardless of quality filter
40
- results.append({"source_url": url, "link": url, "title": url})
41
- for link_data in links:
42
- if query_lower in link_data["title"].lower():
43
- if quality is None or quality.lower() in link_data["title"].lower():
44
- results.append({"source_url": url, "link": link_data["link"], "title": link_data["title"]})
45
-
46
- return results
47
-
48
- if __name__ == "__main__":
49
- import uvicorn
50
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI, HTTPException, Query
2
+ from typing import List, Dict
3
  import json
4
+ from fuzzywuzzy import process # Install fuzzywuzzy: pip install fuzzywuzzy
5
+
6
 
7
  app = FastAPI()
8
 
9
+ # Load data from the JSON file. (Crucial: Handle potential file errors)
10
+ try:
11
+ with open("converted_data.json", "r") as f:
12
+ data = json.load(f)
13
+ except FileNotFoundError:
14
+ raise HTTPException(status_code=500, detail="File 'converted_data.json' not found.")
15
+ except json.JSONDecodeError as e:
16
+ raise HTTPException(status_code=500, detail=f"Error decoding JSON: {e}")
17
 
 
 
 
 
 
 
 
 
18
 
19
+ @app.get("/search_links/")
20
+ async def search_links(query: str = Query(..., description="Search query")):
 
 
 
21
  """
22
+ Searches the movie titles and returns links matching the query using fuzzy search.
 
 
 
 
 
 
 
23
  """
24
+ results = {}
25
+ for title, links in data.items():
26
+ ratio = process.extractOne(query, title)
27
+ if ratio[1] > 80: # Adjust threshold as needed
28
+ results[title] = links
29
+ else:
30
+ # Use extract on links in each entry
31
+ for entry in links:
32
+ if entry:
33
+ ratio_entry = process.extractOne(query, entry["title"])
34
+ if ratio_entry and ratio_entry[1]>80:
35
+ results[title] = [entry]
36
+
37
+
38
+ return results