From 191458b4098fb5412c9cf5b93c4c1b7978fe2fe5 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Thu, 2 Nov 2017 15:13:15 +0100 Subject: [PATCH] Jackett is now a modualr script that fetches in its own way and returns a list of torrent objects. --- torrentSearch/jackett.py | 71 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/torrentSearch/jackett.py b/torrentSearch/jackett.py index 3ba76da..69d4765 100644 --- a/torrentSearch/jackett.py +++ b/torrentSearch/jackett.py @@ -1,13 +1,70 @@ +#!/usr/bin/env python3.6 + +import xml.etree.ElementTree as ET +from xml.etree.ElementTree import fromstring + +from http_utils import convert_query_to_percent_encoded_octets, build_url, fetch_url +from torrent import Torrent class Jackett(object): """docstring for Jackett""" - def __init__(self, url, query, apikey, limit): + def __init__(self, apikey, host, path, limit, ssl): super(Jackett, self).__init__() - self.url = url - self.query = query self.apikey = apikey - self.limit = limit + self.host = host + self.path = path + self.page_limit = limit + self.ssl = ssl + + # Returns the api key set in the initiator + # return [string] + def get_apikey(self): + return self.apikey + + # Returns the path set in the initiator + # return [string] + def get_path(self): + return self.path + + # Returns the page_limit set in the initiator + # return [string] + def get_page_limit(self): + return self.page_limit + + # Starts the call to getting result from our indexer + # query [string] + # returns [List of Torrent objects] + def search(self, query): + baseUrl = 'http://' + self.host + path = self.get_path() + args_dict = { + 'apikey': self.get_apikey(), + 'limit': self.get_page_limit(), + 'q': convert_query_to_percent_encoded_octets(query) + } + + url = build_url(self.ssl, baseUrl, path, args_dict) + + res = fetch_url(url) + + return self.parse_xml_for_torrents(res.read()) + + + # def __init__(self, name, magnet=None, size=None, uploader=None, date=None, + # seed_count=None, leech_count=None, url=None): + + def parse_xml_for_torrents(self, raw_xml): + tree = ET.fromstring(raw_xml) + channel = tree.find('channel') + results = [] + for child in channel.findall('item'): + title = child.find('title').text + date = child.find('pubDate').text + magnet = child.find('link').text + size = child.find('size').text + + torrent = Torrent(title, magnet=magnet, size=size, date=date) + results.append(torrent.get_all_attr()) + + return results - @classmethod - def build_search_url(url, query, apikey, limit): - return cls(url, query, apikey, limit) \ No newline at end of file