You're replying to a comment by Elephant.

Elephant Permalink
November 17, 2009, 16:55

I made the script below in a rush, so it's not the fix of the original code but something that just works (it does upload the video but doesn't check for successful login, nor fills the separate form for title and such, sorry!), tested only under Linux on 17th Nov 2009. Can maybe someone with more time update the original code as well?

Note that the reason I needed this script is because my Internet connection isn't fast and stable, and I had to upload my video with various tricks; this script is one of those. I have no intention of abusing it and you shouldn't either.

Regarding the YouTube API: I think it was designed for different purposes, correct me if I'm wrong. I find it hard to grasp, too.

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Net::SSLeay;

# ==== CONFIGURATION START ===

my $username = 'username';
my $password = 'password';
my $file = './test.avi';

# ==== CONFIGURATION END ===

# TODO: $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1;

my $loginPage = 'https://www.google.com/accounts/ServiceLogin?uilel=3&service=youtube&passive=true&' .
    'continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26nom' .
    'obiletemp%3D1%26hl%3Den_US%26next%3D%252Findex&hl=en_US&ltmpl=sso';

my $loginForm = 'https://www.google.com/accounts/ServiceLoginAuth?service=youtube';
my $uploadPage = 'http://www.youtube.com/my_videos_upload?nobeta';
my $uploadForm = 'http://upload.youtube.com/my_streaming_videos_post';

my $GALX;

sub findGALX
{
	$GALX = $_[2] if $_[1] eq "GALX";
}

my $ua = LWP::UserAgent->new
(
	cookie_jar		=> {},
	agent			=> 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3'
);

push @{$ua->requests_redirectable}, 'POST';

$ua->show_progress(1);

print "\n--- Requesting login page...\n";

my $resp = $ua->get($loginPage);

unless ($resp->is_success)
{
	die "Unable to get login page: ", $resp->status_line;
}

print "\n--- Logging in...\n";

$ua->cookie_jar->scan(\&findGALX);

my $req = POST $loginForm,
[
	'ltmpl'				=> 'sso',
	'continue'			=> 'http://www.youtube.com/signin?action_handle_signin=true&nomobiletemp=1&hl=en_US&next=%2F',
	'service'			=> 'youtube',
	'uilel'				=> '3',
	'hl'				=> 'en_US',
	'GALX'				=> $GALX,
	'Email'				=> $username,
	'Passwd'			=> $password,
	'PersistentCookie'	=> 'yes',
	'rmShown'			=> '1',
	'signIn'			=> 'Sign in',
	'asts'				=> ''
];

$req->referer($loginPage);
$resp = $ua->request($req);

unless ($resp->is_success)
{
	die "Couldn't log in: ", $resp->status_line;
}

print "\n--- Requesting the page after login...\n";

my ($nextPage) = $resp->header('Refresh') =~ /0; url='([^']+)'/;

print $nextPage . "\n";

my $req = GET $nextPage;
$req->referer($loginForm);
$resp = $ua->request($req);

print "\n--- Requesting upload page...\n";

my $req = GET $uploadPage;
$req->referer($nextPage);
$resp = $ua->request($req);

print "\n--- Uploading...\n";

my ($uploadKey) = $resp->content =~ /yt\.www\.upload\.UploadSettings\.Keys\.UPLOAD_KEY, "([^"]+)"/m;
my ($sessionKey) = $resp->content =~ /yt\.www\.upload\.UploadSettings\.Keys\.SESSION_KEY, "([^"]+)"/m;
my ($sessionToken) = $resp->content =~ /'XSRF_TOKEN': '([^']+)'/m;

my $req = POST $uploadForm,
	Content_Type	=> 'form-data',
	Content			=> [
		'uploader_type' => 'Web_HTML',
		'return_address' => 'www.youtube.com',
		'upload_key' => $uploadKey,
		'action_postvideo' => '1',
		'live_thumbnail_id' => $sessionKey,
		'up_locale' => 'en_US',
		'up_content_region' => 'US',
		'up_country' => 'US',
		'field_uploadfile' => [ $file ],
		'session_token' => $sessionToken,
	];

$req->referer($uploadPage);
$resp = $ua->request($req);

# open (MYFILE, '>content.html') || die $!;
# print MYFILE $resp->content . "\n";
# close MYFILE;

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.