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

@ -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