mirror of https://github.com/YGGverse/qBt_SE.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
347 lines
13 KiB
347 lines
13 KiB
![]()
2 years ago
|
# VERSION: 2.7
|
||
![]()
6 years ago
|
# AUTHORS: imDMG [imdmgg@gmail.com]
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
# Kinozal.tv search engine plugin for qBittorrent
|
||
![]()
6 years ago
|
|
||
![]()
5 years ago
|
import base64
|
||
![]()
4 years ago
|
import gzip
|
||
![]()
6 years ago
|
import json
|
||
![]()
6 years ago
|
import logging
|
||
|
import re
|
||
![]()
3 years ago
|
import sys
|
||
![]()
6 years ago
|
import time
|
||
![]()
5 years ago
|
from concurrent.futures.thread import ThreadPoolExecutor
|
||
![]()
2 years ago
|
from dataclasses import dataclass, field
|
||
![]()
5 years ago
|
from functools import partial
|
||
|
from html import unescape
|
||
![]()
5 years ago
|
from http.cookiejar import MozillaCookieJar
|
||
![]()
3 years ago
|
from pathlib import Path
|
||
|
from tempfile import NamedTemporaryFile
|
||
![]()
2 years ago
|
from typing import Union, Optional
|
||
![]()
5 years ago
|
from urllib.error import URLError, HTTPError
|
||
|
from urllib.parse import urlencode, unquote
|
||
|
from urllib.request import build_opener, HTTPCookieProcessor, ProxyHandler
|
||
|
|
||
![]()
3 years ago
|
try:
|
||
|
from novaprinter import prettyPrinter
|
||
|
except ImportError:
|
||
|
sys.path.insert(0, str(Path(__file__).parent.parent.absolute()))
|
||
|
from novaprinter import prettyPrinter
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
# setup logging
|
||
|
logging.basicConfig(
|
||
|
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
|
||
|
datefmt="%m-%d %H:%M",
|
||
|
level=logging.DEBUG
|
||
|
)
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
![]()
5 years ago
|
|
||
![]()
3 years ago
|
FILE = Path(__file__)
|
||
|
BASEDIR = FILE.parent.absolute()
|
||
|
|
||
|
FILENAME = FILE.name[:-3]
|
||
![]()
2 years ago
|
FILE_J, FILE_C = [BASEDIR / (FILENAME + fl) for fl in (".json", ".cookie")]
|
||
![]()
5 years ago
|
|
||
![]()
3 years ago
|
PAGES = 50
|
||
![]()
5 years ago
|
|
||
![]()
4 years ago
|
RE_TORRENTS = re.compile(
|
||
|
r'nam"><a\s+?href="/(.+?)"\s+?class="r\d">(.+?)</a>.+?s\'>.+?s\'>(.+?)<.+?'
|
||
![]()
3 years ago
|
r'sl_s\'>(\d+?)<.+?sl_p\'>(\d+?)<.+?s\'>(.+?)</td>', re.S
|
||
|
)
|
||
![]()
2 years ago
|
RE_RESULTS = re.compile(r"</span>Найдено\s+?(\d+?)\s+?раздач", re.S)
|
||
|
PATTERNS = ("%sbrowse.php?s=%s&c=%s", "%s&page=%s")
|
||
![]()
5 years ago
|
|
||
|
# base64 encoded image
|
||
|
ICON = ("AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAQAQAAAAAAAAAAA"
|
||
|
"AAAAAAAAAAAACARztMgEc7/4BHO/+ARztMAAAAAIBHO0yhd2n/gEc7/6F3af+ARztMAAAA"
|
||
|
"AIBHO0yARzv/gEc7/4BHO0wAAAAAgEc7/7iYiv/O4+r/pH5x/4FIPP+kfnH/zsrE/87j6v"
|
||
|
"/OycL/pYB1/4BHO/+jfHD/ztbV/7+yrP+ARzv/AAAAAIBHO//O4+r/zu/9/87v/f/O7/3/"
|
||
|
"zu/9/87v/f/O7/3/zu/9/87v/f/O7/3/zu/9/87v/f/O1dT/gEc7/wAAAACARztMpYB1/8"
|
||
|
"7v/f8IC5X/CAuV/wgLlf8IC5X/zu/9/77h+v9vgcv/SFSy/wAAif97j87/oXdp/4BHO0wA"
|
||
|
"AAAAAAAAAIBHO//O7/3/gabq/w4Tnv8OE57/gabq/87v/f96muj/DBCd/wAAif83SMf/zu"
|
||
|
"/9/4BHO/8AAAAAAAAAAIBHO0ynhXv/zu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
||
|
"ADAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAACAAwAAAAEAAAABAAAAAQAACCEAAA== ")
|
||
|
|
||
![]()
4 years ago
|
|
||
![]()
2 years ago
|
def rng(t: int) -> range:
|
||
|
return range(1, -(-t // PAGES))
|
||
|
|
||
|
|
||
|
@dataclass
|
||
|
class Config:
|
||
|
username: str = "USERNAME"
|
||
|
password: str = "PASSWORD"
|
||
|
torrent_date: bool = True
|
||
|
magnet: bool = False
|
||
|
proxy: bool = False
|
||
|
# dynamic_proxy: bool = True
|
||
|
proxies: dict = field(default_factory=lambda: {"http": "", "https": ""})
|
||
|
ua: str = ("Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 "
|
||
|
"Firefox/38.0 ")
|
||
|
|
||
|
def __post_init__(self):
|
||
|
try:
|
||
|
if not self._validate_json(json.loads(FILE_J.read_text())):
|
||
|
raise ValueError("Incorrect json scheme.")
|
||
|
except Exception as e:
|
||
|
logger.error(e)
|
||
|
FILE_J.write_text(self.to_str())
|
||
|
(BASEDIR / f"{FILENAME}.ico").write_bytes(base64.b64decode(ICON))
|
||
|
|
||
|
def to_str(self) -> str:
|
||
|
return json.dumps(self.to_dict(), indent=4, sort_keys=False)
|
||
|
|
||
|
def to_dict(self) -> dict:
|
||
|
return {self._to_camel(k): v for k, v in self.__dict__.items()}
|
||
|
|
||
|
def _validate_json(self, obj: dict) -> bool:
|
||
|
is_valid = True
|
||
|
for k, v in self.__dict__.items():
|
||
|
_val = obj.get(self._to_camel(k))
|
||
|
if type(_val) is not type(v):
|
||
|
is_valid = False
|
||
|
continue
|
||
|
setattr(self, k, _val)
|
||
|
return is_valid
|
||
|
|
||
|
@staticmethod
|
||
|
def _to_camel(s: str) -> str:
|
||
|
return "".join(x.title() if i else x
|
||
|
for i, x in enumerate(s.split("_")))
|
||
![]()
5 years ago
|
|
||
![]()
2 years ago
|
|
||
|
config = Config()
|
||
![]()
5 years ago
|
|
||
![]()
6 years ago
|
|
||
![]()
3 years ago
|
class Kinozal:
|
||
![]()
2 years ago
|
name = "Kinozal"
|
||
|
url = "http://kinozal.tv/"
|
||
![]()
5 years ago
|
url_dl = url.replace("//", "//dl.")
|
||
![]()
2 years ago
|
url_login = url + "takelogin.php"
|
||
|
supported_categories = {"all": "0",
|
||
|
"movies": "1002",
|
||
|
"tv": "1001",
|
||
|
"music": "1004",
|
||
|
"games": "23",
|
||
|
"anime": "20",
|
||
|
"software": "32"}
|
||
|
|
||
|
# error message
|
||
|
error: Optional[str] = None
|
||
|
# cookies
|
||
|
mcj = MozillaCookieJar()
|
||
|
# establish connection
|
||
|
session = build_opener(HTTPCookieProcessor(mcj))
|
||
![]()
6 years ago
|
|
||
|
def __init__(self):
|
||
|
# add proxy handler if needed
|
||
![]()
2 years ago
|
if config.proxy:
|
||
|
if any(config.proxies.values()):
|
||
|
self.session.add_handler(ProxyHandler(config.proxies))
|
||
![]()
5 years ago
|
logger.debug("Proxy is set!")
|
||
|
else:
|
||
|
self.error = "Proxy enabled, but not set!"
|
||
![]()
6 years ago
|
|
||
|
# change user-agent
|
||
![]()
2 years ago
|
self.session.addheaders = [("User-Agent", config.ua)]
|
||
![]()
5 years ago
|
|
||
|
# load local cookies
|
||
![]()
5 years ago
|
try:
|
||
![]()
2 years ago
|
self.mcj.load(FILE_C, ignore_discard=True)
|
||
|
if "uid" in [cookie.name for cookie in self.mcj]:
|
||
![]()
5 years ago
|
# if cookie.expires < int(time.time())
|
||
![]()
5 years ago
|
logger.info("Local cookies is loaded")
|
||
![]()
5 years ago
|
else:
|
||
![]()
5 years ago
|
logger.info("Local cookies expired or bad")
|
||
![]()
2 years ago
|
logger.debug(f"That we have: {[cookie for cookie in self.mcj]}")
|
||
|
self.mcj.clear()
|
||
|
self.login()
|
||
![]()
5 years ago
|
except FileNotFoundError:
|
||
![]()
2 years ago
|
self.login()
|
||
![]()
5 years ago
|
|
||
![]()
2 years ago
|
def search(self, what: str, cat: str = "all") -> None:
|
||
![]()
5 years ago
|
if self.error:
|
||
|
self.pretty_error(what)
|
||
![]()
4 years ago
|
return None
|
||
|
query = PATTERNS[0] % (self.url, what.replace(" ", "+"),
|
||
![]()
5 years ago
|
self.supported_categories[cat])
|
||
![]()
5 years ago
|
|
||
![]()
5 years ago
|
# make first request (maybe it enough)
|
||
|
t0, total = time.time(), self.searching(query, True)
|
||
|
if self.error:
|
||
|
self.pretty_error(what)
|
||
![]()
4 years ago
|
return None
|
||
![]()
5 years ago
|
# do async requests
|
||
![]()
3 years ago
|
if total > PAGES:
|
||
![]()
4 years ago
|
qrs = [PATTERNS[1] % (query, x) for x in rng(total)]
|
||
![]()
5 years ago
|
with ThreadPoolExecutor(len(qrs)) as executor:
|
||
|
executor.map(self.searching, qrs, timeout=30)
|
||
![]()
6 years ago
|
|
||
![]()
5 years ago
|
logger.debug(f"--- {time.time() - t0} seconds ---")
|
||
|
logger.info(f"Found torrents: {total}")
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
def download_torrent(self, url: str) -> None:
|
||
![]()
6 years ago
|
# choose download method
|
||
![]()
2 years ago
|
if config.magnet:
|
||
![]()
3 years ago
|
url = "%sget_srv_details.php?action=2&id=%s" % (self.url,
|
||
![]()
2 years ago
|
url.split("=")[1])
|
||
![]()
5 years ago
|
|
||
![]()
2 years ago
|
response = self._request(url)
|
||
![]()
5 years ago
|
if self.error:
|
||
|
self.pretty_error(url)
|
||
![]()
4 years ago
|
return None
|
||
![]()
5 years ago
|
|
||
![]()
2 years ago
|
if config.magnet:
|
||
![]()
4 years ago
|
if response.startswith(b"\x1f\x8b\x08"):
|
||
|
response = gzip.decompress(response)
|
||
![]()
2 years ago
|
path = "magnet:?xt=urn:btih:" + response.decode()[18:58]
|
||
![]()
6 years ago
|
else:
|
||
|
# Create a torrent file
|
||
![]()
2 years ago
|
with NamedTemporaryFile(suffix=".torrent", delete=False) as fd:
|
||
![]()
4 years ago
|
fd.write(response)
|
||
![]()
3 years ago
|
path = fd.name
|
||
![]()
6 years ago
|
|
||
![]()
5 years ago
|
# return magnet link / file path
|
||
|
logger.debug(path + " " + url)
|
||
|
print(path + " " + url)
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
def login(self) -> None:
|
||
![]()
5 years ago
|
if self.error:
|
||
![]()
4 years ago
|
return None
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
form_data = {"username": config.username, "password": config.password}
|
||
![]()
5 years ago
|
logger.debug(f"Login. Data before: {form_data}")
|
||
![]()
2 years ago
|
# encoding to cp1251 then do default decode whole string
|
||
|
data_encoded = urlencode(form_data, encoding="cp1251").encode()
|
||
![]()
5 years ago
|
logger.debug(f"Login. Data after: {data_encoded}")
|
||
|
|
||
![]()
2 years ago
|
self._request(self.url_login, data_encoded)
|
||
![]()
5 years ago
|
if self.error:
|
||
![]()
4 years ago
|
return None
|
||
![]()
2 years ago
|
logger.debug(f"That we have: {[cookie for cookie in self.mcj]}")
|
||
|
if "uid" in [cookie.name for cookie in self.mcj]:
|
||
|
self.mcj.save(FILE_C, ignore_discard=True, ignore_expires=True)
|
||
|
logger.info("We successfully authorized")
|
||
![]()
5 years ago
|
else:
|
||
|
self.error = "We not authorized, please check your credentials!"
|
||
|
logger.warning(self.error)
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
def searching(self, query: str, first: bool = False) -> Union[None, int]:
|
||
|
response = self._request(query)
|
||
|
if self.error:
|
||
![]()
5 years ago
|
return None
|
||
![]()
4 years ago
|
if response.startswith(b"\x1f\x8b\x08"):
|
||
|
response = gzip.decompress(response)
|
||
![]()
2 years ago
|
page, torrents_found = response.decode("cp1251"), -1
|
||
![]()
4 years ago
|
if first:
|
||
![]()
2 years ago
|
# check login status
|
||
|
if "Гость! ( Зарегистрируйтесь )" in page:
|
||
|
logger.debug("Looks like we lost session id, lets login")
|
||
|
self.mcj.clear()
|
||
|
self.login()
|
||
|
if self.error:
|
||
|
return None
|
||
![]()
4 years ago
|
# firstly we check if there is a result
|
||
![]()
2 years ago
|
result = RE_RESULTS.search(page)
|
||
|
if not result:
|
||
|
self.error = "Unexpected page content"
|
||
|
return None
|
||
|
torrents_found = int(result[1])
|
||
![]()
4 years ago
|
if not torrents_found:
|
||
|
return 0
|
||
![]()
6 years ago
|
self.draw(page)
|
||
|
|
||
![]()
4 years ago
|
return torrents_found
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
def draw(self, html: str) -> None:
|
||
![]()
4 years ago
|
torrents = RE_TORRENTS.findall(html)
|
||
![]()
5 years ago
|
_part = partial(time.strftime, "%y.%m.%d")
|
||
|
# yeah this is yesterday
|
||
|
yesterday = _part(time.localtime(time.time() - 86400))
|
||
|
for tor in torrents:
|
||
|
torrent_date = ""
|
||
![]()
2 years ago
|
if config.torrent_date:
|
||
![]()
5 years ago
|
ct = tor[5].split()[0]
|
||
|
if "сегодня" in ct:
|
||
|
torrent_date = _part()
|
||
|
elif "вчера" in ct:
|
||
|
torrent_date = yesterday
|
||
|
else:
|
||
|
torrent_date = _part(time.strptime(ct, "%d.%m.%Y"))
|
||
![]()
2 years ago
|
torrent_date = f"[{torrent_date}] "
|
||
![]()
6 years ago
|
|
||
![]()
5 years ago
|
# replace size units
|
||
![]()
2 years ago
|
table = {"Т": "T", "Г": "G", "М": "M", "К": "K", "Б": "B"}
|
||
![]()
5 years ago
|
|
||
|
prettyPrinter({
|
||
|
"engine_url": self.url,
|
||
|
"desc_link": self.url + tor[0],
|
||
|
"name": torrent_date + unescape(tor[1]),
|
||
![]()
4 years ago
|
"link": f"{self.url_dl}download.php?id={tor[0].split('=')[-1]}",
|
||
![]()
5 years ago
|
"size": tor[2].translate(tor[2].maketrans(table)),
|
||
|
"seeds": tor[3],
|
||
|
"leech": tor[4]
|
||
|
})
|
||
|
del torrents
|
||
![]()
6 years ago
|
|
||
![]()
2 years ago
|
def _request(
|
||
|
self, url: str, data: Optional[bytes] = None, repeated: bool = False
|
||
|
) -> Union[bytes, None]:
|
||
![]()
6 years ago
|
try:
|
||
![]()
4 years ago
|
with self.session.open(url, data, 5) as r:
|
||
|
# checking that tracker isn't blocked
|
||
![]()
2 years ago
|
if r.geturl().startswith((self.url, self.url_dl)):
|
||
![]()
4 years ago
|
return r.read()
|
||
![]()
2 years ago
|
self.error = f"{url} is blocked. Try another proxy."
|
||
![]()
5 years ago
|
except (URLError, HTTPError) as err:
|
||
|
logger.error(err.reason)
|
||
![]()
2 years ago
|
error = str(err.reason)
|
||
|
if "timed out" in error and not repeated:
|
||
|
logger.debug("Repeating request...")
|
||
|
return self._request(url, data, True)
|
||
|
if "no host given" in error:
|
||
|
self.error = "Proxy is bad, try another!"
|
||
|
elif hasattr(err, "code"):
|
||
![]()
5 years ago
|
self.error = f"Request to {url} failed with status: {err.code}"
|
||
![]()
2 years ago
|
else:
|
||
|
self.error = f"{url} is not response! Maybe it is blocked."
|
||
![]()
5 years ago
|
|
||
|
return None
|
||
|
|
||
![]()
2 years ago
|
def pretty_error(self, what: str) -> None:
|
||
![]()
5 years ago
|
prettyPrinter({"engine_url": self.url,
|
||
|
"desc_link": "https://github.com/imDMG/qBt_SE",
|
||
|
"name": f"[{unquote(what)}][Error]: {self.error}",
|
||
|
"link": self.url + "error",
|
||
|
"size": "1 TB", # lol
|
||
|
"seeds": 100,
|
||
|
"leech": 100})
|
||
|
|
||
|
self.error = None
|
||
![]()
6 years ago
|
|
||
![]()
6 years ago
|
|
||
![]()
3 years ago
|
# pep8
|
||
|
kinozal = Kinozal
|
||
|
|
||
![]()
6 years ago
|
if __name__ == "__main__":
|
||
![]()
2 years ago
|
if BASEDIR.parent.joinpath("settings_gui.py").exists():
|
||
![]()
3 years ago
|
from settings_gui import EngineSettingsGUI
|
||
|
|
||
|
EngineSettingsGUI(FILENAME)
|
||
![]()
5 years ago
|
engine = kinozal()
|
||
![]()
2 years ago
|
engine.search("doctor")
|