-
Notifications
You must be signed in to change notification settings - Fork 0
/
iptorrents.py
127 lines (101 loc) · 4.2 KB
/
iptorrents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# VERSION: 1.00
# AUTHORS: la55u
# LICENSING INFORMATION: you do whatever the fuck you want with it
import re
import sys
import os
import logging
import tempfile
try:
from urllib.request import Request, urlopen
except ImportError:
from urllib2 import Request, urlopen
try:
from urllib import urlencode, quote, unquote
from urllib2 import URLError, HTTPError
except ImportError:
from urllib.parse import urlencode, quote, unquote
from urllib.error import URLError, HTTPError
debug = False
logger = logging.getLogger()
if debug:
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
else:
from novaprinter import prettyPrinter
class iptorrents(object):
url = 'https://iptorrents.eu'
name = 'IPTorrents'
############# CHANGE THESE VALUES ONLY #################
### HOW TO: https://github.com/la55u/qBit-IPT-plugin ###
cookies = { #
'uid': '1234567', #
'pass': '123very456long789randomstring012' #
} #
########################################################
# Which search categories are supported by this search engine and their corresponding id
# Possible categories are ('all', 'movies', 'tv', 'music', 'games', 'anime', 'software', 'pictures', 'books')
supported_categories = {
'all': '0',
'movies': '72',
'tv': '73',
'music': '75',
'games': '74',
'anime': '60',
'software': '1=&86=&95=&69=&58',
'books': '35=&92'
}
def headers(self):
return {'Cookie': ";".join(["%s=%s" % item for item in self.cookies.items()])}
def __init__(self):
pass
def download_torrent(self, dl_url):
"""Downloads the torrent file into tmp directory"""
logging.info('Attempting to download torrent')
full_url = self.url + quote(dl_url) # for escaping spaces
req = Request(full_url, headers=self.headers())
file, path = tempfile.mkstemp()
file = os.fdopen(file, "wb")
file.write(urlopen(req).read())
file.close()
print(path, full_url)
logging.info('Download complete.')
# DO NOT CHANGE the name and parameters of this function
# This function will be the one called by nova2.py
def search(self, what, cat='all'):
"""Searches for what on the tracker and parses each result with regular expressions"""
# what is a string with the search tokens, already escaped (e.g. "Ubuntu+Linux")
# cat is the name of a search category in ('all', 'movies', 'tv', 'music', 'games', 'anime', 'software', 'pictures', 'books')
logging.info("Searching for {}...".format(unquote(what)))
try:
req = Request(
'https://iptorrents.eu/t?q=%s' % (what),
headers=self.headers()
)
response = urlopen(req)
# Only continue if response status is OK.
if response.getcode() != 200:
raise HTTPError(response.geturl(), response.getcode(), "HTTP request to {} failed with status: {}".format(self.url, response.getcode()), response.info(), None)
except (URLError, HTTPError) as e:
logging.error(e)
raise e
# Decoding raw html data
data = response.read().decode()
# one regex for extracting every data of a torrent
regex = re.compile(r"""
(?P<desc_link>\/details.php\?id=\d+)"> # description link
(?P<name>.*?)<\/a>.*? # torrent title
(?P<link>\/download.php\/\d+\/.*?\.torrent).*? # download link
<td.*?>(?P<size>\d+\.?\d*\s(KB|MB|GB|TB){1})<td.*?>.*? # size
t_seeders">(?P<seeds>\d+).*? # seeders count
t_leechers">(?P<leech>\d+) # leechers count
""", re.VERBOSE) # ignore multiline formatting
for match in regex.finditer(data):
torrent = match.groupdict()
#torrent['link'] = self.url + torrent['link']
torrent['desc_link'] = self.url + torrent['desc_link']
torrent['engine_url'] = self.url
prettyPrinter(torrent)
logging.info('Search complete.')
if __name__ == '__main__':
ipt = iptorrents()
#ipt.search('random')