Skip to content

Commit 23052dc

Browse files
authored
Merge pull request #45 from sirstudly/add_magnetdl_scraper
Adds magnetDL scraper support
2 parents 0eacb2c + aceb131 commit 23052dc

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

scraper/services/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
from scraper.services import thepiratebay
1717
from scraper.services import torrentgalaxy
1818
from scraper.services import yts
19-
from scraper.services import limetorrents
19+
from scraper.services import magnetdl
2020

2121
#define subclass method
2222
def __subclasses__():
23-
return [rarbg,rarbgv2,x1337,jackett,prowlarr,orionoid,nyaa,torrentio,zilean,torbox,mediafusion,comet,eztv,thepiratebay,torrentgalaxy,yts,limetorrents]
23+
return [rarbg,rarbgv2,x1337,jackett,prowlarr,orionoid,nyaa,torrentio,zilean,torbox,mediafusion,comet,eztv,thepiratebay,torrentgalaxy,yts,limetorrents,magnetdl]
2424

2525
active = ['torrentio']
2626
overwrite = []

scraper/services/magnetdl.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import urllib.request
2+
import urllib.parse
3+
import json
4+
from ui.ui_print import *
5+
import releases
6+
7+
name = "magnetDL"
8+
session = urllib.request.build_opener()
9+
10+
11+
def setup(cls, new=False):
12+
from scraper.services import setup
13+
setup(cls, new)
14+
15+
16+
def scrape(query, altquery):
17+
from scraper.services import active
18+
scraped_releases = []
19+
if 'magnetDL' in active:
20+
ui_print("[magnetDL] using extended query: " + query, ui_settings.debug)
21+
22+
headers = {'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'}
23+
24+
url = 'https://magnetdl.hair/api.php?url=/q.php?q=' + urllib.parse.quote(query) + '&cat=0'
25+
26+
response = None
27+
try:
28+
ui_print("[magnetDL] Sending GET request to 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', errors='ignore')
35+
response_json = json.loads(content)
36+
torrents = response_json if isinstance(response_json, list) else []
37+
38+
if torrents and (torrents[0].get('name') == "No results returned" or 'total_found' in torrents[0]):
39+
ui_print(f"[magnetDL] No torrents found", ui_settings.debug)
40+
else:
41+
ui_print(f"[magnetDL] Found {len(torrents)} torrent(s)", ui_settings.debug)
42+
43+
for torrent in torrents:
44+
title = torrent.get('name', 'Unknown Title')
45+
title = regex.sub(r'[^\w\s\.\-]', '', title)
46+
title = title.replace(" ", '.')
47+
title = regex.sub(r'\.+', ".", title)
48+
49+
if regex.match(r'(' + altquery.replace('.', r'\.').replace(r"\.*", ".*") + ')', title, regex.I):
50+
info_hash = torrent.get('info_hash', '')
51+
download = f"magnet:?xt=urn:btih:{info_hash}&dn={urllib.parse.quote(title)}"
52+
size_bytes = int(torrent.get('size', 0))
53+
size = size_bytes / (1024 * 1024 * 1024) # Convert to GB
54+
seeders = int(torrent.get('seeders', 0))
55+
56+
scraped_releases.append(releases.release('[magnetDL]', 'torrent', title, [], size, [download], seeders=seeders))
57+
ui_print(f"[magnetDL] Scraped release: title={title}, size={size:.2f} GB, seeders={seeders}", ui_settings.debug)
58+
else:
59+
ui_print("[magnetDL] Failed to retrieve the page. Status code: " + str(status_code), ui_settings.debug)
60+
except Exception as e:
61+
if hasattr(response, "status_code") and not str(response.status_code).startswith("2"):
62+
ui_print('[magnetDL] error ' + str(response.status_code) + ': magnetDL is temporarily not reachable')
63+
else:
64+
ui_print('[magnetDL] error: unknown error. turn on debug printing for more information.')
65+
response = None
66+
ui_print('[magnetDL] error: exception: ' + str(e), ui_settings.debug)
67+
68+
return scraped_releases

0 commit comments

Comments
 (0)