from fastapi import FastAPI, HTTPException from pydantic import BaseModel from typing import List, Tuple import requests from bs4 import BeautifulSoup app = FastAPI() class SearchQuery(BaseModel): query: str class LinkResults(BaseModel): four_links_click: List[str] direct4_links: List[str] magnet_links: List[str] def scrape_links(query: str) -> List[str]: try: url = f"https://www.full4movies.fyi/?s={query}" response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') elements = soup.find_all('h2', class_='blog-entry-title entry-title') links = [element.find('a')['href'] for element in elements] return links else: raise HTTPException(status_code=response.status_code, detail="Failed to retrieve the webpage.") except Exception as e: raise HTTPException(status_code=500, detail=str(e)) def find_links(url: str) -> Tuple[List[str], List[str], List[str]]: try: response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') links = soup.find_all('a', href=True) four_links_click = [link['href'] for link in links if '4links.click' in link['href']] direct4_links = [link['href'] for link in links if 'direct4.link' in link['href']] magnet_links = [link['href'] for link in links if link['href'].startswith('magnet:')] return four_links_click, direct4_links, magnet_links else: raise HTTPException(status_code=response.status_code, detail="Failed to retrieve the selected webpage.") except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred while scraping the selected link: {str(e)}") @app.post("/search", response_model=List[str]) def search_links(payload: SearchQuery): links = scrape_links(payload.query) if not links: raise HTTPException(status_code=404, detail="No links found.") return links @app.get("/details", response_model=LinkResults) def get_details(url: str): four_links_click, direct4_links, magnet_links = find_links(url) return LinkResults( four_links_click=four_links_click, direct4_links=direct4_links, magnet_links=magnet_links )