|
|
|
@ -1,4 +1,4 @@
@@ -1,4 +1,4 @@
|
|
|
|
|
# VERSION: 1.2 |
|
|
|
|
# VERSION: 1.3 |
|
|
|
|
# AUTHORS: imDMG [imdmgg@gmail.com] |
|
|
|
|
|
|
|
|
|
# rutracker.org search engine plugin for qBittorrent |
|
|
|
@ -44,12 +44,14 @@ def rng(t):
@@ -44,12 +44,14 @@ def rng(t):
|
|
|
|
|
return range(50, -(-t // 50) * 50, 50) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PATTERNS = (r'(\d{1,3})\s<span', |
|
|
|
|
r'bold"\shref="(viewtopic\.php\?t=\d+)">(.+?)</a.+?(dl\.php\?t=\d+)' |
|
|
|
|
r'">(.+?)\s&.+?data-ts_text="(.+?)">.+?Личи">(\d+)</.+?data-ts_' |
|
|
|
|
r'text="(\d+)"', '%s/tracker.php?nm=%s&c=%s', "%s&start=%s") |
|
|
|
|
RE_TORRENTS = re.compile( |
|
|
|
|
r'data-topic_id="(\d+?)".+?">(.+?)</a.+?tor-size"\sdata-ts_text="(\d+?)">' |
|
|
|
|
r'.+?data-ts_text="([-0-9]+?)">.+?Личи">(\d+?)</.+?data-ts_text="(\d+?)">', |
|
|
|
|
re.S) |
|
|
|
|
RE_RESULTS = re.compile(r'Результатов\sпоиска:\s(\d{1,3})\s<span', re.S) |
|
|
|
|
PATTERNS = ('%s/tracker.php?nm=%s&c=%s', "%s&start=%s") |
|
|
|
|
|
|
|
|
|
FILENAME = __file__[__file__.rfind('/') + 1:-3] |
|
|
|
|
FILENAME = os.path.basename(__file__)[:-3] |
|
|
|
|
FILE_J, FILE_C = [path_to(FILENAME + fl) for fl in ['.json', '.cookie']] |
|
|
|
|
|
|
|
|
|
# base64 encoded image |
|
|
|
@ -79,9 +81,10 @@ ICON = ("AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABMLAAATCw"
@@ -79,9 +81,10 @@ ICON = ("AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAABMLAAATCw"
|
|
|
|
|
# setup logging |
|
|
|
|
logging.basicConfig( |
|
|
|
|
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", |
|
|
|
|
datefmt="%m-%d %H:%M") |
|
|
|
|
datefmt="%m-%d %H:%M", |
|
|
|
|
level=logging.DEBUG) |
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
logger.setLevel(logging.DEBUG) |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
# try to load user data from file |
|
|
|
@ -102,6 +105,8 @@ except OSError as e:
@@ -102,6 +105,8 @@ except OSError as e:
|
|
|
|
|
class rutracker: |
|
|
|
|
name = 'Rutracker' |
|
|
|
|
url = 'https://rutracker.org/forum/' |
|
|
|
|
url_dl = url + 'dl.php?t=' |
|
|
|
|
url_login = url + 'login.php' |
|
|
|
|
supported_categories = {'all': '-1'} |
|
|
|
|
|
|
|
|
|
# 'movies': '2', |
|
|
|
@ -127,8 +132,7 @@ class rutracker:
@@ -127,8 +132,7 @@ class rutracker:
|
|
|
|
|
self.error = "Proxy enabled, but not set!" |
|
|
|
|
|
|
|
|
|
# change user-agent |
|
|
|
|
self.session.addheaders.pop() |
|
|
|
|
self.session.addheaders.append(('User-Agent', config['ua'])) |
|
|
|
|
self.session.addheaders = [('User-Agent', config['ua'])] |
|
|
|
|
|
|
|
|
|
# load local cookies |
|
|
|
|
mcj = MozillaCookieJar() |
|
|
|
@ -149,18 +153,18 @@ class rutracker:
@@ -149,18 +153,18 @@ class rutracker:
|
|
|
|
|
def search(self, what, cat='all'): |
|
|
|
|
if self.error: |
|
|
|
|
self.pretty_error(what) |
|
|
|
|
return |
|
|
|
|
query = PATTERNS[2] % (self.url, what.replace(" ", "+"), |
|
|
|
|
return None |
|
|
|
|
query = PATTERNS[0] % (self.url, what.replace(" ", "+"), |
|
|
|
|
self.supported_categories[cat]) |
|
|
|
|
|
|
|
|
|
# make first request (maybe it enough) |
|
|
|
|
t0, total = time.time(), self.searching(query, True) |
|
|
|
|
if self.error: |
|
|
|
|
self.pretty_error(what) |
|
|
|
|
return |
|
|
|
|
return None |
|
|
|
|
# do async requests |
|
|
|
|
if total > 50: |
|
|
|
|
qrs = [PATTERNS[3] % (query, x) for x in rng(total)] |
|
|
|
|
qrs = [PATTERNS[1] % (query, x) for x in rng(total)] |
|
|
|
|
with ThreadPoolExecutor(len(qrs)) as executor: |
|
|
|
|
executor.map(self.searching, qrs, timeout=30) |
|
|
|
|
|
|
|
|
@ -172,13 +176,13 @@ class rutracker:
@@ -172,13 +176,13 @@ class rutracker:
|
|
|
|
|
response = self._catch_error_request(url) |
|
|
|
|
if self.error: |
|
|
|
|
self.pretty_error(url) |
|
|
|
|
return |
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|
# Create a torrent file |
|
|
|
|
file, path = tempfile.mkstemp('.torrent') |
|
|
|
|
with os.fdopen(file, "wb") as fd: |
|
|
|
|
# Write it to a file |
|
|
|
|
fd.write(response.read()) |
|
|
|
|
fd.write(response) |
|
|
|
|
|
|
|
|
|
# return file path |
|
|
|
|
logger.debug(path + " " + url) |
|
|
|
@ -186,24 +190,24 @@ class rutracker:
@@ -186,24 +190,24 @@ class rutracker:
|
|
|
|
|
|
|
|
|
|
def login(self, mcj): |
|
|
|
|
if self.error: |
|
|
|
|
return |
|
|
|
|
# if we wanna use https we mast add ssl=enable_ssl to cookie |
|
|
|
|
mcj.set_cookie(Cookie(0, 'ssl', "enable_ssl", None, False, |
|
|
|
|
'.rutracker.org', True, False, '/', True, |
|
|
|
|
False, None, 'ParserCookie', None, None, None)) |
|
|
|
|
return None |
|
|
|
|
# if we wanna use https we mast add bb_ssl=1 to cookie |
|
|
|
|
mcj.set_cookie(Cookie(0, "bb_ssl", "1", None, False, ".rutracker.org", |
|
|
|
|
True, True, "/forum/", True, True, |
|
|
|
|
None, False, None, None, {})) |
|
|
|
|
self.session.add_handler(HTTPCookieProcessor(mcj)) |
|
|
|
|
|
|
|
|
|
form_data = {"login_username": config['username'], |
|
|
|
|
"login_password": config['password'], |
|
|
|
|
"login": "вход"} |
|
|
|
|
"login": "Вход"} |
|
|
|
|
logger.debug(f"Login. Data before: {form_data}") |
|
|
|
|
# so we first encode vals to cp1251 then do default decode whole string |
|
|
|
|
data_encoded = urlencode( |
|
|
|
|
{k: v.encode('cp1251') for k, v in form_data.items()}).encode() |
|
|
|
|
logger.debug(f"Login. Data after: {data_encoded}") |
|
|
|
|
self._catch_error_request(self.url + 'login.php', data_encoded) |
|
|
|
|
self._catch_error_request(self.url_login, data_encoded) |
|
|
|
|
if self.error: |
|
|
|
|
return |
|
|
|
|
return None |
|
|
|
|
logger.debug(f"That we have: {[cookie for cookie in mcj]}") |
|
|
|
|
if 'bb_session' in [cookie.name for cookie in mcj]: |
|
|
|
|
mcj.save(FILE_C, ignore_discard=True, ignore_expires=True) |
|
|
|
@ -216,38 +220,54 @@ class rutracker:
@@ -216,38 +220,54 @@ class rutracker:
|
|
|
|
|
response = self._catch_error_request(query) |
|
|
|
|
if not response: |
|
|
|
|
return None |
|
|
|
|
page = response.read().decode('cp1251') |
|
|
|
|
page, torrents_found = response.decode('cp1251'), -1 |
|
|
|
|
if first: |
|
|
|
|
if "log-out-icon" not in page: |
|
|
|
|
logger.debug("Looks like we lost session id, lets login") |
|
|
|
|
self.login(MozillaCookieJar()) |
|
|
|
|
if self.error: |
|
|
|
|
return None |
|
|
|
|
# retry request because guests cant search |
|
|
|
|
response = self._catch_error_request(query) |
|
|
|
|
if not response: |
|
|
|
|
return None |
|
|
|
|
page = response.decode('cp1251') |
|
|
|
|
# firstly we check if there is a result |
|
|
|
|
torrents_found = int(RE_RESULTS.search(page)[1]) |
|
|
|
|
if not torrents_found: |
|
|
|
|
return 0 |
|
|
|
|
self.draw(page) |
|
|
|
|
|
|
|
|
|
return int(re.search(PATTERNS[0], page)[1]) if first else -1 |
|
|
|
|
return torrents_found |
|
|
|
|
|
|
|
|
|
def draw(self, html: str): |
|
|
|
|
torrents = re.findall(PATTERNS[1], html, re.S) |
|
|
|
|
torrents = RE_TORRENTS.findall(html) |
|
|
|
|
for tor in torrents: |
|
|
|
|
local = time.strftime("%y.%m.%d", time.localtime(int(tor[6]))) |
|
|
|
|
local = time.strftime("%y.%m.%d", time.localtime(int(tor[5]))) |
|
|
|
|
torrent_date = f"[{local}] " if config['torrentDate'] else "" |
|
|
|
|
|
|
|
|
|
prettyPrinter({ |
|
|
|
|
"engine_url": self.url, |
|
|
|
|
"desc_link": self.url + tor[0], |
|
|
|
|
"desc_link": self.url + "viewtopic.php?t=" + tor[0], |
|
|
|
|
"name": torrent_date + unescape(tor[1]), |
|
|
|
|
"link": self.url + tor[2], |
|
|
|
|
"size": unescape(tor[3]), |
|
|
|
|
"seeds": tor[4] if tor[4].isdigit() else '0', |
|
|
|
|
"leech": tor[5] |
|
|
|
|
"link": self.url_dl + tor[0], |
|
|
|
|
"size": tor[2], |
|
|
|
|
"seeds": max(0, int(tor[3])), |
|
|
|
|
"leech": tor[4] |
|
|
|
|
}) |
|
|
|
|
del torrents |
|
|
|
|
|
|
|
|
|
def _catch_error_request(self, url='', data=None, retrieve=False): |
|
|
|
|
def _catch_error_request(self, url=None, data=None, repeated=False): |
|
|
|
|
url = url or self.url |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
response = self.session.open(url, data, 5) |
|
|
|
|
# checking that tracker is'nt blocked |
|
|
|
|
if not response.geturl().startswith(self.url): |
|
|
|
|
with self.session.open(url, data, 5) as r: |
|
|
|
|
# checking that tracker isn't blocked |
|
|
|
|
if r.url.startswith((self.url, self.url_dl)): |
|
|
|
|
return r.read() |
|
|
|
|
raise URLError(f"{self.url} is blocked. Try another proxy.") |
|
|
|
|
except (socket.error, socket.timeout) as err: |
|
|
|
|
if not retrieve: |
|
|
|
|
if not repeated: |
|
|
|
|
return self._catch_error_request(url, data, True) |
|
|
|
|
logger.error(err) |
|
|
|
|
self.error = f"{self.url} is not response! Maybe it is blocked." |
|
|
|
@ -258,8 +278,6 @@ class rutracker:
@@ -258,8 +278,6 @@ class rutracker:
|
|
|
|
|
self.error = err.reason |
|
|
|
|
if hasattr(err, 'code'): |
|
|
|
|
self.error = f"Request to {url} failed with status: {err.code}" |
|
|
|
|
else: |
|
|
|
|
return response |
|
|
|
|
|
|
|
|
|
return None |
|
|
|
|
|
|
|
|
|