Merge pull request #2553 from anisse/master
authorPhilipp Hagemeister <phihag@phihag.de>
Mon, 24 Mar 2014 09:42:58 +0000 (10:42 +0100)
committerPhilipp Hagemeister <phihag@phihag.de>
Mon, 24 Mar 2014 09:42:58 +0000 (10:42 +0100)
Add an option to specify custom HTTP headers

1  2 
youtube_dl/__init__.py

diff --combined youtube_dl/__init__.py
index a4cbdb0bdbea7ad238fad1e828a14a106b9cdcd4,470acf3ef4fa46490990b73e1b279749aaeffe41..056e94457ae35bef07f2384a8dd732dbe7597f8f
@@@ -56,6 -56,7 +56,6 @@@ __authors__  = 
  __license__ = 'Public Domain'
  
  import codecs
 -import getpass
  import io
  import locale
  import optparse
@@@ -67,7 -68,6 +67,7 @@@ import sy
  
  
  from .utils import (
 +    compat_getpass,
      compat_print,
      DateRange,
      decodeOption,
@@@ -227,6 -227,9 +227,9 @@@ def parseOpts(overrideArguments=None)
      general.add_option('--referer',
              dest='referer', help='specify a custom referer, use if the video access is restricted to one domain',
              metavar='REF', default=None)
+     general.add_option('--add-header',
+             dest='headers', help='specify a custom HTTP header and its value, separated by a colon \':\'. You can use this option multiple times', action="append",
+             metavar='FIELD:VALUE')
      general.add_option('--list-extractors',
              action='store_true', dest='list_extractors',
              help='List all supported extractors and the URLs they would handle', default=False)
          '--proxy', dest='proxy', default=None, metavar='URL',
          help='Use the specified HTTP/HTTPS proxy. Pass in an empty string (--proxy "") for direct connection')
      general.add_option('--no-check-certificate', action='store_true', dest='no_check_certificate', default=False, help='Suppress HTTPS certificate validation.')
 +    general.add_option(
 +        '--prefer-insecure', action='store_true', dest='prefer_insecure',
 +        help='Use an unencrypted connection to retrieve information about the video. (Currently supported only for YouTube)')
      general.add_option(
          '--cache-dir', dest='cachedir', default=get_cachedir(), metavar='DIR',
          help='Location in the filesystem where youtube-dl can store some downloaded information permanently. By default $XDG_CACHE_HOME/youtube-dl or ~/.cache/youtube-dl . At the moment, only YouTube player files (for videos with obfuscated signatures) are cached, but that may change.')
          action='store_true',
          help='Do not read configuration files. When given in the global configuration file /etc/youtube-dl.conf: do not read the user configuration in ~/.config/youtube-dl.conf (%APPDATA%/youtube-dl/config.txt on Windows)')
  
 -
      selection.add_option(
          '--playlist-start',
          dest='playliststart', metavar='NUMBER', default=1, type=int,
@@@ -556,6 -557,16 +559,16 @@@ def _real_main(argv=None)
      if opts.referer is not None:
          std_headers['Referer'] = opts.referer
  
+     # Custom HTTP headers
+     if opts.headers is not None:
+         for h in opts.headers:
+             if h.find(':', 1) < 0:
+                 parser.error(u'wrong header formatting, it should be key:value, not "%s"'%h)
+             key, value = h.split(':', 2)
+             if opts.verbose:
+                 write_string(u'[debug] Adding header from command line option %s:%s\n'%(key, value))
+             std_headers[key] = value
      # Dump user agent
      if opts.dump_user_agent:
          compat_print(std_headers['User-Agent'])
      if opts.usetitle and opts.useid:
          parser.error(u'using title conflicts with using video ID')
      if opts.username is not None and opts.password is None:
 -        opts.password = getpass.getpass(u'Type account password and press return:')
 +        opts.password = compat_getpass(u'Type account password and press [Return]: ')
      if opts.ratelimit is not None:
          numeric_limit = FileDownloader.parse_bytes(opts.ratelimit)
          if numeric_limit is None:
          'download_archive': download_archive_fn,
          'cookiefile': opts.cookiefile,
          'nocheckcertificate': opts.no_check_certificate,
 +        'prefer_insecure': opts.prefer_insecure,
          'proxy': opts.proxy,
          'socket_timeout': opts.socket_timeout,
          'bidi_workaround': opts.bidi_workaround,