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.
254 lines
8.7 KiB
254 lines
8.7 KiB
4 years ago
|
# VERSION: 1.3
|
||
5 years ago
|
# AUTHORS: imDMG [imdmgg@gmail.com]
|
||
|
|
||
5 years ago
|
# Rutor.org search engine plugin for qBittorrent
|
||
5 years ago
|
|
||
|
import base64
|
||
|
import json
|
||
|
import logging
|
||
|
import re
|
||
|
import socket
|
||
4 years ago
|
import sys
|
||
5 years ago
|
import time
|
||
|
from concurrent.futures.thread import ThreadPoolExecutor
|
||
|
from html import unescape
|
||
4 years ago
|
from pathlib import Path
|
||
|
from tempfile import NamedTemporaryFile
|
||
5 years ago
|
from urllib.error import URLError, HTTPError
|
||
|
from urllib.parse import unquote
|
||
|
from urllib.request import build_opener, ProxyHandler
|
||
|
|
||
4 years ago
|
try:
|
||
|
from novaprinter import prettyPrinter
|
||
|
except ImportError:
|
||
|
sys.path.insert(0, str(Path(__file__).parent.parent.absolute()))
|
||
|
from novaprinter import prettyPrinter
|
||
5 years ago
|
|
||
|
# default config
|
||
|
config = {
|
||
|
"torrentDate": True,
|
||
|
"username": "USERNAME",
|
||
|
"password": "PASSWORD",
|
||
|
"proxy": False,
|
||
|
"proxies": {
|
||
|
"http": "",
|
||
|
"https": ""
|
||
|
},
|
||
|
"ua": "Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0 "
|
||
|
}
|
||
|
|
||
4 years ago
|
FILE = Path(__file__)
|
||
|
BASEDIR = FILE.parent.absolute()
|
||
|
|
||
|
FILENAME = FILE.name[:-3]
|
||
|
FILE_J, FILE_C = [BASEDIR / (FILENAME + fl) for fl in ['.json', '.cookie']]
|
||
5 years ago
|
|
||
4 years ago
|
PAGES = 100
|
||
5 years ago
|
|
||
|
|
||
|
def rng(t):
|
||
4 years ago
|
return range(1, -(-t // PAGES))
|
||
5 years ago
|
|
||
|
|
||
4 years ago
|
RE_TORRENTS = re.compile(
|
||
|
r'(?:gai|tum)"><td>(.+?)</td.+?href="/(torrent/(\d+).+?)">(.+?)</a.+?right"'
|
||
4 years ago
|
r'>([.\d]+ \w+)</td.+?alt="S"\s/>(.+?)</s.+?red">(.+?)</s', re.S
|
||
|
)
|
||
4 years ago
|
RE_RESULTS = re.compile(r'</b>\sРезультатов\sпоиска\s(\d{1,4})\s', re.S)
|
||
|
PATTERNS = ('%ssearch/%i/%i/000/0/%s',)
|
||
5 years ago
|
|
||
|
# base64 encoded image
|
||
|
ICON = ("AAABAAEAEBAAAAEAGABoAwAAFgAAACgAAAAQAAAAIAAAAAEAGAAAAAAAAAAAAAAAAAAAAA"
|
||
|
"AAAAAAAAAAAAAAAc4AAMwHNdcQ4vsN3fYS2fUY3fUe3fMj4fkk4fco4PYo5fgk7f5gp8Zu"
|
||
|
"ZZtsa59FIXZEGm4kh74PyeoLGp8NHK4PHrwQHr8VIb8XJL4bJrUcKJ8optEdtPMBGcQAIc"
|
||
|
"XeZAPVYwdA3MQFf8EDAJoFAMEEAM0AANIAAM4AAM0EAL8CAI8bXaEV1/cBHMsGDNTVWAOo"
|
||
|
"dTIU5/ELuOAJM6sEALsIAMoEALkCBbgFALUGAKshgMcvpNUTzOoFQNIFANqxQgBpkmgKue"
|
||
|
"8IT8UUy+8HO7MHPb8Gt+IG3vQHm9YKi84X4foKI7kRl+AWiMwSDYyxjXZAy84HdNYEALcP"
|
||
|
"guYM+vsL6PgGl/wBWN4K1/EF//8LbdQEALgEVc41zMp0YC+t0N0XxPcCIbwGAMkGGOUGUv"
|
||
|
"QKPPUEANsIU9ENvvAJw/ULnekGAr8FJcIUzfRycEZwzuMFnuYEArQCAdYDANYHAMQFAMwG"
|
||
|
"PcwM2vsHU/QKPegLwvYEEckFBrsOt/Y+kYky5/YGgNAGAKkHAc4JMssSoN0GTb0L2/gHYP"
|
||
|
"kCAPkFKOMP0fIHGc0EAKwLgNAq3OMd/P0Al9ACBqQCAMALbOMG+/8E8v0KjugBAO4CAPAG"
|
||
|
"Q9MNyPYEB8QBAKQCe8cW9//T+/09+/8Aqd8GIbIFAMAKbuUG6f8Ht/IFFeEAAMYPqeYMhO"
|
||
|
"EGB6oCgtUY5fuG0tv//vzs+PlQ9fwAw+4CLLoIALgJR+EFU+wEFcweZNAkquMFMrkArOor"
|
||
|
"4fSrxsvWx8n5/fv5+fn3+/iC8fsLzPIAUscEALMDAL8QPtAsetUFWsUHue1r7/vc6evOzM"
|
||
|
"fFx8n5/fvy+fj89vb/9/e+9/o44/oNi9kBD54CFKQJg9Qu4vu09vr/+ff89fTIz8rFx8n5"
|
||
|
"/fvy+fj59vb49vf/+fbh+vtk6vw1rN03suFn6vnl/f3/+fn49vj18/TIz8rFx8n5/fvy+f"
|
||
|
"j59vb39vf39/f//P3w+fme6/ak8Prv+fj//f369/r39vj18/TIz8rFx8ngBwAA4AMAAMAD"
|
||
|
"AADAAwAAwAMAAMABAACAAQAAgAEAAAAAAAAAAAAAgAEAAMADAADgBwAA+B8AAPw/AAD"
|
||
|
"+fwAA")
|
||
|
|
||
|
# setup logging
|
||
|
logging.basicConfig(
|
||
|
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
|
||
4 years ago
|
datefmt="%m-%d %H:%M",
|
||
4 years ago
|
level=logging.DEBUG
|
||
|
)
|
||
4 years ago
|
|
||
5 years ago
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
try:
|
||
4 years ago
|
config = json.loads(FILE_J.read_text())
|
||
5 years ago
|
logger.debug("Config is loaded.")
|
||
|
except OSError as e:
|
||
|
logger.error(e)
|
||
|
# if file doesn't exist, we'll create it
|
||
4 years ago
|
FILE_J.write_text(json.dumps(config, indent=4, sort_keys=False))
|
||
5 years ago
|
# also write/rewrite ico file
|
||
4 years ago
|
(BASEDIR / (FILENAME + '.ico')).write_bytes(base64.b64decode(ICON))
|
||
5 years ago
|
logger.debug("Write files.")
|
||
|
|
||
|
|
||
4 years ago
|
class Rutor:
|
||
5 years ago
|
name = 'Rutor'
|
||
|
url = 'http://rutor.info/'
|
||
4 years ago
|
url_dl = url.replace("//", "//d.") + "download/"
|
||
5 years ago
|
supported_categories = {'all': 0,
|
||
|
'movies': 1,
|
||
|
'tv': 6,
|
||
|
'music': 2,
|
||
|
'games': 8,
|
||
|
'anime': 10,
|
||
|
'software': 9,
|
||
|
'pictures': 3,
|
||
|
'books': 11}
|
||
|
|
||
|
def __init__(self):
|
||
|
# error message
|
||
|
self.error = None
|
||
|
|
||
|
# establish connection
|
||
|
self.session = build_opener()
|
||
|
|
||
|
# add proxy handler if needed
|
||
|
if config['proxy']:
|
||
|
if any(config['proxies'].values()):
|
||
|
self.session.add_handler(ProxyHandler(config['proxies']))
|
||
|
logger.debug("Proxy is set!")
|
||
|
else:
|
||
|
self.error = "Proxy enabled, but not set!"
|
||
|
|
||
|
# change user-agent
|
||
4 years ago
|
self.session.addheaders = [('User-Agent', config['ua'])]
|
||
5 years ago
|
|
||
|
def search(self, what, cat='all'):
|
||
|
if self.error:
|
||
|
self.pretty_error(what)
|
||
4 years ago
|
return None
|
||
|
query = PATTERNS[0] % (self.url, 0, self.supported_categories[cat],
|
||
5 years ago
|
what.replace(" ", "+"))
|
||
|
|
||
|
# 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
|
||
4 years ago
|
if total > PAGES:
|
||
5 years ago
|
query = query.replace('h/0', 'h/%i')
|
||
|
qrs = [query % x for x in rng(total)]
|
||
|
with ThreadPoolExecutor(len(qrs)) as executor:
|
||
|
executor.map(self.searching, qrs, timeout=30)
|
||
|
|
||
|
logger.debug(f"--- {time.time() - t0} seconds ---")
|
||
|
logger.info(f"Found torrents: {total}")
|
||
|
|
||
|
def download_torrent(self, url: str):
|
||
|
# Download url
|
||
|
response = self._catch_error_request(url)
|
||
|
if self.error:
|
||
|
self.pretty_error(url)
|
||
4 years ago
|
return None
|
||
5 years ago
|
|
||
|
# Create a torrent file
|
||
4 years ago
|
with NamedTemporaryFile(suffix='.torrent', delete=False) as fd:
|
||
4 years ago
|
fd.write(response)
|
||
5 years ago
|
|
||
4 years ago
|
# return file path
|
||
|
logger.debug(fd.name + " " + url)
|
||
|
print(fd.name + " " + url)
|
||
5 years ago
|
|
||
|
def searching(self, query, first=False):
|
||
|
response = self._catch_error_request(query)
|
||
|
if not response:
|
||
|
return None
|
||
4 years ago
|
page, torrents_found = response.decode(), -1
|
||
|
if first:
|
||
|
# firstly we check if there is a result
|
||
|
torrents_found = int(RE_RESULTS.search(page)[1])
|
||
|
if not torrents_found:
|
||
|
return 0
|
||
5 years ago
|
self.draw(page)
|
||
|
|
||
4 years ago
|
return torrents_found
|
||
5 years ago
|
|
||
|
def draw(self, html: str):
|
||
4 years ago
|
torrents = RE_TORRENTS.findall(html)
|
||
5 years ago
|
for tor in torrents:
|
||
|
torrent_date = ""
|
||
|
if config['torrentDate']:
|
||
|
# replace names month
|
||
4 years ago
|
months = ('Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн',
|
||
|
'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек')
|
||
|
ct = [unescape(tor[0].replace(m, f"{i:02d}"))
|
||
|
for i, m in enumerate(months, 1) if m in tor[0]][0]
|
||
5 years ago
|
ct = time.strftime("%y.%m.%d", time.strptime(ct, "%d %m %y"))
|
||
|
torrent_date = f'[{ct}] '
|
||
|
|
||
|
prettyPrinter({
|
||
|
"engine_url": self.url,
|
||
|
"desc_link": self.url + tor[1],
|
||
|
"name": torrent_date + unescape(tor[3]),
|
||
4 years ago
|
"link": self.url_dl + tor[2],
|
||
5 years ago
|
"size": unescape(tor[4]),
|
||
|
"seeds": unescape(tor[5]),
|
||
|
"leech": unescape(tor[6])
|
||
|
})
|
||
|
del torrents
|
||
|
|
||
4 years ago
|
def _catch_error_request(self, url=None, data=None, repeated=False):
|
||
5 years ago
|
url = url or self.url
|
||
|
|
||
|
try:
|
||
4 years ago
|
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()
|
||
5 years ago
|
raise URLError(f"{self.url} is blocked. Try another proxy.")
|
||
|
except (socket.error, socket.timeout) as err:
|
||
4 years ago
|
if not repeated:
|
||
5 years ago
|
return self._catch_error_request(url, data, True)
|
||
|
logger.error(err)
|
||
|
self.error = f"{self.url} is not response! Maybe it is blocked."
|
||
|
if "no host given" in err.args:
|
||
|
self.error = "Proxy is bad, try another!"
|
||
|
except (URLError, HTTPError) as err:
|
||
|
logger.error(err.reason)
|
||
|
self.error = err.reason
|
||
|
if hasattr(err, 'code'):
|
||
|
self.error = f"Request to {url} failed with status: {err.code}"
|
||
|
|
||
|
return None
|
||
|
|
||
|
def pretty_error(self, what):
|
||
|
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
|
||
|
|
||
|
|
||
4 years ago
|
# pep8
|
||
|
rutor = Rutor
|
||
|
|
||
5 years ago
|
if __name__ == "__main__":
|
||
3 years ago
|
# if BASEDIR.parent.joinpath('settings_gui.py').exists():
|
||
|
# from settings_gui import EngineSettingsGUI
|
||
|
#
|
||
|
# EngineSettingsGUI(FILENAME)
|
||
5 years ago
|
engine = rutor()
|
||
|
engine.search('doctor')
|