simplify and factorize code, remove dead code, follow conventions, use new-style classes

This commit is contained in:
Christophe Benz 2010-12-09 15:27:06 +01:00 committed by Romain Bignon
commit f1b3264a67
28 changed files with 202 additions and 251 deletions

View file

@ -15,6 +15,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.capabilities.torrent import ICapTorrent
from weboob.tools.backend import BaseBackend
@ -31,10 +32,6 @@ class IsohuntBackend(BaseBackend, ICapTorrent):
VERSION = '0.5'
DESCRIPTION = 'isohunt.com bittorrent tracker'
LICENSE = 'GPLv3'
#CONFIG = ValuesDict(Value('domain', label='Domain (example "ssl.what.cd")'),
# Value('protocol', label='Protocol to use', choices=('http', 'https')),
# Value('username', label='Username'),
# Value('password', label='Password', masked=True))
BROWSER = IsohuntBrowser
def create_default_browser(self):
@ -47,7 +44,6 @@ class IsohuntBackend(BaseBackend, ICapTorrent):
torrent = self.browser.get_torrent(id)
if not torrent:
return None
return self.browser.openurl(torrent.url.encode('utf-8')).read()
def iter_torrents(self, pattern):

View file

@ -30,24 +30,19 @@ class IsohuntBrowser(BaseBrowser):
ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
PAGES = {
'https://isohunt.com/torrents/.*iht=-1&ihp=1&ihs1=2&iho1=d' : TorrentsPage,
'https://isohunt.com/torrent_details.*tab=summary' : TorrentPage
}
def __init__(self, *args, **kwargs):
BaseBrowser.__init__(self, *args, **kwargs)
'https://isohunt.com/torrents/.*iht=-1&ihp=1&ihs1=2&iho1=d' : TorrentsPage,
'https://isohunt.com/torrent_details.*tab=summary' : TorrentPage,
}
def home(self):
return self.location('https://isohunt.com')
def iter_torrents(self, pattern):
self.location('https://isohunt.com/torrents/%s?iht=-1&ihp=1&ihs1=2&iho1=d' % pattern)
assert self.is_on_page(TorrentsPage)
return self.page.iter_torrents()
def get_torrent(self, id):
self.location('https://isohunt.com/torrent_details/%s/?tab=summary' % id)
assert self.is_on_page(TorrentPage)
return self.page.get_torrent(id)

View file

@ -16,29 +16,18 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BasePage
from weboob.capabilities.torrent import Torrent
from weboob.tools.browser import BasePage
from weboob.tools.misc import get_bytes_size
__all__ = ['TorrentsPage']
class TorrentsPage(BasePage):
def unit(self, n, u):
m = {'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
'TB': 1024*1024*1024*1024,
}
return float(n*m[u])
def iter_torrents(self):
for tr in self.document.getiterator('tr'):
if tr.attrib.get('class','') == 'hlRow':
if tr.attrib.get('class', '') == 'hlRow':
# TODO à corriger
atitle = tr.getchildren()[2].getchildren()[1]
title = atitle.text
@ -57,41 +46,32 @@ class TorrentsPage(BasePage):
seed = tr.getchildren()[4].text
leech = tr.getchildren()[5].text
url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % idt
yield Torrent(idt,
title,
url=url,
size=get_bytes_size(size, u),
seeders=int(seed),
leechers=int(leech))
torrent = Torrent(idt,
title,
url=url,
size=self.unit(size,u),
seeders=int(seed),
leechers=int(leech))
yield torrent
class TorrentPage(BasePage):
def unit(self, n, u):
m = {'KB': 1024,
'MB': 1024*1024,
'GB': 1024*1024*1024,
'TB': 1024*1024*1024*1024,
}
return float(n*m[u])
def get_torrent(self, id):
title = ''
url = 'https://isohunt.com/download/%s/%s.torrent' % (id , id)
url = 'https://isohunt.com/download/%s/%s.torrent' % (id, id)
for a in self.document.getiterator('a'):
if 'Search more torrents of' in a.attrib.get('title',''):
if 'Search more torrents of' in a.attrib.get('title', ''):
title = a.tail
for span in self.document.getiterator('span'):
if span.attrib.get('style','') == 'color:green;' and ('ShowTip' in span.attrib.get('onmouseover','')):
if span.attrib.get('style', '') == 'color:green;' and ('ShowTip' in span.attrib.get('onmouseover', '')):
seed = span.tail.split(' ')[1]
tip_id = span.attrib.get('onmouseover','').split("'")[1]
tip_id = span.attrib.get('onmouseover', '').split("'")[1]
for div in self.document.getiterator('div'):
# find the corresponding super tip which appears on super mouse hover!
if div.attrib.get('class','') == 'dirs ydsf' and tip_id in div.attrib.get('id',''):
if div.attrib.get('class', '') == 'dirs ydsf' and tip_id in div.attrib.get('id', ''):
leech = div.getchildren()[0].getchildren()[1].tail.split(' ')[2]
# the <b> with the size in it doesn't have a distinction
# have to get it by higher
elif div.attrib.get('id','') == 'torrent_details':
elif div.attrib.get('id', '') == 'torrent_details':
size = div.getchildren()[6].getchildren()[0].getchildren()[0].text
u = size[-2:]
size = float(size[:-3])
@ -104,7 +84,7 @@ class TorrentPage(BasePage):
files = []
count_p_found = 0
for p in self.document.getiterator('p'):
if p.attrib.get('style','') == "line-height:1.2em;margin-top:1.8em":
if p.attrib.get('style', '') == 'line-height:1.2em;margin-top:1.8em':
count_p_found += 1
if count_p_found == 1:
description = p.getchildren()[1].tail
@ -115,24 +95,20 @@ class TorrentPage(BasePage):
files.append(p.getchildren()[0].tail.strip())
for td in self.document.getiterator('td'):
#print td.attrib.get('class')
if td.attrib.get('class','') == 'fileRows':
if td.attrib.get('class', '') == 'fileRows':
filename = td.text
#print "len"+str(len(td.getchildren()))
for slash in td.getchildren():
filename += '/'
filename += slash.tail
files.append(filename)
#--------------------------TODO
torrent = Torrent(id, title)
torrent.url = url
torrent.size = self.unit(size,u)
torrent.size = get_bytes_size(size, u)
torrent.seeders = int(seed)
torrent.leechers = int(leech)
torrent.description = description
torrent.files = files
return torrent