rg3/youtube-dl — pull request #405 by hdclark (2012-08-23 07:09:43+00:00)
Title: Support for custom user agent

What
----

A few simple lines were added to provide a "--user-agent" option to specify a custom user agent.

How it changes the code
-----------------------

A new variable (opt.useragent) is introduced. No functionality breaks (I think!) As soon as possible, we update std_header['User-Agent'] with the custom user agent string.

How it should be used
---------------------

youtube-dl --user-agent "My Custom User Agent" "http://www.website.com/"

How it changes the HTML
-----------------------

Without a custom user agent, we can expect

prompt$ youtube-dl 'http://127.0.0.1:8080/somesite'

which yields the GET header

Host: 127.0.0.1:8080
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: close

When we specify a custom agent

prompt$ youtube-dl --user-agent 'My Custom User Agent' 'http://127.0.0.1:8080/somesite'

we get the modified request

Host: 127.0.0.1:8080
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: My Custom User Agent
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: close

Notes
-----

I have very little experience with Python. My tests all worked, but please ensure I've not done something silly.