btmon speeded up, all ICapTorrent backends clarified and fillobj cleaned

This commit is contained in:
Julien Veyssier 2013-03-15 18:20:54 +01:00
commit b907c8b266
10 changed files with 101 additions and 39 deletions

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.torrent import ICapTorrent
from weboob.capabilities.torrent import ICapTorrent, Torrent
from weboob.tools.backend import BaseBackend
from .browser import BtmonBrowser
@ -50,3 +50,16 @@ class BtmonBackend(BaseBackend, ICapTorrent):
def iter_torrents(self, pattern):
return self.browser.iter_torrents(quote_plus(pattern.encode('utf-8')))
def fill_torrent(self, torrent, fields):
if 'description' in fields:
tor = self.get_torrent(torrent.id)
torrent.description = tor.description
torrent.magnet = tor.magnet
torrent.files = tor.files
torrent.url = tor.url
return torrent
OBJECTS = {
Torrent:fill_torrent
}

View file

@ -21,7 +21,7 @@
import string
from weboob.capabilities.torrent import Torrent
from weboob.capabilities.base import NotAvailable
from weboob.capabilities.base import NotAvailable, NotLoaded
from weboob.tools.browser import BasePage
from weboob.tools.misc import get_bytes_size
@ -32,22 +32,50 @@ __all__ = ['TorrentsPage','TorrentPage']
class TorrentsPage(BasePage):
def iter_torrents(self):
for div in self.parser.select(self.document.getroot(),'div.list_tor'):
name = NotAvailable
size = NotAvailable
seeders = NotAvailable
leechers = NotAvailable
right_div = self.parser.select(div,'div.list_tor_right',1)
try:
seeders = int(self.parser.select(right_div,'b.green',1).text)
except ValueError:
seeders = 0
try:
leechers = int(self.parser.select(right_div,'b.red',1).text)
except ValueError:
leechers = 0
sizep = self.parser.select(right_div,'p')[0]
sizespan = self.parser.select(sizep,'span')[0]
nsize = float(sizespan.text_content().split(':')[1].split()[0])
usize = sizespan.text_content().split()[-1].upper()
size = get_bytes_size(nsize,usize)
a = self.parser.select(div,'a.list_tor_title',1)
href = a.attrib.get('href','')
self.browser.location('http://%s%s'%(self.browser.DOMAIN,href))
assert self.browser.is_on_page(TorrentPage)
yield self.browser.page.get_torrent()
name = unicode(a.text_content())
id = unicode(href.strip('/').split('.html')[0])
torrent = Torrent(id,name)
torrent.url = NotLoaded
torrent.filename = id
torrent.magnet = NotLoaded
torrent.size = size
torrent.seeders = seeders
torrent.leechers = leechers
torrent.description = NotLoaded
torrent.files = NotLoaded
yield torrent
class TorrentPage(BasePage):
def get_torrent(self):
seed = 0
leech = 0
description = NotAvailable.__unicode__()
description = NotAvailable
url = NotAvailable
magnet = NotAvailable
title = NotAvailable
id = self.browser.geturl().split('.html')[0].split('/')[-1]
id = unicode(self.browser.geturl().split('.html')[0].split('/')[-1])
div = self.parser.select(self.document.getroot(),'div#middle_content',1)
title = u'%s'%self.parser.select(self.document.getroot(),'div#middle_content > h1',1).text
@ -56,8 +84,8 @@ class TorrentPage(BasePage):
seed = slblock_values[0].text
leech = slblock_values[1].text
href_t = self.parser.select(div,'a.down',1).attrib.get('href','')
url = 'http://%s%s'%(self.browser.DOMAIN,href_t)
magnet = self.parser.select(div,'a.magnet',1).attrib.get('href','')
url = u'http://%s%s'%(self.browser.DOMAIN,href_t)
magnet = unicode(self.parser.select(div,'a.magnet',1).attrib.get('href',''))
divtabs = self.parser.select(div,'div#tabs',1)
files_div = self.parser.select(divtabs,'div.body > div.doubleblock > div.leftblock')
@ -74,7 +102,7 @@ class TorrentPage(BasePage):
u = size_text.split(',')[1].strip().translate(None,string.digits).strip('.').strip().upper()
div_desc = self.parser.select(divtabs,'div#descriptionContent')
if len(div_desc) > 0:
description = div_desc[0].text_content()
description = unicode(div_desc[0].text_content())
torrent = Torrent(id, title)
torrent.url = url

View file

@ -29,11 +29,10 @@ class BtmonTest(BackendTest):
def test_torrent(self):
torrents = list(self.backend.iter_torrents('spiderman'))
for torrent in torrents:
path, qs = urllib.splitquery(torrent.url)
assert path.endswith('.torrent')
assert torrent.id
assert torrent.name
assert torrent.description is NotLoaded
assert torrent.files is NotLoaded
# get the file of a random torrent
# from the list (getting them all would be too long)