Clean up the Torrent object

As discussed on IRC, those huge constructors aren't ideal.
Includes misc pep8 fixes.
This commit is contained in:
Laurent Bachelier 2012-03-09 00:42:31 +01:00
commit a31a751e28
5 changed files with 44 additions and 44 deletions

View file

@ -39,6 +39,7 @@ __all__ = ['TorrentsPage']
class TorrentsPage(BasePage): class TorrentsPage(BasePage):
TORRENTID_REGEXP = re.compile('torrents\.php\?action=download&id=(\d+)') TORRENTID_REGEXP = re.compile('torrents\.php\?action=download&id=(\d+)')
def format_url(self, url): def format_url(self, url):
return '%s://%s/%s' % (self.browser.PROTOCOL, return '%s://%s/%s' % (self.browser.PROTOCOL,
self.browser.DOMAIN, self.browser.DOMAIN,
@ -75,7 +76,7 @@ class TorrentsPage(BasePage):
if len(tds) == 7: if len(tds) == 7:
# Under a group # Under a group
i = 0 i = 0
elif len(tds) in (8,9): elif len(tds) in (8, 9):
# An alone torrent # An alone torrent
i = len(tds) - 1 i = len(tds) - 1
while i >= 0 and tds[i].find('a') is None: while i >= 0 and tds[i].find('a') is None:
@ -102,16 +103,15 @@ class TorrentsPage(BasePage):
size, unit = tds[i+3].text.split() size, unit = tds[i+3].text.split()
except ValueError: except ValueError:
size, unit = tds[i+2].text.split() size, unit = tds[i+2].text.split()
size = get_bytes_size(float(size.replace(',','')), unit) size = get_bytes_size(float(size.replace(',', '')), unit)
seeders = int(tds[-2].text) seeders = int(tds[-2].text)
leechers = int(tds[-1].text) leechers = int(tds[-1].text)
torrent = Torrent(id, torrent = Torrent(id, title)
title, torrent.url = self.format_url(url)
url=self.format_url(url), torrent.size = size
size=size, torrent.seeders = seeders
seeders=seeders, torrent.leechers = leechers
leechers=leechers)
yield torrent yield torrent
else: else:
debug('unknown attrib: %s' % tr.attrib) debug('unknown attrib: %s' % tr.attrib)

View file

@ -32,7 +32,7 @@ class TorrentsPage(BasePage):
if tr.attrib.get('class', '') == 'hlRow': if tr.attrib.get('class', '') == 'hlRow':
# sometimes the first tr also has the attribute hlRow # sometimes the first tr also has the attribute hlRow
# i use that to ditinct it from the others # i use that to ditinct it from the others
if tr.attrib.has_key('onmouseout'): if 'onmouseout' in tr.attrib:
atitle = tr.getchildren()[2].getchildren()[1] atitle = tr.getchildren()[2].getchildren()[1]
title = atitle.text title = atitle.text
if not title: if not title:
@ -42,7 +42,7 @@ class TorrentsPage(BasePage):
title += bold.text title += bold.text
if bold.tail: if bold.tail:
title += bold.tail title += bold.tail
idt = tr.getchildren()[2].getchildren()[0].attrib.get('href','') idt = tr.getchildren()[2].getchildren()[0].attrib.get('href', '')
idt = idt.split('/')[2] idt = idt.split('/')[2]
size = tr.getchildren()[3].text size = tr.getchildren()[3].text
u = size[-2:] u = size[-2:]
@ -50,12 +50,12 @@ class TorrentsPage(BasePage):
seed = tr.getchildren()[4].text seed = tr.getchildren()[4].text
leech = tr.getchildren()[5].text leech = tr.getchildren()[5].text
url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % idt url = 'https://isohunt.com/download/%s/mon_joli_torrent.torrent' % idt
yield Torrent(idt, torrent = Torrent(idt, title)
title, torrent.url = url
url=url, torrent.size = get_bytes_size(size, u)
size=get_bytes_size(size, u), torrent.seeders = int(seed)
seeders=int(seed), torrent.leechers = int(leech)
leechers=int(leech)) yield torrent
class TorrentPage(BasePage): class TorrentPage(BasePage):
@ -95,7 +95,7 @@ class TorrentPage(BasePage):
description = p.getchildren()[1].tail description = p.getchildren()[1].tail
if count_p_found == 2: if count_p_found == 2:
if p.getchildren()[0].text == 'Directory:': if p.getchildren()[0].text == 'Directory:':
files.append(p.getchildren()[0].tail.strip()+'/') files.append(p.getchildren()[0].tail.strip() + '/')
else: else:
files.append(p.getchildren()[0].tail.strip()) files.append(p.getchildren()[0].tail.strip())

View file

@ -22,6 +22,7 @@ try:
from urlparse import parse_qs from urlparse import parse_qs
except ImportError: except ImportError:
from cgi import parse_qs from cgi import parse_qs
from urlparse import urlsplit from urlparse import urlsplit
from weboob.capabilities.torrent import Torrent from weboob.capabilities.torrent import Torrent
@ -59,13 +60,13 @@ class TorrentsPage(BasePage):
seed = tr.getchildren()[4].text seed = tr.getchildren()[4].text
leech = tr.getchildren()[5].text leech = tr.getchildren()[5].text
yield Torrent(idt, torrent = Torrent(idt, title)
title, torrent.url = url
url=url, torrent.filename = parse_qs(urlsplit(url).query).get('title', [None])[0]
filename=parse_qs(urlsplit(url).query).get('title', [None])[0], torrent.size = get_bytes_size(size, u)
size=get_bytes_size(size, u), torrent.seeders = int(seed)
seeders=int(seed), torrent.leechers = int(leech)
leechers=int(leech)) yield torrent
class TorrentPage(BasePage): class TorrentPage(BasePage):
@ -105,7 +106,7 @@ class TorrentPage(BasePage):
for span in self.document.getiterator('span'): for span in self.document.getiterator('span'):
# sometimes there are others span, this is not so sure but the size of the children list # sometimes there are others span, this is not so sure but the size of the children list
# is enough to know if this is the right span # is enough to know if this is the right span
if (span.attrib.get('class', '') == 'folder' or span.attrib.get('class', '') == 'folderopen') and len(span.getchildren())>2: if (span.attrib.get('class', '') == 'folder' or span.attrib.get('class', '') == 'folderopen') and len(span.getchildren()) > 2:
size = span.getchildren()[1].tail size = span.getchildren()[1].tail
u = span.getchildren()[2].text u = span.getchildren()[2].text
size = float(size.split(': ')[1].replace(',', '.')) size = float(size.split(': ')[1].replace(',', '.'))

View file

@ -55,12 +55,11 @@ class TorrentsPage(BasePage):
seed = tr.getchildren()[2].text seed = tr.getchildren()[2].text
leech = tr.getchildren()[3].text leech = tr.getchildren()[3].text
torrent = Torrent(idt, torrent = Torrent(idt, title)
title, torrent.url = url
url=url, torrent.size = self.unit(float(size), u)
size=self.unit(float(size), u), torrent.seeders = int(seed)
seeders=int(seed), torrent.leechers = int(leech)
leechers=int(leech))
yield torrent yield torrent

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon, Laurent Bachelier # Copyright(C) 2010-2012 Romain Bignon, Laurent Bachelier
# #
# This file is part of weboob. # This file is part of weboob.
# #
@ -19,26 +19,26 @@
from datetime import datetime from datetime import datetime
from .base import IBaseCap, CapBaseObject, NotLoaded from .base import IBaseCap, CapBaseObject
__all__ = ['ICapTorrent', 'Torrent'] __all__ = ['ICapTorrent', 'Torrent']
class Torrent(CapBaseObject): class Torrent(CapBaseObject):
def __init__(self, id, name, date=NotLoaded, size=NotLoaded, url=NotLoaded, def __init__(self, id, name):
seeders=NotLoaded, leechers=NotLoaded, files=NotLoaded,
description=NotLoaded, filename=NotLoaded):
CapBaseObject.__init__(self, id) CapBaseObject.__init__(self, id)
self.add_field('name', basestring, name) self.add_field('name', basestring, name)
self.add_field('size', (int,long,float), size) self.add_field('size', (int, long, float))
self.add_field('date', datetime, date) self.add_field('date', datetime)
self.add_field('url', basestring, url) self.add_field('url', basestring)
self.add_field('seeders', int, seeders) self.add_field('magnet', basestring)
self.add_field('leechers', int, leechers) self.add_field('seeders', int)
self.add_field('files', list, files) self.add_field('leechers', int)
self.add_field('description', basestring, description) self.add_field('files', list)
self.add_field('filename', basestring, filename) # suggested name of the .torrent file self.add_field('description', basestring)
self.add_field('filename', basestring) # suggested name of the .torrent file
class ICapTorrent(IBaseCap): class ICapTorrent(IBaseCap):
def iter_torrents(self, pattern): def iter_torrents(self, pattern):