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,
@ -106,12 +107,11 @@ class TorrentsPage(BasePage):
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:
@ -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):

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):

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):