# Author: Gergely Imreh
# Author: Philipp Hagemeister <phihag@phihag.de>
# License: Public domain code
-from __future__ import with_statement
-import contextlib
import cookielib
-import ctypes
import datetime
-import email.utils
import gzip
import htmlentitydefs
import httplib
import warnings
import zlib
+if os.name == 'nt':
+ import ctypes
+
+try:
+ import email.utils
+except ImportError: # Python 2.4
+ import email.Utils
try:
import cStringIO as StringIO
except ImportError:
try:
descfn = filename + '.description'
self.report_writedescription(descfn)
- with contextlib.closing(open(descfn, 'wb')) as descfile:
+ descfile = open(descfn, 'wb')
+ try:
descfile.write(info_dict['description'].encode('utf-8'))
+ finally:
+ descfile.close()
except (OSError, IOError):
self.trouble(u'ERROR: Cannot write description file: %s' % str(descfn))
return
self.trouble(u'ERROR: No JSON encoder found. Update to Python 2.6+, setup a json module, or leave out --write-info-json.')
return
try:
- with contextlib.closing(open(infofn, 'wb')) as infof:
+ infof = open(infofn, 'wb')
+ try:
json.dump(info_dict, infof)
+ finally:
+ infof.close()
except (OSError, IOError):
self.trouble(u'ERROR: Cannot write metadata to JSON file: %s' % str(infofn))
return
class YoutubeIE(InfoExtractor):
"""Information extractor for youtube.com."""
- _VALID_URL = r'^((?:https?://)?(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/)(?:(?:(?:v|embed|e)/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\?|#!?)(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
+ _VALID_URL = r'^((?:https?://)?(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/)(?:(?:(?:v|embed|e)/)|(?:(?:watch(?:_popup)?(?:\.php)?)?(?:\?|#!?)(?:.+&)?v=))?)?([0-9A-Za-z_-]+)(?(1).+)?$'
_LANG_URL = r'http://www.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
_LOGIN_URL = 'https://www.youtube.com/signup?next=/&gl=US&hl=en'
_AGE_URL = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
# Decide which formats to download
req_format = self._downloader.params.get('format', None)
- raw_map = None
- if 'fmt_url_map' in video_info and len(video_info['fmt_url_map']) >= 1:
- raw_map = video_info['fmt_url_map'][0]
- elif 'fmt_stream_map' in video_info and len(video_info['fmt_stream_map']) >= 1:
- raw_map = video_info['fmt_stream_map'][0]
-
- if raw_map is not None:
- url_map = dict(tuple(pair.split('|')[:2]) for pair in raw_map.split(','))
+ if 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
+ self.report_rtmp_download()
+ video_url_list = [(None, video_info['conn'][0])]
+ elif 'url_encoded_fmt_stream_map' in video_info and len(video_info['url_encoded_fmt_stream_map']) >= 1:
+ url_data_strs = video_info['url_encoded_fmt_stream_map'][0].split(',')
+ url_data = [parse_qs(uds) for uds in url_data_strs]
+ url_data = filter(lambda ud: 'itag' in ud and 'url' in ud, url_data)
+ url_map = dict((ud['itag'][0], ud['url'][0]) for ud in url_data)
+
format_limit = self._downloader.params.get('format_limit', None)
if format_limit is not None and format_limit in self._available_formats:
format_list = self._available_formats[self._available_formats.index(format_limit):]
self._downloader.trouble(u'ERROR: requested format not available')
return
video_url_list = [(req_format, url_map[req_format])] # Specific format
-
- elif 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
- self.report_rtmp_download()
- video_url_list = [(None, video_info['conn'][0])]
-
else:
- self._downloader.trouble(u'ERROR: no fmt_url_map or conn information found in video info')
+ self._downloader.trouble(u'ERROR: no conn or url_encoded_fmt_stream_map information found in video info')
return
for format_param, video_real_url in video_url_list:
# Extension
video_extension = self._video_extensions.get(format_param, 'flv')
- # Find the video URL in fmt_url_map or conn paramters
try:
# Process video information
self._downloader.process_info({
# Extension
video_extension = self._video_extensions.get(format_param, 'mp4')
- # Find the video URL in fmt_url_map or conn paramters
try:
# Process video information
self._downloader.process_info({
self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
return
- json_url = url + ('&' if '?' in url else '?') + 'skin=json&version=2&no_wrap=1'
+ if '?' in url:
+ cchar = '&'
+ else:
+ cchar = '?'
+ json_url = url + cchar + 'skin=json&version=2&no_wrap=1'
request = urllib2.Request(json_url)
self.report_extraction(mobj.group(1))
try:
return
try:
json_data = json.loads(json_code)
- data = json_data['Post'] if 'Post' in json_data else json_data
+ if 'Post' in json_data:
+ 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']