File size: 1,303 Bytes
86dbab6
 
dd8aa41
86dbab6
 
ddfbfda
dd8aa41
ddfbfda
86dbab6
 
 
 
 
 
 
 
ddfbfda
 
86dbab6
 
dd8aa41
86dbab6
ddfbfda
86dbab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException, Query
from typing import List, Dict
import json
from fuzzywuzzy import process  # Install fuzzywuzzy: pip install fuzzywuzzy


app = FastAPI()

# Load data from the JSON file.  (Crucial: Handle potential file errors)
try:
    with open("converted_data.json", "r") as f:
        data = json.load(f)
except FileNotFoundError:
    raise HTTPException(status_code=500, detail="File 'converted_data.json' not found.")
except json.JSONDecodeError as e:
    raise HTTPException(status_code=500, detail=f"Error decoding JSON: {e}")


@app.get("/search_links/")
async def search_links(query: str = Query(..., description="Search query")):
    """
    Searches the movie titles and returns links matching the query using fuzzy search.
    """
    results = {}
    for title, links in data.items():
        ratio = process.extractOne(query, title)
        if ratio[1] > 80:  # Adjust threshold as needed
            results[title] = links
        else:
             # Use extract on links in each entry
             for entry in links:
                if entry:
                    ratio_entry = process.extractOne(query, entry["title"])
                    if ratio_entry and ratio_entry[1]>80:
                         results[title] = [entry]

    
    return results