r3hab commited on
Commit
40b057d
·
verified ·
1 Parent(s): c18e184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -1
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import os
3
  import time
4
  import uuid
@@ -7,6 +6,60 @@ import libtorrent as lt
7
  from fastapi import FastAPI, HTTPException
8
  from fastapi.responses import StreamingResponse
9
  import magic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  app = FastAPI()
12
  active_sessions = {}
 
 
1
  import os
2
  import time
3
  import uuid
 
6
  from fastapi import FastAPI, HTTPException
7
  from fastapi.responses import StreamingResponse
8
  import magic
9
+ from fastapi import Request
10
+ from fastapi.staticfiles import StaticFiles
11
+ from fastapi.templating import Jinja2Templates
12
+ from bs4 import BeautifulSoup
13
+ import requests
14
+
15
+ # Add to top of app.py
16
+ app.mount("/static", StaticFiles(directory="static"), name="static")
17
+ templates = Jinja2Templates(directory=".")
18
+
19
+ # Add search endpoint
20
+ @app.get("/search")
21
+ async def search_torrents(q: str):
22
+ try:
23
+ url = f"https://torrentgalaxy.to/torrents.php?search={q}"
24
+ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
25
+
26
+ response = requests.get(url, headers=headers)
27
+ soup = BeautifulSoup(response.text, 'html.parser')
28
+
29
+ torrents = []
30
+ for row in soup.select('div.tgxtablerow'):
31
+ try:
32
+ title = row.select_one('div:nth-child(4)').get_text(strip=True)
33
+ magnet = row.select_one('div:nth-child(5) a[href^="magnet:"]')['href']
34
+ size = row.select_one('div:nth-child(8)').get_text(strip=True)
35
+ seeds = row.select_one('div:nth-child(11) font').get_text(strip=True)
36
+
37
+ torrents.append({
38
+ 'title': title,
39
+ 'magnet': magnet,
40
+ 'size': size,
41
+ 'seeds': seeds
42
+ })
43
+ except:
44
+ continue
45
+
46
+ return torrents[:10] # Return top 10 results
47
+
48
+ except Exception as e:
49
+ raise HTTPException(status_code=500, detail=str(e))
50
+
51
+ # Add mime type endpoint
52
+ @app.get("/stream/{torrent_id}/mime")
53
+ async def get_mime_type(torrent_id: str):
54
+ if torrent_id not in active_sessions:
55
+ raise HTTPException(status_code=404, detail="Torrent session not found")
56
+
57
+ file_path = active_sessions[torrent_id]["file_path"]
58
+ mime = magic.Magic(mime=True)
59
+ return {"mime_type": mime.from_file(file_path)}
60
+
61
+
62
+
63
 
64
  app = FastAPI()
65
  active_sessions = {}