#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Perl Video Upload Script


    Hello,
    I'm working on a video upload script written in perl for dailymotion but I got stuck on something. I keep getting an Error 500: Connection reset by peer.
    I tried to make the request as authentic as possible (headers, cookies, etc) but I still think there is a way by which they find out that I'm trying to upload the video using a script and not from the browser.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use LWP::UserAgent;
    use Getopt::Std;
    use Net::SSLeay;
    use Data::Dumper;
    use HTML::Entities;
    
    $Getopt::Std::STANDARD_HELP_VERSION = 1;
    
    # various urls
    my $login_url  = '(URL address blocked: See forum rules);
    my $upload_url = '(URL address blocked: See forum rules);
    
    # create the user agent, have it store cookies and
    # pretend to be an even cooler Linux firefox browser
    my $ua = LWP::UserAgent->new(
    	cookie_jar => {},
    	agent => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.6) Gecko/20070802 SeaMonkey/1.1.4'
    );
    
    # let the user agent follow redirects after a POST request
    push @{$ua->requests_redirectable}, 'POST';
    
    print "Logging in to DailyMotion…\n";
    login();
    
    print "Uploading the video…\n";
    upload();
    
    print "Done!\n";
    
    sub login {
        # submit the login form
        my $res = $ua->post($login_url,
            {
                username     => "dmtesting",
                password    => "dmtesting",
                login_submit  => "Login",
                form_name => "dm_pageitem_login"
            }
        );
        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 =~ /logout/){
            die $res->content;
            die "Failed logging in: username/password incorrect";
        }
    }
    
    sub upload {
        # upload is actually a two step process, first we set the video info,
        # and then we post the actual video file
        #
    
        my $resp = $ua->get($upload_url);
        unless ($resp->is_success) {
            die "Failed getting $upload_url: ", $resp->status_line;
        }
    
        my $form_action = extract_form_action($resp->content);
        unless (defined $form_action) {
            die "Failed extracting form action, DailyMotion might have redesigned!";
        }
    
        #print $form_action;
    
        # let’s prepare the video field hash which we will need in both steps
        my %vid_fields = (
            file    => [ "testmovie.avi" ],
            send    => "Upload"
        );
    
        $resp = upload_step_one(\%vid_fields, $form_action);
    }
    
    sub extract_form_action {
        my $content = shift;
    
        if ($content =~ m{<form action="([^"]+)" class="dmco_form_container dmco_form dmco_form" id="xupload_form" method="POST" enctype="multipart\/form-data">}) {
            return $1;
        }
    
        return;
    }
    
    sub upload_step_one {
        my ($vid_fields, $action_url) = @_;
    
        my $resp = $ua->post($action_url, $vid_fields,
            "Content_Type" => "form-data");
    
        unless ($resp->is_success) {
            die "First upload step failed: ", $resp->status_line;
        }
    
        return $resp;
    }
    You have to change only line 79 to use a video from your computer and after that is ready to run.
    Any help would be highly appreciated as I've been trying to find what is the problem for more than 12 hours by now and still no clue.

    Thanks in advance!
  2. #2
  3. Guru Meditation
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    Amsterdam
    Posts
    1,304
    Rep Power
    387
    Perhaps they are checking HTTP_REFERER and it looks like you are not setting it ...

    http://en.wikipedia.org/wiki/List_of_HTTP_headers

    So per LWP::UserAgent docs that I found very fuzzy it seems you should try to do something like:

    Code:
    sub upload_step_one {
        my ($vid_fields, $action_url) = @_;
    
        my $resp = $ua->post($action_url, $vid_fields,
            "Content_Type" => "form-data", "Referer" => $upload_url);
    
        unless ($resp->is_success) {
            die "First upload step failed: ", $resp->status_line;
        }
    
        return $resp;
    }
    If that doesn't work (and I'm assuming you made sure that you are including all the form fields in the final POST request), only thing I can suggest is to fire up tcpdump + wireshark and figure out what the browser is sending vs. what you are sending.

    Let us know what it was ...

    PS Best thing about programming problems is the feeling once you've solved them
    www.booking.com is hiring Perl developers!
    Work along some of the biggest names in Perl community. Live in Amsterdam - relocation assistance is provided (paperwork/visa and financial) for you and your family members - for details send me an message


IMN logo majestic logo threadwatch logo seochat tools logo