id decoration belongs to formatter
This commit is contained in:
parent
cc610a5e12
commit
a0f8137fc5
5 changed files with 28 additions and 23 deletions
|
|
@ -51,7 +51,7 @@ class VideoPage(BasePage):
|
|||
continue
|
||||
for m in re.finditer(self.VIDEO_SIGNATURE_REGEX, data.text):
|
||||
video_signature = m.group(1)
|
||||
return 'http://www.youtube.com/get_video?video_id=%s&t=%s&fmt=18' % (self.video.provider_id, video_signature)
|
||||
return 'http://www.youtube.com/get_video?video_id=%s&t=%s&fmt=18' % (self.video.id, video_signature)
|
||||
|
||||
def get_title(self):
|
||||
found = self.document.getroot().cssselect('meta[name=title]')
|
||||
|
|
|
|||
|
|
@ -47,11 +47,7 @@ class BaseVideo(object):
|
|||
|
||||
@property
|
||||
def page_url(self):
|
||||
return self.id2url(self.provider_id)
|
||||
|
||||
@property
|
||||
def provider_id(self):
|
||||
return self.id.split('@')[0]
|
||||
return self.id2url(self.id)
|
||||
|
||||
|
||||
class ICapVideo(ICap):
|
||||
|
|
|
|||
|
|
@ -45,11 +45,10 @@ class Videoob(ConsoleApplication):
|
|||
continue
|
||||
if video is None:
|
||||
continue
|
||||
self.format(video)
|
||||
self.format(video, backend.name)
|
||||
|
||||
@ConsoleApplication.command('Search videos')
|
||||
def command_search(self, pattern=None):
|
||||
print (u'Search pattern: %s' % pattern if pattern else u'Last videos').encode('utf-8')
|
||||
for backend, video in self.weboob.do('iter_search_results', pattern=pattern, nsfw=self.options.nsfw):
|
||||
video.id = self.join_id(video.provider_id, backend.name)
|
||||
self.format(video)
|
||||
self.format(video, backend.name)
|
||||
|
|
|
|||
|
|
@ -200,9 +200,9 @@ class ConsoleApplication(BaseApplication):
|
|||
def command(doc_string, f=register_command):
|
||||
return partial(f, doc_string=doc_string)
|
||||
|
||||
def format(self, result):
|
||||
def format(self, result, backend_name):
|
||||
try:
|
||||
self.formatter.format(result, selected_fields=self.selected_fields, condition=self.condition)
|
||||
self.formatter.format(result, backend_name, selected_fields=self.selected_fields, condition=self.condition)
|
||||
except ResultsConditionException, e:
|
||||
logging.error(e)
|
||||
|
||||
|
|
@ -229,6 +229,3 @@ class ConsoleApplication(BaseApplication):
|
|||
except ValueError:
|
||||
backend_name = None
|
||||
return _id, backend_name
|
||||
|
||||
def join_id(self, provider_id, backend_name):
|
||||
return u'%s@%s' % (provider_id, backend_name)
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ class IFormatter(object):
|
|||
def flush(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
def format(self, obj, selected_fields=None, condition=None, return_only=False):
|
||||
def format(self, obj, backend_name, selected_fields=None, condition=None, return_only=False):
|
||||
"""
|
||||
Format an object to be human-readable.
|
||||
An object has fields which can be selected, and the objects
|
||||
|
|
@ -45,9 +45,9 @@ class IFormatter(object):
|
|||
@param condition [Condition] condition to objects to display
|
||||
@return a string of the formatted object
|
||||
"""
|
||||
item = self.to_dict(obj, condition)
|
||||
if selected_fields is not None:
|
||||
item = dict((k, v) for k, v in item.iteritems() if k in selected_fields)
|
||||
item = self.to_dict(obj, backend_name, condition, selected_fields)
|
||||
if item is None:
|
||||
return None
|
||||
formatted = self.format_dict(item=item)
|
||||
if not return_only and formatted:
|
||||
self.after_format(formatted)
|
||||
|
|
@ -72,10 +72,23 @@ class IFormatter(object):
|
|||
if not isinstance(attribute, types.MethodType):
|
||||
yield attribute_name, attribute
|
||||
|
||||
def to_dict(self, obj, condition=None):
|
||||
def to_dict(self, obj, backend_name, condition=None, selected_fields=None):
|
||||
def iter_select_and_decorate(d):
|
||||
if hasattr(obj, '__id__'):
|
||||
id_attr = getattr(obj, '__id__')
|
||||
if not isinstance(id_attr, (set, list, tuple)):
|
||||
id_attr = (id_attr,)
|
||||
id_fields = id_attr
|
||||
else:
|
||||
id_fields = ('id',)
|
||||
for k, v in d:
|
||||
if selected_fields is not None and k not in selected_fields:
|
||||
continue
|
||||
if k in id_fields:
|
||||
v += u'@%s' % backend_name
|
||||
yield k, v
|
||||
fields_iterator = obj.iter_fields() if hasattr(obj, 'iter_fields') else self.iter_fields(obj)
|
||||
d = dict((k, v) for k, v in fields_iterator)
|
||||
if condition is not None:
|
||||
if not condition.is_valid(d):
|
||||
d = None
|
||||
d = dict((k, v) for k, v in iter_select_and_decorate(fields_iterator))
|
||||
if condition is not None and not condition.is_valid(d):
|
||||
d = None
|
||||
return d
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue