Skip to content

Commit 9d35b54

Browse files
authored
Merge pull request #35 from sirstudly/thepiratebay_scraper_support
adds thepiratebay scraper support
2 parents 7d95cec + 2d11b50 commit 9d35b54

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

scraper/services/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from scraper.services import mediafusion
1313
from scraper.services import comet
1414
from scraper.services import eztv
15+
from scraper.services import thepiratebay
1516

1617
#define subclass method
1718
def __subclasses__():
18-
return [rarbg,x1337,jackett,prowlarr,orionoid,nyaa,torrentio,zilean,torbox,mediafusion,comet,eztv]
19+
return [rarbg,x1337,jackett,prowlarr,orionoid,nyaa,torrentio,zilean,torbox,mediafusion,comet,eztv,thepiratebay]
1920

2021
active = ['torrentio']
2122
overwrite = []

scraper/services/thepiratebay.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import urllib.request
2+
import urllib.parse
3+
from ui.ui_print import *
4+
import releases
5+
import re
6+
7+
name = "thepiratebay"
8+
base_url = "https://apibay.org"
9+
session = urllib.request.build_opener()
10+
11+
12+
def setup(cls, new=False):
13+
from scraper.services import setup
14+
setup(cls, new)
15+
16+
17+
def scrape(query, altquery):
18+
from scraper.services import active
19+
20+
scraped_releases = []
21+
if 'thepiratebay' in active:
22+
q = query.replace('.?', '').replace("'", "").replace("’", "").replace('.', ' ').strip(".").strip(" ")
23+
ui_print("[thepiratebay] using extended query: " + q, ui_settings.debug)
24+
headers = {
25+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'}
26+
url = base_url + '/q.php?q=' + urllib.parse.quote(q, safe=':/')
27+
try:
28+
ui_print("[thepiratebay] Sending GET request to API URL: " + url, ui_settings.debug)
29+
request = urllib.request.Request(url, headers=headers)
30+
response = session.open(request)
31+
status_code = response.getcode()
32+
33+
if status_code == 200:
34+
content = response.read().decode('utf-8')
35+
response_json = json.loads(content)
36+
torrents = response_json if isinstance(response_json, list) else []
37+
38+
# Check for 0 results
39+
if torrents and (torrents[0].get('name') == "No results returned" or 'total_found' in torrents[0]):
40+
ui_print("[thepiratebay] No torrents found", ui_settings.debug)
41+
else:
42+
ui_print(f"[thepiratebay] Found {len(torrents)} torrent(s)", ui_settings.debug)
43+
44+
for torrent in torrents:
45+
title = torrent.get('name')
46+
title = re.sub(r'[^\w\s\.\-]', '', title)
47+
download = 'magnet:?xt=urn:btih:' + torrent.get('info_hash')
48+
size_bytes = int(torrent.get('size', 0))
49+
size = size_bytes / (1024 * 1024 * 1024)
50+
seeders = int(torrent.get('seeders', 0))
51+
52+
if regex.match(r'(' + altquery.replace('.', '\.').replace("\.*", ".*") + ')', title, regex.I):
53+
scraped_releases += [releases.release('[thepiratebay]', 'torrent', title, [], size, [download], seeders=seeders)]
54+
ui_print(f"[thepiratebay] Scraped release: title={title}, size={size:.2f} GB, seeders={seeders}", ui_settings.debug)
55+
else:
56+
ui_print("[thepiratebay] Failed to retrieve data from API. Status code: " + str(status_code), ui_settings.debug)
57+
58+
except Exception as e:
59+
ui_print('[thepiratebay] error: exception: ' + str(e), ui_settings.debug)
60+
return scraped_releases

0 commit comments

Comments
 (0)