2 # -*- coding: utf-8 -*-
3 # Author: Ricardo Garcia Gonzalez
4 # Author: Danny Colligan
5 # Author: Benjamin Johnson
6 # License: Public domain code
23 # parse_qs was moved from the cgi module to the urlparse module recently.
25 from urlparse import parse_qs
27 from cgi import parse_qs
30 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6',
31 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
32 'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
33 'Accept-Language': 'en-us,en;q=0.5',
36 simple_title_chars = string.ascii_letters.decode('ascii') + string.digits.decode('ascii')
38 def preferredencoding():
39 """Get preferred encoding.
41 Returns the best encoding scheme for the system, based on
42 locale.getpreferredencoding() and some further tweaks.
44 def yield_preferredencoding():
46 pref = locale.getpreferredencoding()
52 return yield_preferredencoding().next()
54 def htmlentity_transform(matchobj):
55 """Transforms an HTML entity to a Unicode character.
57 This function receives a match object and is intended to be used with
58 the re.sub() function.
60 entity = matchobj.group(1)
62 # Known non-numeric HTML entity
63 if entity in htmlentitydefs.name2codepoint:
64 return unichr(htmlentitydefs.name2codepoint[entity])
67 mobj = re.match(ur'(?u)#(x?\d+)', entity)
69 numstr = mobj.group(1)
70 if numstr.startswith(u'x'):
72 numstr = u'0%s' % numstr
75 return unichr(long(numstr, base))
77 # Unknown entity in name, return its literal representation
78 return (u'&%s;' % entity)
80 def sanitize_title(utitle):
81 """Sanitizes a video title so it could be used as part of a filename."""
82 utitle = re.sub(ur'(?u)&(.+?);', htmlentity_transform, utitle)
83 return utitle.replace(unicode(os.sep), u'%')
85 def sanitize_open(filename, open_mode):
86 """Try to open the given filename, and slightly tweak it if this fails.
88 Attempts to open the given filename. If this fails, it tries to change
89 the filename slightly, step by step, until it's either able to open it
90 or it fails and raises a final exception, like the standard open()
93 It returns the tuple (stream, definitive_file_name).
96 stream = open(filename, open_mode)
97 return (stream, filename)
98 except (IOError, OSError), err:
99 # In case of error, try to remove win32 forbidden chars
100 filename = re.sub(ur'[<>:"\|\?\*]', u'#', filename)
102 # An exception here should be caught in the caller
103 stream = open(filename, open_mode)
104 return (stream, filename)
107 class DownloadError(Exception):
108 """Download Error exception.
110 This exception may be thrown by FileDownloader objects if they are not
111 configured to continue on errors. They will contain the appropriate
116 class SameFileError(Exception):
117 """Same File exception.
119 This exception will be thrown by FileDownloader objects if they detect
120 multiple files would have to be downloaded to the same file on disk.
124 class PostProcessingError(Exception):
125 """Post Processing exception.
127 This exception may be raised by PostProcessor's .run() method to
128 indicate an error in the postprocessing task.
132 class UnavailableFormatError(Exception):
133 """Unavailable Format exception.
135 This exception will be thrown when a video is requested
136 in a format that is not available for that video.
140 class ContentTooShortError(Exception):
141 """Content Too Short exception.
143 This exception may be raised by FileDownloader objects when a file they
144 download is too small for what the server announced first, indicating
145 the connection was probably interrupted.
151 def __init__(self, downloaded, expected):
152 self.downloaded = downloaded
153 self.expected = expected
155 class FileDownloader(object):
156 """File Downloader class.
158 File downloader objects are the ones responsible of downloading the
159 actual video file and writing it to disk if the user has requested
160 it, among some other tasks. In most cases there should be one per
161 program. As, given a video URL, the downloader doesn't know how to
162 extract all the needed information, task that InfoExtractors do, it
163 has to pass the URL to one of them.
165 For this, file downloader objects have a method that allows
166 InfoExtractors to be registered in a given order. When it is passed
167 a URL, the file downloader handles it to the first InfoExtractor it
168 finds that reports being able to handle it. The InfoExtractor extracts
169 all the information about the video or videos the URL refers to, and
170 asks the FileDownloader to process the video information, possibly
171 downloading the video.
173 File downloaders accept a lot of parameters. In order not to saturate
174 the object constructor with arguments, it receives a dictionary of
175 options instead. These options are available through the params
176 attribute for the InfoExtractors to use. The FileDownloader also
177 registers itself as the downloader in charge for the InfoExtractors
178 that are added to it, so this is a "mutual registration".
182 username: Username for authentication purposes.
183 password: Password for authentication purposes.
184 usenetrc: Use netrc for authentication instead.
185 quiet: Do not print messages to stdout.
186 forceurl: Force printing final URL.
187 forcetitle: Force printing title.
188 simulate: Do not download the video files.
189 format: Video format code.
190 outtmpl: Template for output names.
191 ignoreerrors: Do not stop on download errors.
192 ratelimit: Download speed limit, in bytes/sec.
193 nooverwrites: Prevent overwriting files.
194 continuedl: Try to continue downloads if possible.
200 _download_retcode = None
202 def __init__(self, params):
203 """Create a FileDownloader object with the given options."""
206 self._download_retcode = 0
210 def pmkdir(filename):
211 """Create directory components in filename. Similar to Unix "mkdir -p"."""
212 components = filename.split(os.sep)
213 aggregate = [os.sep.join(components[0:x]) for x in xrange(1, len(components))]
214 aggregate = ['%s%s' % (x, os.sep) for x in aggregate] # Finish names with separator
215 for dir in aggregate:
216 if not os.path.exists(dir):
220 def format_bytes(bytes):
223 if type(bytes) is str:
228 exponent = long(math.log(bytes, 1024.0))
229 suffix = 'bkMGTPEZY'[exponent]
230 converted = float(bytes) / float(1024**exponent)
231 return '%.2f%s' % (converted, suffix)
234 def calc_percent(byte_counter, data_len):
237 return '%6s' % ('%3.1f%%' % (float(byte_counter) / float(data_len) * 100.0))
240 def calc_eta(start, now, total, current):
244 if current == 0 or dif < 0.001: # One millisecond
246 rate = float(current) / dif
247 eta = long((float(total) - float(current)) / rate)
248 (eta_mins, eta_secs) = divmod(eta, 60)
251 return '%02d:%02d' % (eta_mins, eta_secs)
254 def calc_speed(start, now, bytes):
256 if bytes == 0 or dif < 0.001: # One millisecond
257 return '%10s' % '---b/s'
258 return '%10s' % ('%s/s' % FileDownloader.format_bytes(float(bytes) / dif))
261 def best_block_size(elapsed_time, bytes):
262 new_min = max(bytes / 2.0, 1.0)
263 new_max = min(max(bytes * 2.0, 1.0), 4194304) # Do not surpass 4 MB
264 if elapsed_time < 0.001:
266 rate = bytes / elapsed_time
274 def parse_bytes(bytestr):
275 """Parse a string indicating a byte quantity into a long integer."""
276 matchobj = re.match(r'(?i)^(\d+(?:\.\d+)?)([kMGTPEZY]?)$', bytestr)
279 number = float(matchobj.group(1))
280 multiplier = 1024.0 ** 'bkmgtpezy'.index(matchobj.group(2).lower())
281 return long(round(number * multiplier))
285 """Verify a URL is valid and data could be downloaded. Return real data URL."""
286 request = urllib2.Request(url, None, std_headers)
287 data = urllib2.urlopen(request)
293 def add_info_extractor(self, ie):
294 """Add an InfoExtractor object to the end of the list."""
296 ie.set_downloader(self)
298 def add_post_processor(self, pp):
299 """Add a PostProcessor object to the end of the chain."""
301 pp.set_downloader(self)
303 def to_stdout(self, message, skip_eol=False, ignore_encoding_errors=False):
304 """Print message to stdout if not in quiet mode."""
306 if not self.params.get('quiet', False):
307 print (u'%s%s' % (message, [u'\n', u''][skip_eol])).encode(preferredencoding()),
309 except (UnicodeEncodeError), err:
310 if not ignore_encoding_errors:
313 def to_stderr(self, message):
314 """Print message to stderr."""
315 print >>sys.stderr, message.encode(preferredencoding())
317 def fixed_template(self):
318 """Checks if the output template is fixed."""
319 return (re.search(ur'(?u)%\(.+?\)s', self.params['outtmpl']) is None)
321 def trouble(self, message=None):
322 """Determine action to take when a download problem appears.
324 Depending on if the downloader has been configured to ignore
325 download errors or not, this method may throw an exception or
326 not when errors are found, after printing the message.
328 if message is not None:
329 self.to_stderr(message)
330 if not self.params.get('ignoreerrors', False):
331 raise DownloadError(message)
332 self._download_retcode = 1
334 def slow_down(self, start_time, byte_counter):
335 """Sleep if the download speed is over the rate limit."""
336 rate_limit = self.params.get('ratelimit', None)
337 if rate_limit is None or byte_counter == 0:
340 elapsed = now - start_time
343 speed = float(byte_counter) / elapsed
344 if speed > rate_limit:
345 time.sleep((byte_counter - rate_limit * (now - start_time)) / rate_limit)
347 def report_destination(self, filename):
348 """Report destination filename."""
349 self.to_stdout(u'[download] Destination: %s' % filename, ignore_encoding_errors=True)
351 def report_progress(self, percent_str, data_len_str, speed_str, eta_str):
352 """Report download progress."""
353 self.to_stdout(u'\r[download] %s of %s at %s ETA %s' %
354 (percent_str, data_len_str, speed_str, eta_str), skip_eol=True)
356 def report_resuming_byte(self, resume_len):
357 """Report attemtp to resume at given byte."""
358 self.to_stdout(u'[download] Resuming download at byte %s' % resume_len)
360 def report_file_already_downloaded(self, file_name):
361 """Report file has already been fully downloaded."""
363 self.to_stdout(u'[download] %s has already been downloaded' % file_name)
364 except (UnicodeEncodeError), err:
365 self.to_stdout(u'[download] The file has already been downloaded')
367 def report_unable_to_resume(self):
368 """Report it was impossible to resume download."""
369 self.to_stdout(u'[download] Unable to resume')
371 def report_finish(self):
372 """Report download finished."""
375 def process_info(self, info_dict):
376 """Process a single dictionary returned by an InfoExtractor."""
377 # Do nothing else if in simulate mode
378 if self.params.get('simulate', False):
379 # Verify URL if it's an HTTP one
380 if info_dict['url'].startswith('http'):
382 info_dict['url'] = self.verify_url(info_dict['url'].encode('utf-8')).decode('utf-8')
383 except (OSError, IOError, urllib2.URLError, httplib.HTTPException, socket.error), err:
384 raise UnavailableFormatError
387 if self.params.get('forcetitle', False):
388 print info_dict['title'].encode(preferredencoding(), 'xmlcharrefreplace')
389 if self.params.get('forceurl', False):
390 print info_dict['url'].encode(preferredencoding(), 'xmlcharrefreplace')
395 template_dict = dict(info_dict)
396 template_dict['epoch'] = unicode(long(time.time()))
397 filename = self.params['outtmpl'] % template_dict
398 except (ValueError, KeyError), err:
399 self.trouble('ERROR: invalid output template or system charset: %s' % str(err))
400 if self.params.get('nooverwrites', False) and os.path.exists(filename):
401 self.to_stderr(u'WARNING: file exists: %s; skipping' % filename)
405 self.pmkdir(filename)
406 except (OSError, IOError), err:
407 self.trouble('ERROR: unable to create directories: %s' % str(err))
411 success = self._do_download(filename, info_dict['url'].encode('utf-8'))
412 except (OSError, IOError), err:
413 raise UnavailableFormatError
414 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
415 self.trouble('ERROR: unable to download video data: %s' % str(err))
417 except (ContentTooShortError, ), err:
418 self.trouble('ERROR: content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded))
423 self.post_process(filename, info_dict)
424 except (PostProcessingError), err:
425 self.trouble('ERROR: postprocessing: %s' % str(err))
428 def download(self, url_list):
429 """Download a given list of URLs."""
430 if len(url_list) > 1 and self.fixed_template():
431 raise SameFileError(self.params['outtmpl'])
434 suitable_found = False
436 # Go to next InfoExtractor if not suitable
437 if not ie.suitable(url):
440 # Suitable InfoExtractor found
441 suitable_found = True
443 # Extract information from URL and process it
446 # Suitable InfoExtractor had been found; go to next URL
449 if not suitable_found:
450 self.trouble('ERROR: no suitable InfoExtractor: %s' % url)
452 return self._download_retcode
454 def post_process(self, filename, ie_info):
455 """Run the postprocessing chain on the given file."""
457 info['filepath'] = filename
463 def _download_with_rtmpdump(self, filename, url):
464 self.report_destination(filename)
466 # Check for rtmpdump first
468 subprocess.call(['rtmpdump', '-h'], stdout=(file(os.path.devnull, 'w')), stderr=subprocess.STDOUT)
469 except (OSError, IOError):
470 self.trouble(u'ERROR: RTMP download detected but "rtmpdump" could not be run')
473 # Download using rtmpdump. rtmpdump returns exit code 2 when
474 # the connection was interrumpted and resuming appears to be
475 # possible. This is part of rtmpdump's normal usage, AFAIK.
476 basic_args = ['rtmpdump', '-q', '-r', url, '-o', filename]
477 retval = subprocess.call(basic_args + [[], ['-e', '-k', '1']][self.params.get('continuedl', False)])
478 while retval == 2 or retval == 1:
479 self.to_stdout(u'\r[rtmpdump] %s bytes' % os.path.getsize(filename), skip_eol=True)
480 time.sleep(2.0) # This seems to be needed
481 retval = subprocess.call(basic_args + ['-e'] + [[], ['-k', '1']][retval == 1])
483 self.to_stdout(u'\r[rtmpdump] %s bytes' % os.path.getsize(filename))
486 self.trouble('ERROR: rtmpdump exited with code %d' % retval)
489 def _do_download(self, filename, url):
490 # Attempt to download using rtmpdump
491 if url.startswith('rtmp'):
492 return self._download_with_rtmpdump(filename, url)
496 basic_request = urllib2.Request(url, None, std_headers)
497 request = urllib2.Request(url, None, std_headers)
499 # Establish possible resume length
500 if os.path.isfile(filename):
501 resume_len = os.path.getsize(filename)
505 # Request parameters in case of being able to resume
506 if self.params.get('continuedl', False) and resume_len != 0:
507 self.report_resuming_byte(resume_len)
508 request.add_header('Range','bytes=%d-' % resume_len)
511 # Establish connection
513 data = urllib2.urlopen(request)
514 except (urllib2.HTTPError, ), err:
515 if err.code != 416: # 416 is 'Requested range not satisfiable'
518 data = urllib2.urlopen(basic_request)
519 content_length = data.info()['Content-Length']
521 if content_length is not None and long(content_length) == resume_len:
522 # Because the file had already been fully downloaded
523 self.report_file_already_downloaded(filename)
526 # Because the server didn't let us
527 self.report_unable_to_resume()
530 data_len = data.info().get('Content-length', None)
531 data_len_str = self.format_bytes(data_len)
538 data_block = data.read(block_size)
540 data_block_len = len(data_block)
541 if data_block_len == 0:
543 byte_counter += data_block_len
545 # Open file just in time
548 (stream, filename) = sanitize_open(filename, open_mode)
549 self.report_destination(filename)
550 except (OSError, IOError), err:
551 self.trouble('ERROR: unable to open for writing: %s' % str(err))
553 stream.write(data_block)
554 block_size = self.best_block_size(after - before, data_block_len)
557 percent_str = self.calc_percent(byte_counter, data_len)
558 eta_str = self.calc_eta(start, time.time(), data_len, byte_counter)
559 speed_str = self.calc_speed(start, time.time(), byte_counter)
560 self.report_progress(percent_str, data_len_str, speed_str, eta_str)
563 self.slow_down(start, byte_counter)
566 if data_len is not None and str(byte_counter) != data_len:
567 raise ContentTooShortError(byte_counter, long(data_len))
570 class InfoExtractor(object):
571 """Information Extractor class.
573 Information extractors are the classes that, given a URL, extract
574 information from the video (or videos) the URL refers to. This
575 information includes the real video URL, the video title and simplified
576 title, author and others. The information is stored in a dictionary
577 which is then passed to the FileDownloader. The FileDownloader
578 processes this information possibly downloading the video to the file
579 system, among other possible outcomes. The dictionaries must include
580 the following fields:
582 id: Video identifier.
583 url: Final video URL.
584 uploader: Nickname of the video uploader.
585 title: Literal title.
586 stitle: Simplified title.
587 ext: Video filename extension.
589 Subclasses of this one should re-define the _real_initialize() and
590 _real_extract() methods, as well as the suitable() static method.
591 Probably, they should also be instantiated and added to the main
598 def __init__(self, downloader=None):
599 """Constructor. Receives an optional downloader."""
601 self.set_downloader(downloader)
605 """Receives a URL and returns True if suitable for this IE."""
608 def initialize(self):
609 """Initializes an instance (authentication, etc)."""
611 self._real_initialize()
614 def extract(self, url):
615 """Extracts URL information and returns it in list of dicts."""
617 return self._real_extract(url)
619 def set_downloader(self, downloader):
620 """Sets the downloader for this IE."""
621 self._downloader = downloader
623 def _real_initialize(self):
624 """Real initialization process. Redefine in subclasses."""
627 def _real_extract(self, url):
628 """Real extraction process. Redefine in subclasses."""
631 class YoutubeIE(InfoExtractor):
632 """Information extractor for youtube.com."""
634 _VALID_URL = r'^((?:http://)?(?:\w+\.)?youtube\.com/(?:(?:v/)|(?:(?:watch(?:\.php)?)?[\?#](?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$'
635 _LANG_URL = r'http://uk.youtube.com/?hl=en&persist_hl=1&gl=US&persist_gl=1&opt_out_ackd=1'
636 _LOGIN_URL = 'http://www.youtube.com/signup?next=/&gl=US&hl=en'
637 _AGE_URL = 'http://www.youtube.com/verify_age?next_url=/&gl=US&hl=en'
638 _NETRC_MACHINE = 'youtube'
639 _available_formats = ['37', '22', '35', '18', '34', '5', '17', '13', None] # listed in order of priority for -b flag
640 _video_extensions = {
650 return (re.match(YoutubeIE._VALID_URL, url) is not None)
652 def report_lang(self):
653 """Report attempt to set language."""
654 self._downloader.to_stdout(u'[youtube] Setting language')
656 def report_login(self):
657 """Report attempt to log in."""
658 self._downloader.to_stdout(u'[youtube] Logging in')
660 def report_age_confirmation(self):
661 """Report attempt to confirm age."""
662 self._downloader.to_stdout(u'[youtube] Confirming age')
664 def report_video_info_webpage_download(self, video_id):
665 """Report attempt to download video info webpage."""
666 self._downloader.to_stdout(u'[youtube] %s: Downloading video info webpage' % video_id)
668 def report_information_extraction(self, video_id):
669 """Report attempt to extract video information."""
670 self._downloader.to_stdout(u'[youtube] %s: Extracting video information' % video_id)
672 def report_unavailable_format(self, video_id, format):
673 """Report extracted video URL."""
674 self._downloader.to_stdout(u'[youtube] %s: Format %s not available' % (video_id, format))
676 def report_rtmp_download(self):
677 """Indicate the download will use the RTMP protocol."""
678 self._downloader.to_stdout(u'[youtube] RTMP download detected')
680 def _real_initialize(self):
681 if self._downloader is None:
686 downloader_params = self._downloader.params
688 # Attempt to use provided username and password or .netrc data
689 if downloader_params.get('username', None) is not None:
690 username = downloader_params['username']
691 password = downloader_params['password']
692 elif downloader_params.get('usenetrc', False):
694 info = netrc.netrc().authenticators(self._NETRC_MACHINE)
699 raise netrc.NetrcParseError('No authenticators for %s' % self._NETRC_MACHINE)
700 except (IOError, netrc.NetrcParseError), err:
701 self._downloader.to_stderr(u'WARNING: parsing .netrc: %s' % str(err))
705 request = urllib2.Request(self._LANG_URL, None, std_headers)
708 urllib2.urlopen(request).read()
709 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
710 self._downloader.to_stderr(u'WARNING: unable to set language: %s' % str(err))
713 # No authentication to be performed
719 'current_form': 'loginForm',
721 'action_login': 'Log In',
722 'username': username,
723 'password': password,
725 request = urllib2.Request(self._LOGIN_URL, urllib.urlencode(login_form), std_headers)
728 login_results = urllib2.urlopen(request).read()
729 if re.search(r'(?i)<form[^>]* name="loginForm"', login_results) is not None:
730 self._downloader.to_stderr(u'WARNING: unable to log in: bad username or password')
732 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
733 self._downloader.to_stderr(u'WARNING: unable to log in: %s' % str(err))
739 'action_confirm': 'Confirm',
741 request = urllib2.Request(self._AGE_URL, urllib.urlencode(age_form), std_headers)
743 self.report_age_confirmation()
744 age_results = urllib2.urlopen(request).read()
745 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
746 self._downloader.trouble(u'ERROR: unable to confirm age: %s' % str(err))
749 def _real_extract(self, url):
750 # Extract video id from URL
751 mobj = re.match(self._VALID_URL, url)
753 self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
755 video_id = mobj.group(2)
757 # Downloader parameters
761 if self._downloader is not None:
762 params = self._downloader.params
763 format_param = params.get('format', None)
764 if format_param == '0':
765 format_param = self._available_formats[quality_index]
770 video_extension = self._video_extensions.get(format_param, 'flv')
773 video_info_url = 'http://www.youtube.com/get_video_info?&video_id=%s&el=detailpage&ps=default&eurl=&gl=US&hl=en' % video_id
774 request = urllib2.Request(video_info_url, None, std_headers)
776 self.report_video_info_webpage_download(video_id)
777 video_info_webpage = urllib2.urlopen(request).read()
778 video_info = parse_qs(video_info_webpage)
779 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
780 self._downloader.trouble(u'ERROR: unable to download video info webpage: %s' % str(err))
782 self.report_information_extraction(video_id)
785 if 'token' not in video_info:
786 # Attempt to see if YouTube has issued an error message
787 if 'reason' not in video_info:
788 self._downloader.trouble(u'ERROR: unable to extract "t" parameter for unknown reason')
789 stream = open('reportme-ydl-%s.dat' % time.time(), 'wb')
790 stream.write(video_info_webpage)
793 reason = urllib.unquote_plus(video_info['reason'][0])
794 self._downloader.trouble(u'ERROR: YouTube said: %s' % reason.decode('utf-8'))
796 token = urllib.unquote_plus(video_info['token'][0])
797 video_real_url = 'http://www.youtube.com/get_video?video_id=%s&t=%s&eurl=&el=detailpage&ps=default&gl=US&hl=en' % (video_id, token)
798 if format_param is not None:
799 video_real_url = '%s&fmt=%s' % (video_real_url, format_param)
801 # Check possible RTMP download
802 if 'conn' in video_info and video_info['conn'][0].startswith('rtmp'):
803 self.report_rtmp_download()
804 video_real_url = video_info['conn'][0]
807 if 'author' not in video_info:
808 self._downloader.trouble(u'ERROR: unable to extract uploader nickname')
810 video_uploader = urllib.unquote_plus(video_info['author'][0])
813 if 'title' not in video_info:
814 self._downloader.trouble(u'ERROR: unable to extract video title')
816 video_title = urllib.unquote_plus(video_info['title'][0])
817 video_title = video_title.decode('utf-8')
818 video_title = sanitize_title(video_title)
821 simple_title = re.sub(ur'(?u)([^%s]+)' % simple_title_chars, ur'_', video_title)
822 simple_title = simple_title.strip(ur'_')
825 # Process video information
826 self._downloader.process_info({
827 'id': video_id.decode('utf-8'),
828 'url': video_real_url.decode('utf-8'),
829 'uploader': video_uploader.decode('utf-8'),
830 'title': video_title,
831 'stitle': simple_title,
832 'ext': video_extension.decode('utf-8'),
837 except UnavailableFormatError, err:
839 if quality_index == len(self._available_formats) - 1:
840 # I don't ever expect this to happen
841 self._downloader.trouble(u'ERROR: no known formats available for video')
844 self.report_unavailable_format(video_id, format_param)
846 format_param = self._available_formats[quality_index]
849 self._downloader.trouble('ERROR: format not available for video')
853 class MetacafeIE(InfoExtractor):
854 """Information Extractor for metacafe.com."""
856 _VALID_URL = r'(?:http://)?(?:www\.)?metacafe\.com/watch/([^/]+)/([^/]+)/.*'
857 _DISCLAIMER = 'http://www.metacafe.com/family_filter/'
858 _FILTER_POST = 'http://www.metacafe.com/f/index.php?inputType=filter&controllerGroup=user'
861 def __init__(self, youtube_ie, downloader=None):
862 InfoExtractor.__init__(self, downloader)
863 self._youtube_ie = youtube_ie
867 return (re.match(MetacafeIE._VALID_URL, url) is not None)
869 def report_disclaimer(self):
870 """Report disclaimer retrieval."""
871 self._downloader.to_stdout(u'[metacafe] Retrieving disclaimer')
873 def report_age_confirmation(self):
874 """Report attempt to confirm age."""
875 self._downloader.to_stdout(u'[metacafe] Confirming age')
877 def report_download_webpage(self, video_id):
878 """Report webpage download."""
879 self._downloader.to_stdout(u'[metacafe] %s: Downloading webpage' % video_id)
881 def report_extraction(self, video_id):
882 """Report information extraction."""
883 self._downloader.to_stdout(u'[metacafe] %s: Extracting information' % video_id)
885 def _real_initialize(self):
886 # Retrieve disclaimer
887 request = urllib2.Request(self._DISCLAIMER, None, std_headers)
889 self.report_disclaimer()
890 disclaimer = urllib2.urlopen(request).read()
891 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
892 self._downloader.trouble(u'ERROR: unable to retrieve disclaimer: %s' % str(err))
898 'submit': "Continue - I'm over 18",
900 request = urllib2.Request(self._FILTER_POST, urllib.urlencode(disclaimer_form), std_headers)
902 self.report_age_confirmation()
903 disclaimer = urllib2.urlopen(request).read()
904 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
905 self._downloader.trouble(u'ERROR: unable to confirm age: %s' % str(err))
908 def _real_extract(self, url):
909 # Extract id and simplified title from URL
910 mobj = re.match(self._VALID_URL, url)
912 self._downloader.trouble(u'ERROR: invalid URL: %s' % url)
915 video_id = mobj.group(1)
917 # Check if video comes from YouTube
918 mobj2 = re.match(r'^yt-(.*)$', video_id)
919 if mobj2 is not None:
920 self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % mobj2.group(1))
923 simple_title = mobj.group(2).decode('utf-8')
924 video_extension = 'flv'
926 # Retrieve video webpage to extract further information
927 request = urllib2.Request('http://www.metacafe.com/watch/%s/' % video_id)
929 self.report_download_webpage(video_id)
930 webpage = urllib2.urlopen(request).read()
931 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
932 self._downloader.trouble(u'ERROR: unable retrieve video webpage: %s' % str(err))
935 # Extract URL, uploader and title from webpage
936 self.report_extraction(video_id)
937 mobj = re.search(r'(?m)&mediaURL=([^&]+)', webpage)
939 self._downloader.trouble(u'ERROR: unable to extract media URL')
941 mediaURL = urllib.unquote(mobj.group(1))
943 #mobj = re.search(r'(?m)&gdaKey=(.*?)&', webpage)
945 # self._downloader.trouble(u'ERROR: unable to extract gdaKey')
947 #gdaKey = mobj.group(1)
949 #video_url = '%s?__gda__=%s' % (mediaURL, gdaKey)
953 mobj = re.search(r'(?im)<title>(.*) - Video</title>', webpage)
955 self._downloader.trouble(u'ERROR: unable to extract title')
957 video_title = mobj.group(1).decode('utf-8')
958 video_title = sanitize_title(video_title)
960 mobj = re.search(r'(?ms)By:\s*<a .*?>(.+?)<', webpage)
962 self._downloader.trouble(u'ERROR: unable to extract uploader nickname')
964 video_uploader = mobj.group(1)
967 # Process video information
968 self._downloader.process_info({
969 'id': video_id.decode('utf-8'),
970 'url': video_url.decode('utf-8'),
971 'uploader': video_uploader.decode('utf-8'),
972 'title': video_title,
973 'stitle': simple_title,
974 'ext': video_extension.decode('utf-8'),
976 except UnavailableFormatError:
977 self._downloader.trouble(u'ERROR: format not available for video')
980 class GoogleIE(InfoExtractor):
981 """Information extractor for video.google.com."""
983 _VALID_URL = r'(?:http://)?video\.google\.(?:com(?:\.au)?|co\.(?:uk|jp|kr|cr)|ca|de|es|fr|it|nl|pl)/videoplay\?docid=([^\&]+).*'
985 def __init__(self, downloader=None):
986 InfoExtractor.__init__(self, downloader)
990 return (re.match(GoogleIE._VALID_URL, url) is not None)
992 def report_download_webpage(self, video_id):
993 """Report webpage download."""
994 self._downloader.to_stdout(u'[video.google] %s: Downloading webpage' % video_id)
996 def report_extraction(self, video_id):
997 """Report information extraction."""
998 self._downloader.to_stdout(u'[video.google] %s: Extracting information' % video_id)
1000 def _real_initialize(self):
1003 def _real_extract(self, url):
1004 # Extract id from URL
1005 mobj = re.match(self._VALID_URL, url)
1007 self._downloader.trouble(u'ERROR: Invalid URL: %s' % url)
1010 video_id = mobj.group(1)
1012 video_extension = 'mp4'
1014 # Retrieve video webpage to extract further information
1015 request = urllib2.Request('http://video.google.com/videoplay?docid=%s&hl=en&oe=utf-8' % video_id)
1017 self.report_download_webpage(video_id)
1018 webpage = urllib2.urlopen(request).read()
1019 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
1020 self._downloader.trouble(u'ERROR: Unable to retrieve video webpage: %s' % str(err))
1023 # Extract URL, uploader, and title from webpage
1024 self.report_extraction(video_id)
1025 mobj = re.search(r"download_url:'([^']+)'", webpage)
1027 video_extension = 'flv'
1028 mobj = re.search(r"(?i)videoUrl\\x3d(.+?)\\x26", webpage)
1030 self._downloader.trouble(u'ERROR: unable to extract media URL')
1032 mediaURL = urllib.unquote(mobj.group(1))
1033 mediaURL = mediaURL.replace('\\x3d', '\x3d')
1034 mediaURL = mediaURL.replace('\\x26', '\x26')
1036 video_url = mediaURL
1038 mobj = re.search(r'<title>(.*)</title>', webpage)
1040 self._downloader.trouble(u'ERROR: unable to extract title')
1042 video_title = mobj.group(1).decode('utf-8')
1043 video_title = sanitize_title(video_title)
1044 simple_title = re.sub(ur'(?u)([^%s]+)' % simple_title_chars, ur'_', video_title)
1046 # Google Video doesn't show uploader nicknames?
1047 video_uploader = 'NA'
1050 # Process video information
1051 self._downloader.process_info({
1052 'id': video_id.decode('utf-8'),
1053 'url': video_url.decode('utf-8'),
1054 'uploader': video_uploader.decode('utf-8'),
1055 'title': video_title,
1056 'stitle': simple_title,
1057 'ext': video_extension.decode('utf-8'),
1059 except UnavailableFormatError:
1060 self._downloader.trouble(u'ERROR: format not available for video')
1063 class PhotobucketIE(InfoExtractor):
1064 """Information extractor for photobucket.com."""
1066 _VALID_URL = r'(?:http://)?(?:[a-z0-9]+\.)?photobucket\.com/.*[\?\&]current=(.*\.flv)'
1068 def __init__(self, downloader=None):
1069 InfoExtractor.__init__(self, downloader)
1073 return (re.match(PhotobucketIE._VALID_URL, url) is not None)
1075 def report_download_webpage(self, video_id):
1076 """Report webpage download."""
1077 self._downloader.to_stdout(u'[photobucket] %s: Downloading webpage' % video_id)
1079 def report_extraction(self, video_id):
1080 """Report information extraction."""
1081 self._downloader.to_stdout(u'[photobucket] %s: Extracting information' % video_id)
1083 def _real_initialize(self):
1086 def _real_extract(self, url):
1087 # Extract id from URL
1088 mobj = re.match(self._VALID_URL, url)
1090 self._downloader.trouble(u'ERROR: Invalid URL: %s' % url)
1093 video_id = mobj.group(1)
1095 video_extension = 'flv'
1097 # Retrieve video webpage to extract further information
1098 request = urllib2.Request(url)
1100 self.report_download_webpage(video_id)
1101 webpage = urllib2.urlopen(request).read()
1102 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
1103 self._downloader.trouble(u'ERROR: Unable to retrieve video webpage: %s' % str(err))
1106 # Extract URL, uploader, and title from webpage
1107 self.report_extraction(video_id)
1108 mobj = re.search(r'<link rel="video_src" href=".*\?file=([^"]+)" />', webpage)
1110 self._downloader.trouble(u'ERROR: unable to extract media URL')
1112 mediaURL = urllib.unquote(mobj.group(1))
1114 video_url = mediaURL
1116 mobj = re.search(r'<title>(.*) video by (.*) - Photobucket</title>', webpage)
1118 self._downloader.trouble(u'ERROR: unable to extract title')
1120 video_title = mobj.group(1).decode('utf-8')
1121 video_title = sanitize_title(video_title)
1122 simple_title = re.sub(ur'(?u)([^%s]+)' % simple_title_chars, ur'_', video_title)
1124 video_uploader = mobj.group(2).decode('utf-8')
1127 # Process video information
1128 self._downloader.process_info({
1129 'id': video_id.decode('utf-8'),
1130 'url': video_url.decode('utf-8'),
1131 'uploader': video_uploader,
1132 'title': video_title,
1133 'stitle': simple_title,
1134 'ext': video_extension.decode('utf-8'),
1136 except UnavailableFormatError:
1137 self._downloader.trouble(u'ERROR: format not available for video')
1140 class GenericIE(InfoExtractor):
1141 """Generic last-resort information extractor."""
1143 def __init__(self, downloader=None):
1144 InfoExtractor.__init__(self, downloader)
1150 def report_download_webpage(self, video_id):
1151 """Report webpage download."""
1152 self._downloader.to_stdout(u'WARNING: Falling back on generic information extractor.')
1153 self._downloader.to_stdout(u'[generic] %s: Downloading webpage' % video_id)
1155 def report_extraction(self, video_id):
1156 """Report information extraction."""
1157 self._downloader.to_stdout(u'[generic] %s: Extracting information' % video_id)
1159 def _real_initialize(self):
1162 def _real_extract(self, url):
1163 video_id = url.split('/')[-1]
1164 request = urllib2.Request(url)
1166 self.report_download_webpage(video_id)
1167 webpage = urllib2.urlopen(request).read()
1168 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
1169 self._downloader.trouble(u'ERROR: Unable to retrieve video webpage: %s' % str(err))
1171 except ValueError, err:
1172 # since this is the last-resort InfoExtractor, if
1173 # this error is thrown, it'll be thrown here
1174 self._downloader.trouble(u'ERROR: Invalid URL: %s' % url)
1177 # Start with something easy: JW Player in SWFObject
1178 mobj = re.search(r'flashvars: [\'"](?:.*&)?file=(http[^\'"&]*)', webpage)
1180 # Broaden the search a little bit
1181 mobj = re.search(r'[^A-Za-z0-9]?(?:file|source)=(http[^\'"&]*)', webpage)
1183 self._downloader.trouble(u'ERROR: Invalid URL: %s' % url)
1186 # It's possible that one of the regexes
1187 # matched, but returned an empty group:
1188 if mobj.group(1) is None:
1189 self._downloader.trouble(u'ERROR: Invalid URL: %s' % url)
1192 video_url = urllib.unquote(mobj.group(1))
1193 video_id = os.path.basename(video_url)
1195 # here's a fun little line of code for you:
1196 video_extension = os.path.splitext(video_id)[1][1:]
1197 video_id = os.path.splitext(video_id)[0]
1199 # it's tempting to parse this further, but you would
1200 # have to take into account all the variations like
1201 # Video Title - Site Name
1202 # Site Name | Video Title
1203 # Video Title - Tagline | Site Name
1204 # and so on and so forth; it's just not practical
1205 mobj = re.search(r'<title>(.*)</title>', webpage)
1207 self._downloader.trouble(u'ERROR: unable to extract title')
1209 video_title = mobj.group(1).decode('utf-8')
1210 video_title = sanitize_title(video_title)
1211 simple_title = re.sub(ur'(?u)([^%s]+)' % simple_title_chars, ur'_', video_title)
1213 # video uploader is domain name
1214 mobj = re.match(r'(?:https?://)?([^/]*)/.*', url)
1216 self._downloader.trouble(u'ERROR: unable to extract title')
1218 video_uploader = mobj.group(1).decode('utf-8')
1221 # Process video information
1222 self._downloader.process_info({
1223 'id': video_id.decode('utf-8'),
1224 'url': video_url.decode('utf-8'),
1225 'uploader': video_uploader,
1226 'title': video_title,
1227 'stitle': simple_title,
1228 'ext': video_extension.decode('utf-8'),
1230 except UnavailableFormatError:
1231 self._downloader.trouble(u'ERROR: format not available for video')
1234 class YoutubeSearchIE(InfoExtractor):
1235 """Information Extractor for YouTube search queries."""
1236 _VALID_QUERY = r'ytsearch(\d+|all)?:[\s\S]+'
1237 _TEMPLATE_URL = 'http://www.youtube.com/results?search_query=%s&page=%s&gl=US&hl=en'
1238 _VIDEO_INDICATOR = r'href="/watch\?v=.+?"'
1239 _MORE_PAGES_INDICATOR = r'(?m)>\s*Next\s*</a>'
1241 _max_youtube_results = 1000
1243 def __init__(self, youtube_ie, downloader=None):
1244 InfoExtractor.__init__(self, downloader)
1245 self._youtube_ie = youtube_ie
1249 return (re.match(YoutubeSearchIE._VALID_QUERY, url) is not None)
1251 def report_download_page(self, query, pagenum):
1252 """Report attempt to download playlist page with given number."""
1253 query = query.decode(preferredencoding())
1254 self._downloader.to_stdout(u'[youtube] query "%s": Downloading page %s' % (query, pagenum))
1256 def _real_initialize(self):
1257 self._youtube_ie.initialize()
1259 def _real_extract(self, query):
1260 mobj = re.match(self._VALID_QUERY, query)
1262 self._downloader.trouble(u'ERROR: invalid search query "%s"' % query)
1265 prefix, query = query.split(':')
1267 query = query.encode('utf-8')
1269 self._download_n_results(query, 1)
1271 elif prefix == 'all':
1272 self._download_n_results(query, self._max_youtube_results)
1278 self._downloader.trouble(u'ERROR: invalid download number %s for query "%s"' % (n, query))
1280 elif n > self._max_youtube_results:
1281 self._downloader.to_stderr(u'WARNING: ytsearch returns max %i results (you requested %i)' % (self._max_youtube_results, n))
1282 n = self._max_youtube_results
1283 self._download_n_results(query, n)
1285 except ValueError: # parsing prefix as integer fails
1286 self._download_n_results(query, 1)
1289 def _download_n_results(self, query, n):
1290 """Downloads a specified number of results for a query"""
1293 already_seen = set()
1297 self.report_download_page(query, pagenum)
1298 result_url = self._TEMPLATE_URL % (urllib.quote_plus(query), pagenum)
1299 request = urllib2.Request(result_url, None, std_headers)
1301 page = urllib2.urlopen(request).read()
1302 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
1303 self._downloader.trouble(u'ERROR: unable to download webpage: %s' % str(err))
1306 # Extract video identifiers
1307 for mobj in re.finditer(self._VIDEO_INDICATOR, page):
1308 video_id = page[mobj.span()[0]:mobj.span()[1]].split('=')[2][:-1]
1309 if video_id not in already_seen:
1310 video_ids.append(video_id)
1311 already_seen.add(video_id)
1312 if len(video_ids) == n:
1313 # Specified n videos reached
1314 for id in video_ids:
1315 self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
1318 if re.search(self._MORE_PAGES_INDICATOR, page) is None:
1319 for id in video_ids:
1320 self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
1323 pagenum = pagenum + 1
1325 class YoutubePlaylistIE(InfoExtractor):
1326 """Information Extractor for YouTube playlists."""
1328 _VALID_URL = r'(?:http://)?(?:\w+\.)?youtube.com/(?:view_play_list|my_playlists)\?.*?p=([^&]+).*'
1329 _TEMPLATE_URL = 'http://www.youtube.com/view_play_list?p=%s&page=%s&gl=US&hl=en'
1330 _VIDEO_INDICATOR = r'/watch\?v=(.+?)&'
1331 _MORE_PAGES_INDICATOR = r'/view_play_list?p=%s&page=%s'
1334 def __init__(self, youtube_ie, downloader=None):
1335 InfoExtractor.__init__(self, downloader)
1336 self._youtube_ie = youtube_ie
1340 return (re.match(YoutubePlaylistIE._VALID_URL, url) is not None)
1342 def report_download_page(self, playlist_id, pagenum):
1343 """Report attempt to download playlist page with given number."""
1344 self._downloader.to_stdout(u'[youtube] PL %s: Downloading page #%s' % (playlist_id, pagenum))
1346 def _real_initialize(self):
1347 self._youtube_ie.initialize()
1349 def _real_extract(self, url):
1350 # Extract playlist id
1351 mobj = re.match(self._VALID_URL, url)
1353 self._downloader.trouble(u'ERROR: invalid url: %s' % url)
1356 # Download playlist pages
1357 playlist_id = mobj.group(1)
1362 self.report_download_page(playlist_id, pagenum)
1363 request = urllib2.Request(self._TEMPLATE_URL % (playlist_id, pagenum), None, std_headers)
1365 page = urllib2.urlopen(request).read()
1366 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
1367 self._downloader.trouble(u'ERROR: unable to download webpage: %s' % str(err))
1370 # Extract video identifiers
1372 for mobj in re.finditer(self._VIDEO_INDICATOR, page):
1373 if mobj.group(1) not in ids_in_page:
1374 ids_in_page.append(mobj.group(1))
1375 video_ids.extend(ids_in_page)
1377 if (self._MORE_PAGES_INDICATOR % (playlist_id.upper(), pagenum + 1)) not in page:
1379 pagenum = pagenum + 1
1381 for id in video_ids:
1382 self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
1385 class YoutubeUserIE(InfoExtractor):
1386 """Information Extractor for YouTube users."""
1388 _VALID_URL = r'(?:http://)?(?:\w+\.)?youtube.com/user/(.*)'
1389 _TEMPLATE_URL = 'http://gdata.youtube.com/feeds/api/users/%s'
1390 _VIDEO_INDICATOR = r'http://gdata.youtube.com/feeds/api/videos/(.*)' # XXX Fix this.
1393 def __init__(self, youtube_ie, downloader=None):
1394 InfoExtractor.__init__(self, downloader)
1395 self._youtube_ie = youtube_ie
1399 return (re.match(YoutubeUserIE._VALID_URL, url) is not None)
1401 def report_download_page(self, username):
1402 """Report attempt to download user page."""
1403 self._downloader.to_stdout(u'[youtube] user %s: Downloading page ' % (username))
1405 def _real_initialize(self):
1406 self._youtube_ie.initialize()
1408 def _real_extract(self, url):
1410 mobj = re.match(self._VALID_URL, url)
1412 self._downloader.trouble(u'ERROR: invalid url: %s' % url)
1415 # Download user page
1416 username = mobj.group(1)
1420 self.report_download_page(username)
1421 request = urllib2.Request(self._TEMPLATE_URL % (username), None, std_headers)
1423 page = urllib2.urlopen(request).read()
1424 except (urllib2.URLError, httplib.HTTPException, socket.error), err:
1425 self._downloader.trouble(u'ERROR: unable to download webpage: %s' % str(err))
1428 # Extract video identifiers
1431 for mobj in re.finditer(self._VIDEO_INDICATOR, page):
1432 if mobj.group(1) not in ids_in_page:
1433 ids_in_page.append(mobj.group(1))
1434 video_ids.extend(ids_in_page)
1436 for id in video_ids:
1437 self._youtube_ie.extract('http://www.youtube.com/watch?v=%s' % id)
1440 class PostProcessor(object):
1441 """Post Processor class.
1443 PostProcessor objects can be added to downloaders with their
1444 add_post_processor() method. When the downloader has finished a
1445 successful download, it will take its internal chain of PostProcessors
1446 and start calling the run() method on each one of them, first with
1447 an initial argument and then with the returned value of the previous
1450 The chain will be stopped if one of them ever returns None or the end
1451 of the chain is reached.
1453 PostProcessor objects follow a "mutual registration" process similar
1454 to InfoExtractor objects.
1459 def __init__(self, downloader=None):
1460 self._downloader = downloader
1462 def set_downloader(self, downloader):
1463 """Sets the downloader for this PP."""
1464 self._downloader = downloader
1466 def run(self, information):
1467 """Run the PostProcessor.
1469 The "information" argument is a dictionary like the ones
1470 composed by InfoExtractors. The only difference is that this
1471 one has an extra field called "filepath" that points to the
1474 When this method returns None, the postprocessing chain is
1475 stopped. However, this method may return an information
1476 dictionary that will be passed to the next postprocessing
1477 object in the chain. It can be the one it received after
1478 changing some fields.
1480 In addition, this method may raise a PostProcessingError
1481 exception that will be taken into account by the downloader
1484 return information # by default, do nothing
1486 ### MAIN PROGRAM ###
1487 if __name__ == '__main__':
1489 # Modules needed only when running the main program
1493 # Function to update the program file with the latest version from bitbucket.org
1494 def update_self(downloader, filename):
1495 # Note: downloader only used for options
1496 if not os.access (filename, os.W_OK):
1497 sys.exit('ERROR: no write permissions on %s' % filename)
1499 downloader.to_stdout('Updating to latest stable version...')
1500 latest_url = 'http://bitbucket.org/rg3/youtube-dl/raw/tip/LATEST_VERSION'
1501 latest_version = urllib.urlopen(latest_url).read().strip()
1502 prog_url = 'http://bitbucket.org/rg3/youtube-dl/raw/%s/youtube-dl' % latest_version
1503 newcontent = urllib.urlopen(prog_url).read()
1504 stream = open(filename, 'w')
1505 stream.write(newcontent)
1507 downloader.to_stdout('Updated to version %s' % latest_version)
1509 # General configuration
1510 urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()))
1511 urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor()))
1512 socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
1514 # Parse command line
1515 parser = optparse.OptionParser(
1516 usage='Usage: %prog [options] url...',
1517 version='2010.02.13',
1518 conflict_handler='resolve',
1521 parser.add_option('-h', '--help',
1522 action='help', help='print this help text and exit')
1523 parser.add_option('-v', '--version',
1524 action='version', help='print program version and exit')
1525 parser.add_option('-U', '--update',
1526 action='store_true', dest='update_self', help='update this program to latest stable version')
1527 parser.add_option('-i', '--ignore-errors',
1528 action='store_true', dest='ignoreerrors', help='continue on download errors', default=False)
1529 parser.add_option('-r', '--rate-limit',
1530 dest='ratelimit', metavar='L', help='download rate limit (e.g. 50k or 44.6m)')
1532 authentication = optparse.OptionGroup(parser, 'Authentication Options')
1533 authentication.add_option('-u', '--username',
1534 dest='username', metavar='UN', help='account username')
1535 authentication.add_option('-p', '--password',
1536 dest='password', metavar='PW', help='account password')
1537 authentication.add_option('-n', '--netrc',
1538 action='store_true', dest='usenetrc', help='use .netrc authentication data', default=False)
1539 parser.add_option_group(authentication)
1541 video_format = optparse.OptionGroup(parser, 'Video Format Options')
1542 video_format.add_option('-f', '--format',
1543 action='store', dest='format', metavar='FMT', help='video format code')
1544 video_format.add_option('-b', '--best-quality',
1545 action='store_const', dest='format', help='download the best quality video possible', const='0')
1546 video_format.add_option('-m', '--mobile-version',
1547 action='store_const', dest='format', help='alias for -f 17', const='17')
1548 video_format.add_option('-d', '--high-def',
1549 action='store_const', dest='format', help='alias for -f 22', const='22')
1550 parser.add_option_group(video_format)
1552 verbosity = optparse.OptionGroup(parser, 'Verbosity / Simulation Options')
1553 verbosity.add_option('-q', '--quiet',
1554 action='store_true', dest='quiet', help='activates quiet mode', default=False)
1555 verbosity.add_option('-s', '--simulate',
1556 action='store_true', dest='simulate', help='do not download video', default=False)
1557 verbosity.add_option('-g', '--get-url',
1558 action='store_true', dest='geturl', help='simulate, quiet but print URL', default=False)
1559 verbosity.add_option('-e', '--get-title',
1560 action='store_true', dest='gettitle', help='simulate, quiet but print title', default=False)
1561 parser.add_option_group(verbosity)
1563 filesystem = optparse.OptionGroup(parser, 'Filesystem Options')
1564 filesystem.add_option('-t', '--title',
1565 action='store_true', dest='usetitle', help='use title in file name', default=False)
1566 filesystem.add_option('-l', '--literal',
1567 action='store_true', dest='useliteral', help='use literal title in file name', default=False)
1568 filesystem.add_option('-o', '--output',
1569 dest='outtmpl', metavar='TPL', help='output filename template')
1570 filesystem.add_option('-a', '--batch-file',
1571 dest='batchfile', metavar='F', help='file containing URLs to download')
1572 filesystem.add_option('-w', '--no-overwrites',
1573 action='store_true', dest='nooverwrites', help='do not overwrite files', default=False)
1574 filesystem.add_option('-c', '--continue',
1575 action='store_true', dest='continue_dl', help='resume partially downloaded files', default=False)
1576 parser.add_option_group(filesystem)
1578 (opts, args) = parser.parse_args()
1580 # Batch file verification
1582 if opts.batchfile is not None:
1584 batchurls = open(opts.batchfile, 'r').readlines()
1585 batchurls = [x.strip() for x in batchurls]
1586 batchurls = [x for x in batchurls if len(x) > 0]
1588 sys.exit(u'ERROR: batch file could not be read')
1589 all_urls = batchurls + args
1591 # Conflicting, missing and erroneous options
1592 if opts.usenetrc and (opts.username is not None or opts.password is not None):
1593 parser.error(u'using .netrc conflicts with giving username/password')
1594 if opts.password is not None and opts.username is None:
1595 parser.error(u'account username missing')
1596 if opts.outtmpl is not None and (opts.useliteral or opts.usetitle):
1597 parser.error(u'using output template conflicts with using title or literal title')
1598 if opts.usetitle and opts.useliteral:
1599 parser.error(u'using title conflicts with using literal title')
1600 if opts.username is not None and opts.password is None:
1601 opts.password = getpass.getpass(u'Type account password and press return:')
1602 if opts.ratelimit is not None:
1603 numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
1604 if numeric_limit is None:
1605 parser.error(u'invalid rate limit specified')
1606 opts.ratelimit = numeric_limit
1608 # Information extractors
1609 youtube_ie = YoutubeIE()
1610 metacafe_ie = MetacafeIE(youtube_ie)
1611 youtube_pl_ie = YoutubePlaylistIE(youtube_ie)
1612 youtube_user_ie = YoutubeUserIE(youtube_ie)
1613 youtube_search_ie = YoutubeSearchIE(youtube_ie)
1614 google_ie = GoogleIE()
1615 photobucket_ie = PhotobucketIE()
1616 generic_ie = GenericIE()
1619 fd = FileDownloader({
1620 'usenetrc': opts.usenetrc,
1621 'username': opts.username,
1622 'password': opts.password,
1623 'quiet': (opts.quiet or opts.geturl or opts.gettitle),
1624 'forceurl': opts.geturl,
1625 'forcetitle': opts.gettitle,
1626 'simulate': (opts.simulate or opts.geturl or opts.gettitle),
1627 'format': opts.format,
1628 'outtmpl': ((opts.outtmpl is not None and opts.outtmpl.decode(preferredencoding()))
1629 or (opts.usetitle and u'%(stitle)s-%(id)s.%(ext)s')
1630 or (opts.useliteral and u'%(title)s-%(id)s.%(ext)s')
1631 or u'%(id)s.%(ext)s'),
1632 'ignoreerrors': opts.ignoreerrors,
1633 'ratelimit': opts.ratelimit,
1634 'nooverwrites': opts.nooverwrites,
1635 'continuedl': opts.continue_dl,
1637 fd.add_info_extractor(youtube_search_ie)
1638 fd.add_info_extractor(youtube_pl_ie)
1639 fd.add_info_extractor(youtube_user_ie)
1640 fd.add_info_extractor(metacafe_ie)
1641 fd.add_info_extractor(youtube_ie)
1642 fd.add_info_extractor(google_ie)
1643 fd.add_info_extractor(photobucket_ie)
1645 # This must come last since it's the
1646 # fallback if none of the others work
1647 fd.add_info_extractor(generic_ie)
1650 if opts.update_self:
1651 update_self(fd, sys.argv[0])
1654 if len(all_urls) < 1:
1655 if not opts.update_self:
1656 parser.error(u'you must provide at least one URL')
1659 retcode = fd.download(all_urls)
1662 except DownloadError:
1664 except SameFileError:
1665 sys.exit(u'ERROR: fixed output name but more than one file to download')
1666 except KeyboardInterrupt:
1667 sys.exit(u'\nERROR: Interrupted by user')