| from fastapi import FastAPI, HTTPException |
| import requests |
| from bs4 import BeautifulSoup |
| from urllib.parse import unquote |
|
|
| app = FastAPI() |
|
|
| @app.get("/search/{query}") |
| async def search(query: str): |
| |
| url = f'http://thepiratebay7.com/search/{query}' |
|
|
| |
| response = requests.get(url) |
|
|
| |
| if response.status_code == 200: |
| |
| soup = BeautifulSoup(response.content, 'html.parser') |
|
|
| |
| td_elements = soup.find_all('td') |
|
|
| results = [] |
| |
| for td in td_elements: |
| |
| title = 'N/A' |
| link = 'N/A' |
| |
| |
| title_div = td.find('div', class_='detName') |
| if title_div: |
| title_link = title_div.find('a') |
| title = title_link.text if title_link else 'N/A' |
| link = title_link['href'] if title_link else 'N/A' |
| |
| magnet_link = td.find('a', href=lambda href: href and href.startswith('magnet:')) |
| magnet = magnet_link['href'] if magnet_link else 'N/A' |
|
|
| |
| if magnet != 'N/A': |
| magnet = unquote(magnet) |
|
|
| |
| results.append({'File Name': title, 'Magnet Link': magnet}) |
|
|
| return {"results": results} |
| else: |
| raise HTTPException(status_code=response.status_code, detail="Failed to retrieve data") |
|
|