merge upstream
[youtube-dl] / youtube_dl / InfoExtractors.py
index 4314f14022b650cb965707ee5c90e07fbecd67c4..01201a145404deadfb74066d47a7296bf3ad2b5b 100644 (file)
@@ -12,28 +12,14 @@ import time
 import urllib
 import urllib2
 import email.utils
+import xml.etree.ElementTree
+from urlparse import parse_qs
 
 try:
        import cStringIO as StringIO
 except ImportError:
        import StringIO
 
-# parse_qs was moved from the cgi module to the urlparse module recently.
-try:
-       from urlparse import parse_qs
-except ImportError:
-       from cgi import parse_qs
-
-try:
-       import lxml.etree
-except ImportError:
-       pass # Handled below
-
-try:
-       import xml.etree.ElementTree
-except ImportError: # Python<2.5: Not officially supported, but let it slip
-       warnings.warn('xml.etree.ElementTree support is missing. Consider upgrading to Python >= 2.5 if you get related errors.')
-
 from utils import *
 
 
@@ -117,8 +103,8 @@ class YoutubeIE(InfoExtractor):
        _NEXT_URL_RE = r'[\?&]next_url=([^&]+)'
        _NETRC_MACHINE = 'youtube'
        # Listed in order of quality
-       _available_formats = ['38', '37', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13']
-       _available_formats_prefer_free = ['38', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13']
+       _available_formats = ['38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13']
+       _available_formats_prefer_free = ['38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13']
        _video_extensions = {
                '13': '3gp',
                '17': 'mp4',
@@ -129,6 +115,7 @@ class YoutubeIE(InfoExtractor):
                '43': 'webm',
                '44': 'webm',
                '45': 'webm',
+               '46': 'webm',
        }
        _video_dimensions = {
                '5': '240x400',
@@ -144,6 +131,7 @@ class YoutubeIE(InfoExtractor):
                '43': '360x640',
                '44': '480x854',
                '45': '720x1280',
+               '46': '1080x1920',
        }       
        IE_NAME = u'youtube'
 
@@ -193,8 +181,8 @@ class YoutubeIE(InfoExtractor):
                        end = start + float(dur)
                        start = "%02i:%02i:%02i,%03i" %(start/(60*60), start/60%60, start%60, start%1*1000)
                        end = "%02i:%02i:%02i,%03i" %(end/(60*60), end/60%60, end%60, end%1*1000)
-                       caption = re.sub(ur'(?u)&(.+?);', htmlentity_transform, caption)
-                       caption = re.sub(ur'(?u)&(.+?);', htmlentity_transform, caption) # double cycle, inentional
+                       caption = unescapeHTML(caption)
+                       caption = unescapeHTML(caption) # double cycle, inentional
                        srt += str(n) + '\n'
                        srt += start + ' --> ' + end + '\n'
                        srt += caption + '\n\n'
@@ -364,18 +352,9 @@ class YoutubeIE(InfoExtractor):
                                        pass
 
                # description
-               try:
-                       lxml.etree
-               except NameError:
-                       video_description = u'No description available.'
-                       mobj = re.search(r'<meta name="description" content="(.*?)">', video_webpage)
-                       if mobj is not None:
-                               video_description = mobj.group(1).decode('utf-8')
-               else:
-                       html_parser = lxml.etree.HTMLParser(encoding='utf-8')
-                       vwebpage_doc = lxml.etree.parse(StringIO.StringIO(video_webpage), html_parser)
-                       video_description = u''.join(vwebpage_doc.xpath('id("eow-description")//text()'))
-                       # TODO use another parser
+               video_description = get_element_by_id("eow-description", video_webpage.decode('utf8'))
+               if video_description: video_description = clean_html(video_description)
+               else: video_description = ''
                        
                # closed captions
                video_subtitles = None
@@ -992,7 +971,7 @@ class YahooIE(InfoExtractor):
                        self._downloader.trouble(u'ERROR: Unable to extract media URL')
                        return
                video_url = urllib.unquote(mobj.group(1) + mobj.group(2)).decode('utf-8')
-               video_url = re.sub(r'(?u)&(.+?);', htmlentity_transform, video_url)
+               video_url = unescapeHTML(video_url)
 
                return [{
                        'id':           video_id.decode('utf-8'),
@@ -1069,18 +1048,9 @@ class VimeoIE(InfoExtractor):
                video_thumbnail = config["video"]["thumbnail"]
 
                # Extract video description
-               try:
-                       lxml.etree
-               except NameError:
-                       video_description = u'No description available.'
-                       mobj = re.search(r'<meta name="description" content="(.*?)" />', webpage, re.MULTILINE)
-                       if mobj is not None:
-                               video_description = mobj.group(1)
-               else:
-                       html_parser = lxml.etree.HTMLParser()
-                       vwebpage_doc = lxml.etree.parse(StringIO.StringIO(webpage), html_parser)
-                       video_description = u''.join(vwebpage_doc.xpath('id("description")//text()')).strip()
-                       # TODO use another parser
+               video_description = get_element_by_id("description", webpage.decode('utf8'))
+               if video_description: video_description = clean_html(video_description)
+               else: video_description = ''
 
                # Extract upload date
                video_upload_date = u'NA'
@@ -2009,14 +1979,14 @@ class BlipTVIE(InfoExtractor):
                                        data = json_data['Post']
                                else:
                                        data = json_data
-       
+
                                upload_date = datetime.datetime.strptime(data['datestamp'], '%m-%d-%y %H:%M%p').strftime('%Y%m%d')
                                video_url = data['media']['url']
                                umobj = re.match(self._URL_EXT, video_url)
                                if umobj is None:
                                        raise ValueError('Can not determine filename extension')
                                ext = umobj.group(1)
-       
+
                                info = {
                                        'id': data['item_id'],
                                        'url': video_url,
@@ -2109,7 +2079,7 @@ class ComedyCentralIE(InfoExtractor):
 
        def report_extraction(self, episode_id):
                self._downloader.to_screen(u'[comedycentral] %s: Extracting information' % episode_id)
-       
+
        def report_config_download(self, episode_id):
                self._downloader.to_screen(u'[comedycentral] %s: Downloading configuration' % episode_id)
 
@@ -2248,8 +2218,6 @@ class EscapistIE(InfoExtractor):
                self._downloader.to_screen(u'[escapist] %s: Downloading configuration' % showName)
 
        def _real_extract(self, url):
-               htmlParser = HTMLParser.HTMLParser()
-
                mobj = re.match(self._VALID_URL, url)
                if mobj is None:
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
@@ -2265,11 +2233,11 @@ class EscapistIE(InfoExtractor):
                        return
 
                descMatch = re.search('<meta name="description" content="([^"]*)"', webPage)
-               description = htmlParser.unescape(descMatch.group(1))
+               description = unescapeHTML(descMatch.group(1))
                imgMatch = re.search('<meta property="og:image" content="([^"]*)"', webPage)
-               imgUrl = htmlParser.unescape(imgMatch.group(1))
+               imgUrl = unescapeHTML(imgMatch.group(1))
                playerUrlMatch = re.search('<meta property="og:video" content="([^"]*)"', webPage)
-               playerUrl = htmlParser.unescape(playerUrlMatch.group(1))
+               playerUrl = unescapeHTML(playerUrlMatch.group(1))
                configUrlMatch = re.search('config=(.*)$', playerUrl)
                configUrl = urllib2.unquote(configUrlMatch.group(1))
 
@@ -2324,8 +2292,6 @@ class CollegeHumorIE(InfoExtractor):
                self._downloader.to_screen(u'[%s] %s: Extracting information' % (self.IE_NAME, video_id))
 
        def _real_extract(self, url):
-               htmlParser = HTMLParser.HTMLParser()
-
                mobj = re.match(self._VALID_URL, url)
                if mobj is None:
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
@@ -2391,8 +2357,6 @@ class XVideosIE(InfoExtractor):
                self._downloader.to_screen(u'[%s] %s: Extracting information' % (self.IE_NAME, video_id))
 
        def _real_extract(self, url):
-               htmlParser = HTMLParser.HTMLParser()
-
                mobj = re.match(self._VALID_URL, url)
                if mobj is None:
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
@@ -2475,8 +2439,6 @@ class SoundcloudIE(InfoExtractor):
                self._downloader.to_screen(u'[%s] %s: Extracting information' % (self.IE_NAME, video_id))
 
        def _real_extract(self, url):
-               htmlParser = HTMLParser.HTMLParser()
-
                mobj = re.match(self._VALID_URL, url)
                if mobj is None:
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
@@ -2561,8 +2523,6 @@ class InfoQIE(InfoExtractor):
                self._downloader.to_screen(u'[%s] %s: Extracting information' % (self.IE_NAME, video_id))
 
        def _real_extract(self, url):
-               htmlParser = HTMLParser.HTMLParser()
-
                mobj = re.match(self._VALID_URL, url)
                if mobj is None:
                        self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
@@ -2647,7 +2607,6 @@ class MixcloudIE(InfoExtractor):
                        url_list = jsonData[fmt][bitrate]
                except TypeError: # we have no bitrate info.
                        url_list = jsonData[fmt]
-                               
                return url_list
 
        def check_urls(self, url_list):
@@ -2761,7 +2720,7 @@ class StanfordOpenClassroomIE(InfoExtractor):
                        info = {
                                'id': simplify_title(course + '_' + video),
                        }
-       
+
                        self.report_extraction(info['id'])
                        baseUrl = 'http://openclassroom.stanford.edu/MainFolder/courses/' + course + '/videos/'
                        xmlUrl = baseUrl + video + '.xml'
@@ -2782,8 +2741,6 @@ class StanfordOpenClassroomIE(InfoExtractor):
                        info['format'] = info['ext']
                        return [info]
                elif mobj.group('course'): # A course page
-                       unescapeHTML = HTMLParser.HTMLParser().unescape
-
                        course = mobj.group('course')
                        info = {
                                'id': simplify_title(course),
@@ -2822,8 +2779,6 @@ class StanfordOpenClassroomIE(InfoExtractor):
                        return results
                        
                else: # Root page
-                       unescapeHTML = HTMLParser.HTMLParser().unescape
-
                        info = {
                                'id': 'Stanford OpenClassroom',
                                'type': 'playlist',