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