diff options
Diffstat (limited to 'youtube_dl/extractor/common.py')
| -rw-r--r-- | youtube_dl/extractor/common.py | 46 | 
1 files changed, 45 insertions, 1 deletions
diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index 60cab6f4e..450c7dfd6 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -1,6 +1,7 @@  from __future__ import unicode_literals  import base64 +import datetime  import hashlib  import json  import netrc @@ -21,6 +22,7 @@ from ..utils import (      clean_html,      compiled_regex_type,      ExtractorError, +    float_or_none,      int_or_none,      RegexNotFoundError,      sanitize_filename, @@ -136,6 +138,8 @@ class InfoExtractor(object):      Unless mentioned otherwise, the fields should be Unicode strings. +    Unless mentioned otherwise, None is equivalent to absence of information. +      Subclasses of this one should re-define the _real_initialize() and      _real_extract() methods and define a _VALID_URL regexp.      Probably, they should also be added to the list of extractors. @@ -165,6 +169,14 @@ class InfoExtractor(object):          return cls._VALID_URL_RE.match(url) is not None      @classmethod +    def _match_id(cls, url): +        if '_VALID_URL_RE' not in cls.__dict__: +            cls._VALID_URL_RE = re.compile(cls._VALID_URL) +        m = cls._VALID_URL_RE.match(url) +        assert m +        return m.group('id') + +    @classmethod      def working(cls):          """Getter method for _WORKING."""          return cls._WORKING @@ -324,7 +336,11 @@ class InfoExtractor(object):          try:              return json.loads(json_string)          except ValueError as ve: -            raise ExtractorError('Failed to download JSON', cause=ve) +            errmsg = '%s: Failed to parse JSON ' % video_id +            if fatal: +                raise ExtractorError(errmsg, cause=ve) +            else: +                self.report_warning(errmsg + str(ve))      def report_warning(self, msg, video_id=None):          idstr = '' if video_id is None else '%s: ' % video_id @@ -705,6 +721,34 @@ class InfoExtractor(object):          self._sort_formats(formats)          return formats +    def _live_title(self, name): +        """ Generate the title for a live video """ +        now = datetime.datetime.now() +        now_str = now.strftime("%Y-%m-%d %H:%M") +        return name + ' ' + now_str + +    def _int(self, v, name, fatal=False, **kwargs): +        res = int_or_none(v, **kwargs) +        if 'get_attr' in kwargs: +            print(getattr(v, kwargs['get_attr'])) +        if res is None: +            msg = 'Failed to extract %s: Could not parse value %r' % (name, v) +            if fatal: +                raise ExtractorError(msg) +            else: +                self._downloader.report_warning(msg) +        return res + +    def _float(self, v, name, fatal=False, **kwargs): +        res = float_or_none(v, **kwargs) +        if res is None: +            msg = 'Failed to extract %s: Could not parse value %r' % (name, v) +            if fatal: +                raise ExtractorError(msg) +            else: +                self._downloader.report_warning(msg) +        return res +  class SearchInfoExtractor(InfoExtractor):      """  | 
