import urllib
import urllib2
-std_headers = {
+std_headers = {
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
def suitable(url):
return (re.match(YoutubeIE._VALID_URL, url) is not None)
+ @staticmethod
+ def htmlentity_transform(matchobj):
+ """Transforms an HTML entity to a Unicode character."""
+ entity = matchobj.group(1)
+
+ # Known non-numeric HTML entity
+ if entity in htmlentitydefs.name2codepoint:
+ return unichr(htmlentitydefs.name2codepoint[entity])
+
+ # Unicode character
+ mobj = re.match(ur'(?u)#(x?\d+)', entity)
+ if mobj is not None:
+ numstr = mobj.group(1)
+ if numstr.startswith(u'x'):
+ base = 16
+ numstr = u'0%s' % numstr
+ else:
+ base = 10
+ return unichr(long(numstr, base))
+
+ # Unknown entity in name, return its literal representation
+ return (u'&%s;' % entity)
+
def report_lang(self):
"""Report attempt to set language."""
self.to_stdout(u'[youtube] Setting language')
def report_video_url(self, video_id, video_real_url):
"""Report extracted video URL."""
self.to_stdout(u'[youtube] %s: URL: %s' % (video_id, video_real_url))
-
+
def _real_initialize(self):
if self._downloader is None:
return
format_param = params.get('format', None)
# Extension
- video_extension = {'18': 'mp4', '17': '3gp'}.get(format_param, 'flv')
+ video_extension = {
+ '17': '3gp',
+ '18': 'mp4',
+ '22': 'mp4',
+ }.get(format_param, 'flv')
# Normalize URL, including format
normalized_url = 'http://www.youtube.com/watch?v=%s&gl=US&hl=en' % video_id
self.to_stderr(u'ERROR: unable to extract video title')
return [None]
video_title = mobj.group(1).decode('utf-8')
- video_title = re.sub(ur'(?u)&(.+?);', lambda x: unichr(htmlentitydefs.name2codepoint[x.group(1)]), video_title)
+ video_title = re.sub(ur'(?u)&(.+?);', self.htmlentity_transform, video_title)
video_title = video_title.replace(os.sep, u'%')
# simplified title
_MORE_PAGES_INDICATOR = r'>Next</a>'
_youtube_ie = None
- def __init__(self, youtube_ie, downloader=None):
+ def __init__(self, youtube_ie, downloader=None):
InfoExtractor.__init__(self, downloader)
self._youtube_ie = youtube_ie
prefix, query = query.split(':')
prefix = prefix[8:]
- if prefix == '':
+ if prefix == '':
return self._download_n_results(query, 1)
- elif prefix == 'all':
+ elif prefix == 'all':
return self._download_n_results(query, -1)
- else:
+ else:
try:
n = int(prefix)
if n <= 0:
action='store_true', dest='gettitle', help='simulate, quiet but print title', default=False)
parser.add_option('-f', '--format',
dest='format', metavar='FMT', help='video format code')
- parser.add_option('-b', '--best-quality',
- action='store_const', dest='format', help='alias for -f 18', const='18')
parser.add_option('-m', '--mobile-version',
action='store_const', dest='format', help='alias for -f 17', const='17')
+ parser.add_option('-d', '--high-def',
+ action='store_const', dest='format', help='alias for -f 22', const='22')
parser.add_option('-i', '--ignore-errors',
action='store_true', dest='ignoreerrors', help='continue on download errors', default=False)
parser.add_option('-r', '--rate-limit',