From 7e7f8c85c0914121a6b39e8e47eb5594a838a781 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Tue, 13 Sep 2011 01:27:04 -0400 Subject: [PATCH] Implemented search with a basic UI --- resources/language/English/strings.xml | 9 ++++ resources/lib/gui.py | 63 ++++++++++++++++++++------ resources/lib/search.py | 11 +++-- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index bcd03a4..27c324b 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -15,6 +15,15 @@ Start All Exit + + Browse for torrent file + Search BTJunkie.org + Search ThePirateBay.org + Searching... + No results found + Could not connect to search site + Could not download torrent data + RPC Settings Host diff --git a/resources/lib/gui.py b/resources/lib/gui.py index 3ca68e6..fe3c564 100644 --- a/resources/lib/gui.py +++ b/resources/lib/gui.py @@ -4,10 +4,12 @@ import os import sys import base64 +import urllib2 import xbmc import xbmcgui from basictypes.bytes import Bytes from repeater import Repeater +import search _ = sys.modules[ "__main__" ].__language__ __settings__ = sys.modules[ "__main__" ].__settings__ @@ -39,7 +41,6 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): except: p.close() self.close() - d = xbmcgui.Dialog() (type, e, traceback) = sys.exc_info() message = _(9000) # Unexpected error @@ -49,17 +50,17 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): message = _(9002) # Invalid auth else: message = _(9001) # Unable to connect - if d.yesno(_(2), message, _(3)): + if xbmcgui.Dialog().yesno(_(2), message, _(3)): __settings__.openSettings() elif type is ValueError: # In python 2.4, urllib2.HTTPDigestAuthHandler will barf up a lung # if auth fails and the server wants non-digest authentication message = _(9002) # Invalid auth - if d.yesno(_(2), message, _(3)): + if xbmcgui.Dialog().yesno(_(2), message, _(3)): __settings__.openSettings() else: message = _(9000) # Unexpected error - d.ok(_(2), message) + xbmcgui.Dialog().ok(_(2), message) return False p.close() @@ -107,14 +108,51 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): list = self.getControl(20) if (controlID == 11): # Add torrent - d = xbmcgui.Dialog() - filename = d.browse(1, _(0), 'files', '.torrent') - try: - f = open(filename, 'r') - data = base64.b64encode(f.read()) - self.transmission.add(data) - except: - pass + engines = [ + (_(200), None), + (_(201), search.BTJunkie), + (_(202), search.TPB), + ] + selected = xbmcgui.Dialog().select(_(0), [i[0] for i in engines]) + if selected < 0: + return + engine = engines[selected][1] + if not engine: + filename = xbmcgui.Dialog().browse(1, _(0), 'files', '.torrent') + try: + f = open(filename, 'r') + data = base64.b64encode(f.read()) + self.transmission.add(data) + except: + pass + else: + kb = xbmc.Keyboard('', engines[selected][0]) + kb.doModal() + if not kb.isConfirmed(): + return + terms = kb.getText() + p = xbmcgui.DialogProgress() + p.create(_(0), _(290)) + try: + results = engine().search(terms) + except: + p.close() + xbmcgui.Dialog().ok(_(0), _(292)) + return + p.close() + if not results: + xbmcgui.Dialog().ok(_(0), _(291)) + return + selected = xbmcgui.Dialog().select(_(0), ['[S:%d L:%d] %s' % (t['seeds'], t['leechers'], t['name']) for t in results]) + if selected < 0: + return + try: + f = urllib2.urlopen(results[selected]['url']) + data = base64.b64encode(f.read()) + self.transmission.add(data) + except: + xbmcgui.Dialog().ok(_(0), _(293)) + return if (controlID == 12): # Remove selected torrent item = list.getSelectedItem() @@ -130,7 +168,6 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): # Start selected torrent item = list.getSelectedItem() if item: - t = int(item.getProperty('TorrentID')) self.transmission.start(int(item.getProperty('TorrentID'))) if (controlID == 15): # Stop all torrents diff --git a/resources/lib/search.py b/resources/lib/search.py index c32004c..8821007 100644 --- a/resources/lib/search.py +++ b/resources/lib/search.py @@ -1,7 +1,10 @@ import re +import socket from urllib2 import urlopen from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup +socket.setdefaulttimeout(15) + class Search: def __init__(self): return NotImplemented @@ -10,10 +13,10 @@ class Search: class BTJunkie(Search): def __init__(self): - self.search_uri = 'http://btjunkie.org/rss.xml?query={terms}&o=52' + self.search_uri = 'http://btjunkie.org/rss.xml?query=%s&o=52' def search(self, terms): torrents = [] - url = self.search_uri.format(terms=terms) + url = self.search_uri % '+'.join(terms.split(' ')) f = urlopen(url) soup = BeautifulStoneSoup(f.read()) for item in soup.findAll('item'): @@ -27,10 +30,10 @@ class BTJunkie(Search): return torrents class TPB(Search): def __init__(self): - self.search_uri = 'http://thepiratebay.org/search/{terms}/' + self.search_uri = 'http://thepiratebay.org/search/%s/' def search(self, terms): torrents = [] - url = self.search_uri.format(terms=terms) + url = self.search_uri % '+'.join(terms.split(' ')) f = urlopen(url) soup = BeautifulSoup(f.read()) for details in soup.findAll('a', {'class': 'detLink'}):