Make Collection more safe and sane
* Remove callbacks in Collection object Make Collection a "dumb" object (and also a base object, though it isn't very useful for now) * Rename Path to WorkingPath, because it is more about managing state than being a single path. * Rewrite almost all WorkingPath, because the code was overly complicated for no reason (I tried some special cases and it turned out that fromstring didn't handle them, and that the quote-escape-unquote was just unecessary). I also rewrote it to be more pythonic (no more lambdas and maps) and added tests. * Require the full split path when creating a Collection. Because, come to think of it, an object needs an unique identifier; in the case of Collections, it is the full path, not only its last part. I might even replace the id by the full split path in the future. * There is now only one way to get items of a Collection: calling iter_resources(). * Rewrite flatten_resources to iter_resources_flat(), which just calls iter_resources() recursively. * Rewrite the collection part of the canalplus module. There is no more callback or a page calling the browser to check another page! The logic is only in iter_resources(). The resulting code is not very pretty, but it should get better. As a bonus, avoid to reload the main XML file when we already have it open. * change_path() now expects a split path and not a string. * up/home special cases for "cd" are handled in the same place, and store the previous place properly (but are not yet exploitable by an user command). This is a big commit but it would be hard to split it in *working* commits. If you read this entire commit message, I will buy you a beer. refs #774 fixes #773
This commit is contained in:
parent
1dd26e5ffe
commit
b4b7182960
13 changed files with 147 additions and 148 deletions
|
|
@ -28,13 +28,13 @@ from weboob.tools.browser.decorators import id2url
|
|||
from .pages import InitPage, VideoPage
|
||||
from .video import CanalplusVideo
|
||||
|
||||
from weboob.capabilities.collection import Collection, CollectionNotFound
|
||||
from weboob.capabilities.collection import CollectionNotFound
|
||||
|
||||
__all__ = ['CanalplusBrowser']
|
||||
|
||||
|
||||
class XMLParser(object):
|
||||
def parse(self, data, encoding=None):
|
||||
def parse(self, data, encoding=None):
|
||||
if encoding is None:
|
||||
parser = None
|
||||
else:
|
||||
|
|
@ -60,11 +60,8 @@ class CanalplusBrowser(BaseBrowser):
|
|||
}
|
||||
|
||||
def __init__(self, quality, *args, **kwargs):
|
||||
BaseBrowser.__init__(self, parser= self.PARSER, *args, **kwargs)
|
||||
if quality in self.FORMATS:
|
||||
self.quality = self.FORMATS[quality]
|
||||
else:
|
||||
self.quality = 'HD'
|
||||
BaseBrowser.__init__(self, parser=self.PARSER, *args, **kwargs)
|
||||
self.quality = self.FORMATS.get(quality, self.FORMATS['hd'])
|
||||
|
||||
def home(self):
|
||||
self.location('http://service.canal-plus.com/video/rest/initPlayer/cplus/')
|
||||
|
|
@ -79,19 +76,33 @@ class CanalplusBrowser(BaseBrowser):
|
|||
return self.page.get_video(video, self.quality)
|
||||
|
||||
def iter_resources(self, split_path):
|
||||
self.home()
|
||||
collections = self.page.collections
|
||||
if not self.is_on_page(InitPage):
|
||||
self.home()
|
||||
channels = self.page.get_channels()
|
||||
|
||||
def walk_res(path, collections):
|
||||
if len(path) == 0 or not isinstance(collections, (list, Collection)):
|
||||
return collections
|
||||
i = path[0]
|
||||
matches = [collection
|
||||
for collection in collections
|
||||
if collection.id == i or collection.title == i]
|
||||
if not len(matches):
|
||||
raise CollectionNotFound(path)
|
||||
if len(split_path) == 0:
|
||||
for channel in channels:
|
||||
if len(channel.split_path) == 1:
|
||||
yield channel
|
||||
elif len(split_path) == 1:
|
||||
for channel in channels:
|
||||
if len(channel.split_path) == 2 and split_path[0] == channel.split_path[0]:
|
||||
yield channel
|
||||
elif len(split_path) == 2:
|
||||
subchannels = self.iter_resources(split_path[0:1])
|
||||
channel = None
|
||||
for subchannel in subchannels:
|
||||
# allow matching by title for backward compatibility (for now)
|
||||
if split_path[0] == subchannel.split_path[0] and \
|
||||
split_path[1] in (subchannel.split_path[1], subchannel.title):
|
||||
channel = subchannel
|
||||
if channel:
|
||||
self.location("http://service.canal-plus.com/video/rest/getMEAs/cplus/%s" % channel.id)
|
||||
assert self.is_on_page(VideoPage)
|
||||
for video in self.page.iter_channel():
|
||||
yield video
|
||||
else:
|
||||
raise CollectionNotFound(split_path)
|
||||
|
||||
return walk_res(path[1:], matches[0])
|
||||
|
||||
return walk_res(split_path, collections)
|
||||
else:
|
||||
raise CollectionNotFound(split_path)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright(C) 2010-2011 Nicolas Duhamel
|
||||
# Copyright(C) 2010-2012 Nicolas Duhamel, Laurent Bachelier
|
||||
#
|
||||
# This file is part of weboob.
|
||||
#
|
||||
|
|
@ -26,24 +26,19 @@ __all__ = ['InitPage']
|
|||
|
||||
|
||||
class InitPage(BasePage):
|
||||
def on_loaded(self):
|
||||
self.collections = []
|
||||
|
||||
def do(_id):
|
||||
self.browser.location("http://service.canal-plus.com/video/rest/getMEAs/cplus/%s" % _id)
|
||||
return self.browser.page.iter_channel()
|
||||
|
||||
# Parse the list of channels
|
||||
def get_channels(self):
|
||||
"""
|
||||
Extract all possible channels (paths) from the page
|
||||
"""
|
||||
channels = list()
|
||||
for elem in self.document[2].getchildren():
|
||||
children = []
|
||||
for e in elem.getchildren():
|
||||
if e.tag == "NOM":
|
||||
_id = e.text.strip()
|
||||
name = e.text.strip()
|
||||
channels.append(Collection([name]))
|
||||
elif e.tag == "SELECTIONS":
|
||||
for select in e:
|
||||
sub = Collection(_id=select[0].text,
|
||||
title=select[1].text.strip(),
|
||||
fct=do)
|
||||
children.append(sub)
|
||||
coll = Collection(_id, children=children)
|
||||
self.collections.append(coll)
|
||||
sub = Collection([name, select[0].text],
|
||||
title=select[1].text.strip())
|
||||
channels.append(sub)
|
||||
return channels
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue