|
|
@ -1,4 +1,4 @@ |
|
|
|
# VERSION: 2.4 |
|
|
|
# VERSION: 2.5 |
|
|
|
# AUTHORS: imDMG [imdmgg@gmail.com] |
|
|
|
# AUTHORS: imDMG [imdmgg@gmail.com] |
|
|
|
|
|
|
|
|
|
|
|
# Kinozal.tv search engine plugin for qBittorrent |
|
|
|
# Kinozal.tv search engine plugin for qBittorrent |
|
|
@ -7,16 +7,15 @@ import base64 |
|
|
|
import gzip |
|
|
|
import gzip |
|
|
|
import json |
|
|
|
import json |
|
|
|
import logging |
|
|
|
import logging |
|
|
|
import os |
|
|
|
|
|
|
|
import re |
|
|
|
import re |
|
|
|
import socket |
|
|
|
import socket |
|
|
|
import tempfile |
|
|
|
|
|
|
|
import time |
|
|
|
import time |
|
|
|
|
|
|
|
|
|
|
|
from concurrent.futures.thread import ThreadPoolExecutor |
|
|
|
from concurrent.futures.thread import ThreadPoolExecutor |
|
|
|
from functools import partial |
|
|
|
from functools import partial |
|
|
|
from html import unescape |
|
|
|
from html import unescape |
|
|
|
from http.cookiejar import MozillaCookieJar |
|
|
|
from http.cookiejar import MozillaCookieJar |
|
|
|
|
|
|
|
from pathlib import Path |
|
|
|
|
|
|
|
from tempfile import NamedTemporaryFile |
|
|
|
from urllib.error import URLError, HTTPError |
|
|
|
from urllib.error import URLError, HTTPError |
|
|
|
from urllib.parse import urlencode, unquote |
|
|
|
from urllib.parse import urlencode, unquote |
|
|
|
from urllib.request import build_opener, HTTPCookieProcessor, ProxyHandler |
|
|
|
from urllib.request import build_opener, HTTPCookieProcessor, ProxyHandler |
|
|
@ -25,7 +24,6 @@ from novaprinter import prettyPrinter |
|
|
|
|
|
|
|
|
|
|
|
# default config |
|
|
|
# default config |
|
|
|
config = { |
|
|
|
config = { |
|
|
|
"version": 3, |
|
|
|
|
|
|
|
"torrentDate": True, |
|
|
|
"torrentDate": True, |
|
|
|
"username": "USERNAME", |
|
|
|
"username": "USERNAME", |
|
|
|
"password": "PASSWORD", |
|
|
|
"password": "PASSWORD", |
|
|
@ -38,24 +36,26 @@ config = { |
|
|
|
"ua": "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0 " |
|
|
|
"ua": "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0 " |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FILE = Path(__file__) |
|
|
|
|
|
|
|
BASEDIR = FILE.parent.absolute() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FILENAME = FILE.name[:-3] |
|
|
|
|
|
|
|
FILE_J, FILE_C = [BASEDIR / (FILENAME + fl) for fl in ['.json', '.cookie']] |
|
|
|
|
|
|
|
|
|
|
|
def path_to(*file): |
|
|
|
PAGES = 50 |
|
|
|
return os.path.abspath(os.path.join(os.path.dirname(__file__), *file)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rng(t): |
|
|
|
def rng(t): |
|
|
|
return range(1, -(-t // 50)) |
|
|
|
return range(1, -(-t // PAGES)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RE_TORRENTS = re.compile( |
|
|
|
RE_TORRENTS = re.compile( |
|
|
|
r'nam"><a\s+?href="/(.+?)"\s+?class="r\d">(.+?)</a>.+?s\'>.+?s\'>(.+?)<.+?' |
|
|
|
r'nam"><a\s+?href="/(.+?)"\s+?class="r\d">(.+?)</a>.+?s\'>.+?s\'>(.+?)<.+?' |
|
|
|
r'sl_s\'>(\d+?)<.+?sl_p\'>(\d+?)<.+?s\'>(.+?)</td>', re.S) |
|
|
|
r'sl_s\'>(\d+?)<.+?sl_p\'>(\d+?)<.+?s\'>(.+?)</td>', re.S |
|
|
|
|
|
|
|
) |
|
|
|
RE_RESULTS = re.compile(r'</span>Найдено\s+?(\d+?)\s+?раздач', re.S) |
|
|
|
RE_RESULTS = re.compile(r'</span>Найдено\s+?(\d+?)\s+?раздач', re.S) |
|
|
|
PATTERNS = ('%sbrowse.php?s=%s&c=%s', "%s&page=%s") |
|
|
|
PATTERNS = ('%sbrowse.php?s=%s&c=%s', "%s&page=%s") |
|
|
|
|
|
|
|
|
|
|
|
FILENAME = os.path.basename(__file__)[:-3] |
|
|
|
|
|
|
|
FILE_J, FILE_C = [path_to(FILENAME + fe) for fe in ['.json', '.cookie']] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# base64 encoded image |
|
|
|
# base64 encoded image |
|
|
|
ICON = ("AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAAAAAAAAAAA" |
|
|
|
ICON = ("AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAAAAAAAAAAA" |
|
|
|
"AAAAAAAAAAAACARztMgEc7/4BHO/+ARztMAAAAAIBHO0yhd2n/gEc7/6F3af+ARztMAAAA" |
|
|
|
"AAAAAAAAAAAACARztMgEc7/4BHO/+ARztMAAAAAIBHO0yhd2n/gEc7/6F3af+ARztMAAAA" |
|
|
@ -84,33 +84,24 @@ ICON = ("AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAAAAAAAAAAA" |
|
|
|
logging.basicConfig( |
|
|
|
logging.basicConfig( |
|
|
|
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", |
|
|
|
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", |
|
|
|
datefmt="%m-%d %H:%M", |
|
|
|
datefmt="%m-%d %H:%M", |
|
|
|
level=logging.DEBUG) |
|
|
|
level=logging.DEBUG |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
try: |
|
|
|
# try to load user data from file |
|
|
|
config = json.loads(FILE_J.read_text()) |
|
|
|
with open(FILE_J, 'r+') as f: |
|
|
|
|
|
|
|
cfg = json.load(f) |
|
|
|
|
|
|
|
if "version" not in cfg.keys(): |
|
|
|
|
|
|
|
cfg.update({"version": 2, "torrentDate": True}) |
|
|
|
|
|
|
|
f.seek(0) |
|
|
|
|
|
|
|
f.write(json.dumps(cfg, indent=4, sort_keys=False)) |
|
|
|
|
|
|
|
f.truncate() |
|
|
|
|
|
|
|
config = cfg |
|
|
|
|
|
|
|
logger.debug("Config is loaded.") |
|
|
|
logger.debug("Config is loaded.") |
|
|
|
except OSError as e: |
|
|
|
except OSError as e: |
|
|
|
logger.error(e) |
|
|
|
logger.error(e) |
|
|
|
# if file doesn't exist, we'll create it |
|
|
|
# if file doesn't exist, we'll create it |
|
|
|
with open(FILE_J, 'w') as f: |
|
|
|
FILE_J.write_text(json.dumps(config, indent=4, sort_keys=False)) |
|
|
|
f.write(json.dumps(config, indent=4, sort_keys=False)) |
|
|
|
|
|
|
|
# also write/rewrite ico file |
|
|
|
# also write/rewrite ico file |
|
|
|
with open(path_to(FILENAME + '.ico'), 'wb') as f: |
|
|
|
(BASEDIR / (FILENAME + '.ico')).write_bytes(base64.b64decode(ICON)) |
|
|
|
f.write(base64.b64decode(ICON)) |
|
|
|
|
|
|
|
logger.debug("Write files.") |
|
|
|
logger.debug("Write files.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class kinozal: |
|
|
|
class Kinozal: |
|
|
|
name = 'Kinozal' |
|
|
|
name = 'Kinozal' |
|
|
|
url = 'http://kinozal.tv/' |
|
|
|
url = 'http://kinozal.tv/' |
|
|
|
url_dl = url.replace("//", "//dl.") |
|
|
|
url_dl = url.replace("//", "//dl.") |
|
|
@ -170,7 +161,7 @@ class kinozal: |
|
|
|
self.pretty_error(what) |
|
|
|
self.pretty_error(what) |
|
|
|
return None |
|
|
|
return None |
|
|
|
# do async requests |
|
|
|
# do async requests |
|
|
|
if total > 50: |
|
|
|
if total > PAGES: |
|
|
|
qrs = [PATTERNS[1] % (query, x) for x in rng(total)] |
|
|
|
qrs = [PATTERNS[1] % (query, x) for x in rng(total)] |
|
|
|
with ThreadPoolExecutor(len(qrs)) as executor: |
|
|
|
with ThreadPoolExecutor(len(qrs)) as executor: |
|
|
|
executor.map(self.searching, qrs, timeout=30) |
|
|
|
executor.map(self.searching, qrs, timeout=30) |
|
|
@ -195,10 +186,9 @@ class kinozal: |
|
|
|
path = 'magnet:?xt=urn:btih:' + response.decode()[18:58] |
|
|
|
path = 'magnet:?xt=urn:btih:' + response.decode()[18:58] |
|
|
|
else: |
|
|
|
else: |
|
|
|
# Create a torrent file |
|
|
|
# Create a torrent file |
|
|
|
file, path = tempfile.mkstemp('.torrent') |
|
|
|
with NamedTemporaryFile(suffix='.torrent', delete=False) as fd: |
|
|
|
with os.fdopen(file, "wb") as fd: |
|
|
|
|
|
|
|
# Write it to a file |
|
|
|
|
|
|
|
fd.write(response) |
|
|
|
fd.write(response) |
|
|
|
|
|
|
|
path = fd.name |
|
|
|
|
|
|
|
|
|
|
|
# return magnet link / file path |
|
|
|
# return magnet link / file path |
|
|
|
logger.debug(path + " " + url) |
|
|
|
logger.debug(path + " " + url) |
|
|
@ -214,7 +204,8 @@ class kinozal: |
|
|
|
logger.debug(f"Login. Data before: {form_data}") |
|
|
|
logger.debug(f"Login. Data before: {form_data}") |
|
|
|
# so we first encode vals to cp1251 then do default decode whole string |
|
|
|
# so we first encode vals to cp1251 then do default decode whole string |
|
|
|
data_encoded = urlencode( |
|
|
|
data_encoded = urlencode( |
|
|
|
{k: v.encode('cp1251') for k, v in form_data.items()}).encode() |
|
|
|
{k: v.encode('cp1251') for k, v in form_data.items()} |
|
|
|
|
|
|
|
).encode() |
|
|
|
logger.debug(f"Login. Data after: {data_encoded}") |
|
|
|
logger.debug(f"Login. Data after: {data_encoded}") |
|
|
|
|
|
|
|
|
|
|
|
self._catch_error_request(self.url_login, data_encoded) |
|
|
|
self._catch_error_request(self.url_login, data_encoded) |
|
|
@ -311,6 +302,9 @@ class kinozal: |
|
|
|
self.error = None |
|
|
|
self.error = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# pep8 |
|
|
|
|
|
|
|
kinozal = Kinozal |
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
if __name__ == "__main__": |
|
|
|
engine = kinozal() |
|
|
|
engine = kinozal() |
|
|
|
engine.search('doctor') |
|
|
|
engine.search('doctor') |
|
|
|