|
|
|
@ -1,11 +1,10 @@
@@ -1,11 +1,10 @@
|
|
|
|
|
# VERSION: 2.0 |
|
|
|
|
# VERSION: 2.1 |
|
|
|
|
# AUTHORS: imDMG [imdmgg@gmail.com] |
|
|
|
|
|
|
|
|
|
# NoNaMe-Club search engine plugin for qBittorrent |
|
|
|
|
|
|
|
|
|
import json |
|
|
|
|
import logging |
|
|
|
|
import math |
|
|
|
|
import os |
|
|
|
|
import re |
|
|
|
|
import tempfile |
|
|
|
@ -15,23 +14,13 @@ import time
@@ -15,23 +14,13 @@ import time
|
|
|
|
|
from urllib.request import build_opener, HTTPCookieProcessor, ProxyHandler |
|
|
|
|
from urllib.parse import urlencode # , parse_qs |
|
|
|
|
from urllib.error import URLError, HTTPError |
|
|
|
|
from http.cookiejar import Cookie, CookieJar |
|
|
|
|
from http.cookiejar import Cookie, MozillaCookieJar |
|
|
|
|
from novaprinter import prettyPrinter |
|
|
|
|
|
|
|
|
|
# setup logging into qBittorrent/logs |
|
|
|
|
logging.basicConfig(level=logging.INFO, |
|
|
|
|
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', |
|
|
|
|
datefmt='%m-%d %H:%M', |
|
|
|
|
filename=os.path.abspath(os.path.join(os.path.dirname(__file__), '../../logs', 'nnmclub.log')), |
|
|
|
|
filemode='w') |
|
|
|
|
|
|
|
|
|
# benchmark |
|
|
|
|
start_time = time.time() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class nnmclub(object): |
|
|
|
|
name = 'NoNaMe-Club' |
|
|
|
|
url = 'https://nnm-club.me/forum/' |
|
|
|
|
url = 'https://nnmclub.to/forum/' |
|
|
|
|
supported_categories = {'all': '-1', |
|
|
|
|
'movies': '14', |
|
|
|
|
'tv': '27', |
|
|
|
@ -40,30 +29,45 @@ class nnmclub(object):
@@ -40,30 +29,45 @@ class nnmclub(object):
|
|
|
|
|
'anime': '24', |
|
|
|
|
'software': '21'} |
|
|
|
|
|
|
|
|
|
# getting config from kinozal.json |
|
|
|
|
config = None |
|
|
|
|
# default config for nnmclub.json |
|
|
|
|
config = { |
|
|
|
|
"version": 2, |
|
|
|
|
"torrentDate": False, |
|
|
|
|
"username": "USERNAME", |
|
|
|
|
"password": "PASSWORD", |
|
|
|
|
"proxy": False, |
|
|
|
|
"proxies": { |
|
|
|
|
"http": "", |
|
|
|
|
"https": "" |
|
|
|
|
}, |
|
|
|
|
"ua": "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
# setup logging into qBittorrent/logs |
|
|
|
|
logging.basicConfig(handlers=[logging.FileHandler(self.path_to('../../logs', 'nnmclub.log'), 'w', 'utf-8')], |
|
|
|
|
level=logging.DEBUG, |
|
|
|
|
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', |
|
|
|
|
datefmt='%m-%d %H:%M') |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
# try to load user data from file |
|
|
|
|
with open(os.path.abspath(os.path.join(os.path.dirname(__file__), 'nnmclub.json'))) as f: |
|
|
|
|
config: dict = json.load(f) |
|
|
|
|
with open(self.path_to('nnmclub.json'), 'r+') as f: |
|
|
|
|
config = json.load(f) |
|
|
|
|
if "version" not in config.keys(): |
|
|
|
|
config.update({"version": 2, "torrentDate": False}) |
|
|
|
|
f.seek(0) |
|
|
|
|
f.write(json.dumps(config, indent=4, sort_keys=False)) |
|
|
|
|
f.truncate() |
|
|
|
|
self.config = config |
|
|
|
|
except OSError as e: |
|
|
|
|
# file not found |
|
|
|
|
logging.error(e) |
|
|
|
|
raise e |
|
|
|
|
# if file doesn't exist, we'll create it |
|
|
|
|
with open(self.path_to('nnmclub.json'), 'w') as f: |
|
|
|
|
f.write(json.dumps(self.config, indent=4, sort_keys=False)) |
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
# establish connection |
|
|
|
|
# |
|
|
|
|
# make cookie |
|
|
|
|
cj = CookieJar() |
|
|
|
|
# if we wanna use https we mast add ssl=enable_ssl to cookie |
|
|
|
|
c = Cookie(0, 'ssl', "enable_ssl", None, False, '.nnm-club.me', |
|
|
|
|
True, False, '/', True, False, None, 'ParserCookie', None, None, None) |
|
|
|
|
cj.set_cookie(c) |
|
|
|
|
self.session = build_opener(HTTPCookieProcessor(cj)) |
|
|
|
|
|
|
|
|
|
# avoid endless waiting |
|
|
|
|
self.blocked = False |
|
|
|
|
self.session = build_opener() |
|
|
|
|
|
|
|
|
|
# add proxy handler if needed |
|
|
|
|
if self.config['proxy'] and any(self.config['proxies'].keys()): |
|
|
|
@ -73,6 +77,32 @@ class nnmclub(object):
@@ -73,6 +77,32 @@ class nnmclub(object):
|
|
|
|
|
self.session.addheaders.pop() |
|
|
|
|
self.session.addheaders.append(('User-Agent', self.config['ua'])) |
|
|
|
|
|
|
|
|
|
# avoid endless waiting |
|
|
|
|
self.blocked = False |
|
|
|
|
|
|
|
|
|
mcj = MozillaCookieJar() |
|
|
|
|
cookie_file = self.path_to('nnmclub.cookie') |
|
|
|
|
# load local cookies |
|
|
|
|
if os.path.isfile(cookie_file): |
|
|
|
|
mcj.load(cookie_file, ignore_discard=True) |
|
|
|
|
if 'phpbb2mysql_4_sid' in [cookie.name for cookie in mcj]: |
|
|
|
|
# if cookie.expires < int(time.time()) |
|
|
|
|
logging.info("Local cookies is loaded") |
|
|
|
|
self.session.add_handler(HTTPCookieProcessor(mcj)) |
|
|
|
|
else: |
|
|
|
|
logging.info("Local cookies expired or bad") |
|
|
|
|
logging.debug(f"That we have: {[cookie for cookie in mcj]}") |
|
|
|
|
mcj.clear() |
|
|
|
|
self.login(mcj, cookie_file) |
|
|
|
|
else: |
|
|
|
|
self.login(mcj, cookie_file) |
|
|
|
|
|
|
|
|
|
def login(self, mcj, cookie_file): |
|
|
|
|
# if we wanna use https we mast add ssl=enable_ssl to cookie |
|
|
|
|
mcj.set_cookie(Cookie(0, 'ssl', "enable_ssl", None, False, '.nnmclub.to', True, |
|
|
|
|
False, '/', True, False, None, 'ParserCookie', None, None, None)) |
|
|
|
|
self.session.add_handler(HTTPCookieProcessor(mcj)) |
|
|
|
|
|
|
|
|
|
response = self._catch_error_request(self.url + 'login.php') |
|
|
|
|
if not self.blocked: |
|
|
|
|
code = re.search(r'code"\svalue="(.+?)"', response.read().decode('cp1251'))[1] |
|
|
|
@ -85,20 +115,23 @@ class nnmclub(object):
@@ -85,20 +115,23 @@ class nnmclub(object):
|
|
|
|
|
data_encoded = urlencode({k: v.encode('cp1251') for k, v in form_data.items()}).encode() |
|
|
|
|
|
|
|
|
|
self._catch_error_request(self.url + 'login.php', data_encoded) |
|
|
|
|
|
|
|
|
|
if 'phpbb2mysql_4_sid' not in [cookie.name for cookie in cj]: |
|
|
|
|
if 'phpbb2mysql_4_sid' not in [cookie.name for cookie in mcj]: |
|
|
|
|
logging.warning("we not authorized, please check your credentials") |
|
|
|
|
else: |
|
|
|
|
mcj.save(cookie_file, ignore_discard=True, ignore_expires=True) |
|
|
|
|
logging.info('We successfully authorized') |
|
|
|
|
|
|
|
|
|
def draw(self, html: str): |
|
|
|
|
torrents = re.findall(r'd\stopic.+?href="(.+?)".+?<b>(.+?)</b>.+?href="(d.+?)"' |
|
|
|
|
r'.+?/u>\s(.+?)<.+?b>(\d+)</.+?b>(\d+)<', html, re.S) |
|
|
|
|
r'.+?/u>\s(.+?)<.+?b>(\d+)</.+?b>(\d+)<.+?<u>(\d+)</u>', html, re.S) |
|
|
|
|
|
|
|
|
|
for tor in torrents: |
|
|
|
|
torrent_date = "" |
|
|
|
|
if self.config['torrentDate']: |
|
|
|
|
torrent_date = f'[{time.strftime("%d.%m.%y", time.localtime(int(tor[6])))}] ' |
|
|
|
|
torrent = {"engine_url": self.url, |
|
|
|
|
"desc_link": self.url + tor[0], |
|
|
|
|
"name": tor[1], |
|
|
|
|
"name": torrent_date + tor[1], |
|
|
|
|
"link": self.url + tor[2], |
|
|
|
|
"size": tor[3].replace(',', '.'), |
|
|
|
|
"seeds": tor[4], |
|
|
|
@ -108,6 +141,9 @@ class nnmclub(object):
@@ -108,6 +141,9 @@ class nnmclub(object):
|
|
|
|
|
del torrents |
|
|
|
|
# return len(torrents) |
|
|
|
|
|
|
|
|
|
def path_to(self, *file): |
|
|
|
|
return os.path.abspath(os.path.join(os.path.dirname(__file__), *file)) |
|
|
|
|
|
|
|
|
|
def download_torrent(self, url): |
|
|
|
|
if self.blocked: |
|
|
|
|
return |
|
|
|
@ -130,7 +166,7 @@ class nnmclub(object):
@@ -130,7 +166,7 @@ class nnmclub(object):
|
|
|
|
|
response = self._catch_error_request(query) |
|
|
|
|
page = response.read().decode('cp1251') |
|
|
|
|
self.draw(page) |
|
|
|
|
total = int(re.search(r'\(max:\s(\d{1,3})\)', page)[1]) if first else -1 |
|
|
|
|
total = int(re.search(r'(\d{1,3})\s\(max:', page)[1]) if first else -1 |
|
|
|
|
|
|
|
|
|
return total |
|
|
|
|
|
|
|
|
@ -138,15 +174,15 @@ class nnmclub(object):
@@ -138,15 +174,15 @@ class nnmclub(object):
|
|
|
|
|
if self.blocked: |
|
|
|
|
return |
|
|
|
|
c = self.supported_categories[cat] |
|
|
|
|
query = '{}tracker.php?nm={}&{}'.format(self.url, what.replace(" ", "+"), "f=-1" if c == '-1' else "c=" + c) |
|
|
|
|
query = f'{self.url}tracker.php?nm={what.replace(" ", "+")}&{"f=-1" if c == "-1" else "c=" + c}' |
|
|
|
|
|
|
|
|
|
# make first request (maybe it enough) |
|
|
|
|
total = self.searching(query, True) |
|
|
|
|
# do async requests |
|
|
|
|
if total > 50: |
|
|
|
|
tasks = [] |
|
|
|
|
for x in range(1, math.ceil(total / 50)): |
|
|
|
|
task = threading.Thread(target=self.searching, args=(query + "&start={}".format(x * 50),)) |
|
|
|
|
for x in range(1, -(-total//50)): |
|
|
|
|
task = threading.Thread(target=self.searching, args=(query + f"&start={x * 50}",)) |
|
|
|
|
tasks.append(task) |
|
|
|
|
task.start() |
|
|
|
|
|
|
|
|
@ -155,11 +191,11 @@ class nnmclub(object):
@@ -155,11 +191,11 @@ class nnmclub(object):
|
|
|
|
|
task.join() |
|
|
|
|
del tasks |
|
|
|
|
|
|
|
|
|
logging.debug("--- {} seconds ---".format(time.time() - start_time)) |
|
|
|
|
logging.info("Found torrents: {}".format(total)) |
|
|
|
|
logging.debug(f"--- {time.time() - start_time} seconds ---") |
|
|
|
|
logging.info(f"Found torrents: {total}") |
|
|
|
|
|
|
|
|
|
def _catch_error_request(self, url='', data=None): |
|
|
|
|
url = url if url else self.url |
|
|
|
|
url = url or self.url |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
response = self.session.open(url, data) |
|
|
|
@ -167,7 +203,7 @@ class nnmclub(object):
@@ -167,7 +203,7 @@ class nnmclub(object):
|
|
|
|
|
if response.getcode() != 200: |
|
|
|
|
logging.error('Unable connect') |
|
|
|
|
raise HTTPError(response.geturl(), response.getcode(), |
|
|
|
|
"HTTP request to {} failed with status: {}".format(url, response.getcode()), |
|
|
|
|
f"HTTP request to {url} failed with status: {response.getcode()}", |
|
|
|
|
response.info(), None) |
|
|
|
|
except (URLError, HTTPError) as e: |
|
|
|
|
logging.error(e) |
|
|
|
@ -176,12 +212,17 @@ class nnmclub(object):
@@ -176,12 +212,17 @@ class nnmclub(object):
|
|
|
|
|
# checking that tracker is'nt blocked |
|
|
|
|
self.blocked = False |
|
|
|
|
if self.url not in response.geturl(): |
|
|
|
|
logging.warning("{} is blocked. Try proxy or another proxy".format(self.url)) |
|
|
|
|
print(response.geturl()) |
|
|
|
|
logging.warning(f"{self.url} is blocked. Try proxy or another proxy") |
|
|
|
|
self.blocked = True |
|
|
|
|
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
nnmclub_se = nnmclub() |
|
|
|
|
nnmclub_se.search('supernatural') |
|
|
|
|
# benchmark start |
|
|
|
|
start_time = time.time() |
|
|
|
|
# nnmclub_se = nnmclub() |
|
|
|
|
# nnmclub_se.search('bird') |
|
|
|
|
print(f"--- {time.time() - start_time} seconds ---") |
|
|
|
|
# benchmark end |
|
|
|
|