You're viewing a comment by MikaH and its responses.

MikaH Permalink
September 18, 2007, 21:25

Hi all,

I got inspired enough to try getting my feet wet with modifying YouTube version to go with Google Video.

Below is the outcome, it uploads the video but then dumps and ends with 404 error.
If someone is brave enough to finalize this; for me this is good enough tho.

--

#!/usr/bin/perl
#
# 2007-09-17
#
# Mika Helander
#
# Original YouTube version:
# 2007.07.30
#
# Peteris Krumins (peter@catonmat.net)
# http://www.catonmat.net  -  good coders code, great reuse
#

use constant VERSION => "1.0";

use strict;
use warnings;
use LWP::UserAgent;
use Getopt::Std;

$Getopt::Std::STANDARD_HELP_VERSION = 1;

# set these values for default -l (login) and -p (pass) values
#
use constant YT_LOGIN => "";
use constant YT_PASS  => "";

# video categories
#
my %cats = (
 'Action & Adventure'    => "ACTION_ADV",
 'Ads & Promotional'     => "AD_PROMO",
 'Anime & Animation'     => "ANIMATION",
 'Art & Experimental'    => "ART_EXPR",
 'Business'              => "BUSINESS",
 'Children & Family'     => "CHILDRENS",
 'Comedy'                => "COMEDY",
 'Dance'                 => "DANCE",
 'Documentary'           => "DOCUMENTARY",
 'Drama'                 => "DRAMA",
 'Educational'           => "EDUCATIONAL",
 'Entertainment'         => "ENTERTAINMENT",
 'Faith & Spirituality'  => "FAITH_SPIRIT",
 'Foreign'               => "FOREIGN",
 'Gaming'                => "GAMING",
 'Gay & Lesbian'         => "GAY_LESBIAN",
 'Health & Fitness'      => "FITNESS",
 'Home Video'            => "HOME_VIDEO",
 'Horror'                => "HORROR",
 'Independent'           => "INDY",
 'Mature & Adult'        => "MATURE_ADULT",
 'Movie (feature)'       => "MOVIE_FEATURE",
 'Movie (short)'         => "MOVIE_SHORT",
 'Movie Trailer'         => "MOVIE_TRAILER",
 'Music & Musical'       => "MUSIC",
 'Nature'                => "NATURE",
 'News'                  => "NEWS",
 'Political'             => "POLITCAL",
 'Religious'             => "RELIGIOUS",
 'Romance'               => "ROMANCE",
 'Science & Technology'  => "SCI_TECH",
 'Sci-Fi & Fantasy'      => "SCIENCE_FICTION",
 'Special Interest'      => "SPECIAL_INT",
 'Sports'                => "SPORTS",
 'Stock Footage'         => "STOCK_FOOTAGE",
 'Thriller'              => "THRILLER",
 'Travel'                => "TRAVEL",
 'TV Show'               => "TV_SHOW",
 'Western'               => "WESTERN"
);

# various urls
my $login_url  = 'https://www.google.com/accounts/LoginAuth?continue=http%3A%2F%2Fvideo.google.com%2F&hl=en';
my $upload_url = 'http://www.google.com/video/uploader/form/videoonline';

unless (@ARGV) {
    HELP_MESSAGE();
    exit 1;
}

my %opts;
getopts('l:p:f:c:t:d:', \%opts);

# if -l or -p are not given, try to use YT_LOGIN and YT_PASS constants
unless (defined $opts{l}) {
    unless (length YT_LOGIN) {
        preamble();
 print "Google Video username/login as neither defined nor passed as an argument\n";
        print "Use -l switch to specify the username\n";
        print "Example: -l joe_random\n";
        exit 1;
    }
    else {
        $opts{l} = YT_LOGIN;
    }
}

unless (defined $opts{p}) {
    unless (length YT_PASS) {
        preamble();
        print "Password was neither defined nor passed as an argument\n";
        print "Use -p switch to specify the password\n";
        print "Example: -p secretPass\n";
        exit 1;
    }
    else {
        $opts{p} = YT_PASS;
    }
}

unless (defined $opts{f} && length $opts{f}) {
    preamble();
    print "No video file was specified\n";
    print "Use -f switch to specify the video file\n";
    print 'Example: -f "C:\Program Files\movie.avi"', "\n";
    print 'Example: -f "/home/pkrumins/super.cool.video.wmv"', "\n";
    exit 1;
}

unless (-r $opts{f}) {
    preamble();
    print "Video file is not readable or does not exist\n";
    print "Check the permissions and the path to the file\n";
    exit 1;
}

unless (defined $opts{c} && length $opts{c}) {
    preamble();
    print "No video category was specified\n";
    print "Use -c switch to set the category of the video\n";
    print "Example: -c SCI_TECH would set category to \"Science & Technology\"\n\n";
    print_cats();
    exit 1;
}

unless (defined $cats{$opts{c}}) {
    preamble();
    print "Category '$opts{c}' does not exist\n\n";
    print_cats();
    exit 1;
}

unless (defined $opts{t} && length $opts{t}) {
    preamble();
    print "No video title was specified\n";
    print "Use -t switch to set the title of the video\n";
    print 'Example: -t "Super Cool Video Title"', "\n";
    exit 1;
}

unless (defined $opts{d} && length $opts{d}) {
    preamble();
    print "No video description was specified\n";
    print "Use -d switch to set the description of the video\n";
    print 'Example: -d "The coolest video description"', "\n";
    exit 1;
}

# create the user agent, have it store cookies and
# pretend to be a cool windows firefox browser
my $ua = LWP::UserAgent->new(
    cookie_jar => {},
    agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) '.
             'Gecko/20070515 Firefox/2.0.0.4'
);

# let the user agent follow redirects after a POST request
push @{$ua->requests_redirectable}, 'POST';

print "Logging in to Google Video...\n";
login();

print "Uploading the video ($opts{t})...\n";
upload();

print "Done!\n";

sub login {
    # submit the login form
    my $res = $ua->post($login_url,
        {
            continue     => 'http://video.google.com/',
	    skipvpage    => 'true',
	    sendvemail   => 'false',
	    hl           => 'en',
            Email        => $opts{l},
            Passwd       => $opts{p},
	    PersistentCookie => 'yes',
	    rmShown      => '1',
	    signIn       => 'Sign in'
        }
    );
    unless ($res->is_success) {
        die "Failed logging in: failed submitting login form: ",
            $res->status_line;
    }

    # We have no good way to check if we really logged in.
    # I found that when you have logged in the upper right menu changes
    # and you have access to 'Log Out' option.
    # We check for this string to see if we have logged in.
    unless ($res->content =~ /Redirecting/) {
        die "Failed logging in: username/password incorrect";
    }
}

sub upload {
    # let's prepare the video field hash which we will need in both steps
    my %vid_fields = (
        baseurl                   => 'http://video.google.com',
	domain                    => 'video.google.com',
	countrycode               => 'FI',
	hl                        => 'en',
        title                     => $opts{t},
        description               => $opts{d},
        genre                     => $opts{c},
        language                  => "FI",
        access                    => "unlisted",
	tos                       => "agree",
        ignore_broadcast_settings => 0,
        s                         => "Upload video"
    );
    $vid_fields{'video-file'} = [ $opts{f} ];

    my $resp = upload_step($upload_url, \%vid_fields);

    # After the video has been uploaded, youtube thanks the user
    # for uploading the vid. Let's test for this thanks message
    # to see if the upload was successful
    print $resp->content;
    unless ($resp->content =~ /Upload Complete/) {
        die "Upload might have failed, no 'thanks for upload' message ",
            "was found!.\n",
            "Or Google Video might have redesigned!";
    }
}

sub extract_session_token {
    my $content = shift;

    if ($content =~ m{token_elem\.setAttribute\('value', '(.+?)'\);}) {
        return $1;
    }

    return;
}

sub upload_step {
    my ($url, $vid_fields) = @_;
    my $resp = $ua->post($url, $vid_fields,
        "Content_Type" => "form-data");

    print $resp->content;
    unless ($resp->is_success) {
        die "Upload step failed: ", $resp->status_line;
    }

    return $resp;
}

sub HELP_MESSAGE {
    preamble();
    print "Usage: $0 ",
          "-l [login] ",
          "-p [password] ",
          "-f  ",
          "-c  ",
          "-t  ",
          "-d  ";
    print_cats();
}

sub print_cats {
    print "Possible categories (for -c switch):\n";
    printf "%-4s - %s\n", $_, $cats{$_} foreach (sort {
        $cats{$a} cmp $cats{$b}
    } keys %cats);
}

sub VERSION_MESSAGE {
    preamble();
    print "Version: v", VERSION, "\n";
}

sub preamble {
    print "Google Video uploader by Mika Helander\n";
    print "Modified from Peteris Krumins YouTube version (peter\@catonmat.net)\n";
    print "http://www.catonmat.net  -  good coders code, great reuse\n";
    print "\n"
}

Cheers,
-Mika

Reply To This Comment

(why do I need your e-mail?)

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

Type the word "sandbox": (just to make sure you're a human)

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