X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2FInfoExtractors.py;h=5d54e93e7808c630fe1519d7c9a06a32da88cbfd;hb=8409501206e37d57f01e5fe72bfc54a5562e4e0a;hp=a6294e1e1823bb3c47a8860f6986c7a09f57c92f;hpb=951b9dfd943a86843efb92afc6c2251efa3b4f1a;p=youtube-dl
diff --git a/youtube_dl/InfoExtractors.py b/youtube_dl/InfoExtractors.py
index a6294e1e1..5d54e93e7 100755
--- a/youtube_dl/InfoExtractors.py
+++ b/youtube_dl/InfoExtractors.py
@@ -191,6 +191,38 @@ class InfoExtractor(object):
video_info['title'] = playlist_title
return video_info
+ def _search_regex(self, pattern, string, name, default=None, fatal=True, flags=0):
+ """
+ Perform a regex search on the given string, using a single or a list of
+ patterns returning the first matching group.
+ In case of failure return a default value or raise a WARNING or a
+ ExtractorError, depending on fatal, specifying the field name.
+ """
+ if isinstance(pattern, (str, compat_str, compiled_regex_type)):
+ mobj = re.search(pattern, string, flags)
+ else:
+ for p in pattern:
+ mobj = re.search(p, string, flags)
+ if mobj: break
+
+ if sys.stderr.isatty() and os.name != 'nt':
+ _name = u'\033[0;34m%s\033[0m' % name
+ else:
+ _name = name
+
+ if mobj:
+ # return the first matching group
+ return next(g for g in mobj.groups() if g is not None)
+ elif default is not None:
+ return default
+ elif fatal:
+ raise ExtractorError(u'Unable to extract %s; '
+ u'please report this issue on GitHub.' % _name)
+ else:
+ self._downloader.report_warning(u'unable to extract %s; '
+ u'please report this issue on GitHub.' % _name)
+ return None
+
class SearchInfoExtractor(InfoExtractor):
"""
Base class for paged search queries extractors.
@@ -964,18 +996,13 @@ class PhotobucketIE(InfoExtractor):
}]
# We try looking in other parts of the webpage
- mobj = re.search(r'', webpage)
- if mobj is None:
- raise ExtractorError(u'Unable to extract media URL')
- mediaURL = compat_urllib_parse.unquote(mobj.group(1))
-
- video_url = mediaURL
+ video_url = self._search_regex(r'',
+ webpage, u'video URL')
mobj = re.search(r'
(.*) video by (.*) - Photobucket', webpage)
if mobj is None:
raise ExtractorError(u'Unable to extract title')
video_title = mobj.group(1).decode('utf-8')
-
video_uploader = mobj.group(2).decode('utf-8')
return [{
@@ -1803,10 +1830,7 @@ class DepositFilesIE(InfoExtractor):
file_extension = os.path.splitext(file_url)[1][1:]
# Search for file title
- mobj = re.search(r'', webpage)
- if mobj is None:
- raise ExtractorError(u'Unable to extract title')
- file_title = mobj.group(1).decode('utf-8')
+ file_title = self._search_regex(r'', webpage, u'title')
return [{
'id': file_id.decode('utf-8'),
@@ -1900,10 +1924,9 @@ class FacebookIE(InfoExtractor):
video_duration = int(video_data['video_duration'])
thumbnail = video_data['thumbnail_src']
- m = re.search('
([^<]+)
', webpage)
- if not m:
- raise ExtractorError(u'Cannot find title in webpage')
- video_title = unescapeHTML(m.group(1))
+ video_title = self._search_regex('
([^<]+)
',
+ webpage, u'title')
+ video_title = unescapeHTML(video_title)
info = {
'id': video_id,
@@ -2065,15 +2088,10 @@ class MyVideoIE(InfoExtractor):
self.report_extraction(video_id)
video_url = mobj.group(1) + '.flv'
- mobj = re.search('([^<]+)', webpage)
- if mobj is None:
- raise ExtractorError(u'Unable to extract title')
- video_title = mobj.group(1)
+ video_title = self._search_regex('([^<]+)',
+ webpage, u'title')
- mobj = re.search('[.](.+?)$', video_url)
- if mobj is None:
- raise ExtractorError(u'Unable to extract extention')
- video_ext = mobj.group(1)
+ video_ext = self._search_regex('[.](.+?)$', video_url, u'extension')
return [{
'id': video_id,
@@ -2121,25 +2139,23 @@ class MyVideoIE(InfoExtractor):
# extracting infos
self.report_extraction(video_id)
+ video_url = None
mobj = re.search('connectionurl=\'(.*?)\'', dec_data)
- if mobj is None:
- raise ExtractorError(u'unable to extract rtmpurl')
- video_rtmpurl = compat_urllib_parse.unquote(mobj.group(1))
- if 'myvideo2flash' in video_rtmpurl:
- self._downloader.report_warning(u'forcing RTMPT ...')
- video_rtmpurl = video_rtmpurl.replace('rtmpe://', 'rtmpt://')
-
- # extract non rtmp videos
- if (video_rtmpurl is None) or (video_rtmpurl == ''):
+ if mobj:
+ video_url = compat_urllib_parse.unquote(mobj.group(1))
+ if 'myvideo2flash' in video_url:
+ self._downloader.report_warning(u'forcing RTMPT ...')
+ video_url = video_url.replace('rtmpe://', 'rtmpt://')
+
+ if not video_url:
+ # extract non rtmp videos
mobj = re.search('path=\'(http.*?)\' source=\'(.*?)\'', dec_data)
if mobj is None:
raise ExtractorError(u'unable to extract url')
- video_rtmpurl = compat_urllib_parse.unquote(mobj.group(1)) + compat_urllib_parse.unquote(mobj.group(2))
+ video_url = compat_urllib_parse.unquote(mobj.group(1)) + compat_urllib_parse.unquote(mobj.group(2))
- mobj = re.search('source=\'(.*?)\'', dec_data)
- if mobj is None:
- raise ExtractorError(u'unable to extract swfobj')
- video_file = compat_urllib_parse.unquote(mobj.group(1))
+ video_file = self._search_regex('source=\'(.*?)\'', dec_data, u'video file')
+ video_file = compat_urllib_parse.unquote(video_file)
if not video_file.endswith('f4m'):
ppath, prefix = video_file.split('.')
@@ -2151,20 +2167,16 @@ class MyVideoIE(InfoExtractor):
video_filepath + video_file
).replace('.f4m', '.m3u8')
- mobj = re.search('swfobject.embedSWF\(\'(.+?)\'', webpage)
- if mobj is None:
- raise ExtractorError(u'unable to extract swfobj')
- video_swfobj = compat_urllib_parse.unquote(mobj.group(1))
+ video_swfobj = self._search_regex('swfobject.embedSWF\(\'(.+?)\'', webpage, u'swfobj')
+ video_swfobj = compat_urllib_parse.unquote(video_swfobj)
- mobj = re.search("
(.*?)
", webpage)
- if mobj is None:
- raise ExtractorError(u'unable to extract title')
- video_title = mobj.group(1)
+ video_title = self._search_regex("
(.*?)
",
+ webpage, u'title')
return [{
'id': video_id,
- 'url': video_rtmpurl,
- 'tc_url': video_rtmpurl,
+ 'url': video_url,
+ 'tc_url': video_url,
'uploader': None,
'upload_date': None,
'title': video_title,
@@ -2175,6 +2187,7 @@ class MyVideoIE(InfoExtractor):
'player_url': video_swfobj,
}]
+
class ComedyCentralIE(InfoExtractor):
"""Information extractor for The Daily Show and Colbert Report """
@@ -2357,16 +2370,22 @@ class EscapistIE(InfoExtractor):
videoId = mobj.group('episode')
self.report_extraction(showName)
- webPage = self._download_webpage(url, showName)
+ webpage = self._download_webpage(url, showName)
+
+ videoDesc = self._search_regex('(.*?)\s+-\s+XVID', webpage)
- if mobj is None:
- raise ExtractorError(u'Unable to extract video title')
- video_title = mobj.group(1)
-
+ video_title = self._search_regex(r'(.*?)\s+-\s+XVID',
+ webpage, u'title')
# Extract video thumbnail
- mobj = re.search(r'http://(?:img.*?\.)xvideos.com/videos/thumbs/[a-fA-F0-9]+/[a-fA-F0-9]+/[a-fA-F0-9]+/[a-fA-F0-9]+/([a-fA-F0-9.]+jpg)', webpage)
- if mobj is None:
- raise ExtractorError(u'Unable to extract video thumbnail')
- video_thumbnail = mobj.group(0)
+ video_thumbnail = self._search_regex(r'http://(?:img.*?\.)xvideos.com/videos/thumbs/[a-fA-F0-9]+/[a-fA-F0-9]+/[a-fA-F0-9]+/[a-fA-F0-9]+/([a-fA-F0-9.]+jpg)',
+ webpage, u'thumbnail', fatal=False)
info = {
'id': video_id,
@@ -2652,16 +2662,12 @@ class InfoQIE(InfoExtractor):
video_url = 'rtmpe://video.infoq.com/cfx/st/' + real_id
# Extract title
- mobj = re.search(r'contentTitle = "(.*?)";', webpage)
- if mobj is None:
- raise ExtractorError(u'Unable to extract video title')
- video_title = mobj.group(1)
+ video_title = self._search_regex(r'contentTitle = "(.*?)";',
+ webpage, u'title')
# Extract description
- video_description = u'No description available.'
- mobj = re.search(r'', webpage)
- if mobj is not None:
- video_description = mobj.group(1)
+ video_description = self._search_regex(r'',
+ webpage, u'description', fatal=False)
video_filename = video_url.split('/')[-1]
video_id, extension = video_filename.split('.')
@@ -2832,15 +2838,12 @@ class StanfordOpenClassroomIE(InfoExtractor):
note='Downloading course info page',
errnote='Unable to download course info page')
- m = re.search('