123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- # VERSION: 1.01
- # AUTHORS: txtsd (thexerothermicsclerodermoid@gmail.com)
- # iptorrents.py - A plugin for qBittorrent to search on iptorrents.com
- # Copyright (C) 2019 txtsd <thexerothermicsclerodermoid@gmail.com>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- import gzip
- import io
- import logging
- import re
- import tempfile
- import urllib.request as request
- from http.cookiejar import CookieJar
- from urllib.error import URLError
- from urllib.parse import urlencode, quote
- from helpers import htmlentitydecode
- from novaprinter import prettyPrinter
- logger = logging.getLogger()
- logger.setLevel(logging.DEBUG)
- class iptorrents(object):
- # Login information ######################################################
- #
- # SET THESE VALUES!!
- #
- username = ""
- password = ""
- ###########################################################################
- url = 'https://iptorrents.com'
- name = 'IPTorrents'
- supported_categories = {
- 'all': '',
- 'movies': '72',
- 'tv': '73',
- 'music': '75',
- 'games': '74',
- 'anime': '60',
- 'software': '1',
- 'pictures': '36',
- 'books': '35'
- }
- def __init__(self):
- """
- Class initialization
- Requires personal login information
- """
- self.ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0'
- self.session = None
- self._login()
- def _login(self):
- """Initiate a session and log into IPTorrents"""
- # Build opener
- cj = CookieJar()
- params = {
- 'username': self.username,
- 'password': self.password
- }
- session = request.build_opener(request.HTTPCookieProcessor(cj))
- # change user-agent
- session.addheaders.pop()
- session.addheaders.append(('User-Agent', self.ua))
- session.addheaders.append(('Referrer', self.url + '/login.php'))
- # send request
- try:
- logging.debug("Trying to connect using given credentials.")
- logging.debug(self.url + '/take_login.php')
- logging.debug(urlencode(params).encode('utf-8'))
- session.open(
- self.url + '/take_login.php',
- urlencode(params).encode('utf-8')
- )
- logging.debug("Connected using given credentials.")
- self.session = session
- except URLError as errno:
- print("Connection Error: {} {}".format(errno.code, errno.reason))
- return
- def _get_link(self, link):
- """Return the HTML content of url page as a string """
- try:
- logging.debug("Trying to open " + link)
- res = self.session.open(link)
- except URLError as errno:
- print("Connection Error: {} {}".format(errno.code, errno.reason))
- return ""
- charset = 'utf-8'
- info = res.info()
- _, charset = info['Content-Type'].split('charset=')
- data = res.read()
- data = data.decode(charset, 'replace')
- data = htmlentitydecode(data)
- return data
- def search_parse(self, link, page=1):
- """ Parses IPTorrents for search results and prints them"""
- logging.debug("Parsing " + link)
- data = self._get_link(link + '&p=' + str(page))
- _tor_table = re.search('<form>(<table id=torrents.+?)</form>', data)
- tor_table = _tor_table.groups()[0] if _tor_table else None
- results = re.finditer(
- r'<a class=" hv" href="(?P<desc_link>/details.+?)">(?P<name>.+?)</a>.+?href="(?P<link>/download.+?)".+?(?P<size>\d+?\.*?\d*? (|K|M|G)B)<.+?t_seeders">(?P<seeds>\d+).+?t_leechers">(?P<leech>\d+?)</t',
- tor_table
- )
- for result in results:
- entry = dict()
- entry['link'] = self.url + quote(result.group('link'))
- entry['name'] = result.group('name')
- entry['size'] = result.group('size')
- entry['seeds'] = result.group('seeds')
- entry['leech'] = result.group('leech')
- entry['engine_url'] = self.url
- entry['desc_link'] = self.url + result.group('desc_link')
- prettyPrinter(entry)
- _num_pages = re.search(r'<a>Page <b>(\d+)</b> of <b>(\d+)</b>', data)
- page = _num_pages.groups()[0] if _num_pages else None
- num_pages = _num_pages.groups()[1] if _num_pages else None
- if (page and num_pages) and (int(page) < int(num_pages)):
- next_page = int(page) + 1
- self.search_parse(link, next_page)
- def download_torrent(self, info):
- """
- Downloads torrent to a temp file and loads it in qBittorrent
- """
- file, path = tempfile.mkstemp('.torrent')
- url = info
- # self._login()
- try:
- logging.debug("Trying to download " + url)
- res = self.session.open(url)
- except URLError as errno:
- print("Connection Error: {} {}".format(errno.code, errno.reason))
- return ""
- data = res.read()
- if data[:2] == b'\x1f\x8b':
- # Data is gzip encoded, decode it
- logging.debug("Data is gzip encoded, decode it")
- compressedstream = io.BytesIO(data)
- gzipper = gzip.GzipFile(fileobj=compressedstream)
- extracted_data = gzipper.read()
- data = extracted_data
- with open(file, 'wb') as f:
- f.write(data)
- print(path + " " + url)
- def search(self, what, cat='all'):
- """
- Formats url according to category and calls search_parse()
- """
- if cat == 'all':
- url = "{0}/t?q={1}&o=seeders".format(
- self.url,
- what
- )
- else:
- url = "{0}/t?{1}=&q={2}&o=seeders".format(
- self.url,
- self.supported_categories[cat],
- what
- )
- self.search_parse(url)
- # For testing purposes.
- # Run with python -m engines.iptorrents
- if __name__ == "__main__":
- engine = iptorrents()
- engine.search('one+piece')
- # engine.download_torrent('')
|