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
|
continue
|
||||||
for m in re.finditer(self.VIDEO_SIGNATURE_REGEX, data.text):
|
for m in re.finditer(self.VIDEO_SIGNATURE_REGEX, data.text):
|
||||||
video_signature = m.group(1)
|
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):
|
def get_title(self):
|
||||||
found = self.document.getroot().cssselect('meta[name=title]')
|
found = self.document.getroot().cssselect('meta[name=title]')
|
||||||
|
|
|
||||||
|
|
@ -47,11 +47,7 @@ class BaseVideo(object):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def page_url(self):
|
def page_url(self):
|
||||||
return self.id2url(self.provider_id)
|
return self.id2url(self.id)
|
||||||
|
|
||||||
@property
|
|
||||||
def provider_id(self):
|
|
||||||
return self.id.split('@')[0]
|
|
||||||
|
|
||||||
|
|
||||||
class ICapVideo(ICap):
|
class ICapVideo(ICap):
|
||||||
|
|
|
||||||
|
|
@ -45,11 +45,10 @@ class Videoob(ConsoleApplication):
|
||||||
continue
|
continue
|
||||||
if video is None:
|
if video is None:
|
||||||
continue
|
continue
|
||||||
self.format(video)
|
self.format(video, backend.name)
|
||||||
|
|
||||||
@ConsoleApplication.command('Search videos')
|
@ConsoleApplication.command('Search videos')
|
||||||
def command_search(self, pattern=None):
|
def command_search(self, pattern=None):
|
||||||
print (u'Search pattern: %s' % pattern if pattern else u'Last videos').encode('utf-8')
|
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):
|
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, backend.name)
|
||||||
self.format(video)
|
|
||||||
|
|
|
||||||
|
|
@ -200,9 +200,9 @@ class ConsoleApplication(BaseApplication):
|
||||||
def command(doc_string, f=register_command):
|
def command(doc_string, f=register_command):
|
||||||
return partial(f, doc_string=doc_string)
|
return partial(f, doc_string=doc_string)
|
||||||
|
|
||||||
def format(self, result):
|
def format(self, result, backend_name):
|
||||||
try:
|
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:
|
except ResultsConditionException, e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
|
|
||||||
|
|
@ -229,6 +229,3 @@ class ConsoleApplication(BaseApplication):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
backend_name = None
|
backend_name = None
|
||||||
return _id, backend_name
|
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):
|
def flush(self):
|
||||||
raise NotImplementedError()
|
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.
|
Format an object to be human-readable.
|
||||||
An object has fields which can be selected, and the objects
|
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
|
@param condition [Condition] condition to objects to display
|
||||||
@return a string of the formatted object
|
@return a string of the formatted object
|
||||||
"""
|
"""
|
||||||
item = self.to_dict(obj, condition)
|
item = self.to_dict(obj, backend_name, condition, selected_fields)
|
||||||
if selected_fields is not None:
|
if item is None:
|
||||||
item = dict((k, v) for k, v in item.iteritems() if k in selected_fields)
|
return None
|
||||||
formatted = self.format_dict(item=item)
|
formatted = self.format_dict(item=item)
|
||||||
if not return_only and formatted:
|
if not return_only and formatted:
|
||||||
self.after_format(formatted)
|
self.after_format(formatted)
|
||||||
|
|
@ -72,10 +72,23 @@ class IFormatter(object):
|
||||||
if not isinstance(attribute, types.MethodType):
|
if not isinstance(attribute, types.MethodType):
|
||||||
yield attribute_name, attribute
|
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)
|
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)
|
d = dict((k, v) for k, v in iter_select_and_decorate(fields_iterator))
|
||||||
if condition is not None:
|
if condition is not None and not condition.is_valid(d):
|
||||||
if not condition.is_valid(d):
|
d = None
|
||||||
d = None
|
|
||||||
return d
|
return d
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue