rapidshare upload perl script for linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 8anos
    New Member
    • Feb 2007
    • 3

    rapidshare upload perl script for linux

    Hello,
    I am new at the community and newbie at programming :)
    As you may know rapidshare provides a perl script for linux, to upload files at their servers.
    You can find the original scripts at rapidshare news :

    Code:
    http://images.rapidshare.com/software/rsapi.pl

    If you test it you will see that you can upload one file at time.
    I try to modify it in that way that script can read a text file with the names of the files i want to upload.
    But the script works only if there one name file at the text.
    If i try with two files (lets say File Finder_48x48.pn g and Dashboard_128x1 28.png) i take this result:

    Code:
    thanos@localhost ~/rapid2 $ perl rsapiteliko.pl Unsuccessful stat on filename containing newline at rsapiteliko.pl line 52. File Finder_48x48.png Dashboard_128x128.png is empty or does not exist!
    When the text file contains only one name file it works :confused:
    The script as i modified is this :


    Code:
    #!/usr/bin/perl 
    
    # RapidShare AG OpenSource Perl Uploader V1.0. For non-commercial use only. All rights reserved.
    # Included: Uploading to free, collector's and premium-zone. The MD5-check after uploads checks if the upload worked.
    # NOT included in this version: Upload-resume via new RS API.
    # This is a PERL script written for experts and for coders wanting to know how to write own upload programs.
    # Tested under Linux and Linux only.
    # If you write your own upload-tools, please look at our rsapi.cgi calls. You need them to have fun.
    #
    # To upload a file, put this script on a machine with perl installed and use the following syntax:
    # perl rsapi.pl free mytestfile.rar        (this uploads mytestfile.rar as a free user)
    # perl rsapi.pl prem archive.rar 334 test  (this uploads archive.rar to the premium-zone of login 334 with password test)
    # perl rsapi.pl col a.rar testuser mypw    (this uploads a.rar to the collector's-zone of login testuser with password mypw)
    #
    # We will publish another version with upload resume enabled soon, but this script actually works and we actually
    # want you to understand how it works and upload resume would make this script even more complex.
    
    
    #Edited by 8anos
    #Find the original script here: http://images.rapidshare.com/software/rsapi.pl
    
    
    
    
    use strict;
    use warnings;
    use Digest::MD5("md5_hex");
    use Fcntl;
    use IO::Socket;
    
    my ($x, $text, $file, $filename, $uploadpath, $size, $socket, $uploadserver, $cursize, $fh, $bufferlen, $buffer, $boundary, $header, $contentheader,
    $contenttail, $contentlength, $result, $maxbufsize, $md5hex, $filecontent, $size2, %key_val, $login, $password, $zone);
    
    
    
    # This chapter sets some vars and parses some vars.
    $/ = undef;
    $text ="up.txt"; #This is text with the files which you want to upload
    open (INFO, "$text") || die("Could not open file!");
    my @plirof=<INFO>;
    close (INFO);
    
    foreach $x (@plirof) {
    
    $file = $x || die "Syntax: $0 <filename to upload> <free|prem|col> [login] [password]\n";
    $zone = 'prem'; #If you use Free Rapidshare change "prem" to free
    $login = 'username'; # Your username, if your previous choise is free you havent to change it
    $password = 'password'; #Your password σας, if your previous choise is free you havent to change it
    $maxbufsize = 64000;
    $uploadpath = "l3";
    $cursize = 0;
    $size = -s $file || die "File $file is empty or does not exist!\n";
    $filename = $file =~ /[\/\\]([^\/\\]+)$/ ? $1 : $file;}
    
    
    
     #This chapter checks the file and calculates the MD5HEX of the existing local file.
    print "File $file has $size bytes. Calculating MD5HEX...\n";
    open(FH, $file) || die "Unable to open file: $!\n";
    $filecontent = <FH>;
    close(FH);
    $md5hex = uc(md5_hex($filecontent));
    $size2 = length($filecontent);
    print "MD5HEX is $md5hex ($size2 bytes analyzed.)\n";
    unless ($size == $size2) { die "Strange error: $size bytes found, but only $size2 bytes analyzed?\n" }
    
    
    
    # This chapter finds out which upload server is free for uploading our file by fetching (URL address blocked: See forum rules)/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1
    if ($login and $password) { print "Trying to upload to your premium account.\n" } else { print "Uploading as a free user.\n" }
    print "Uploading as filename '$filename'. Getting upload server infos.\n";
    $socket = IO::Socket::INET->new(PeerAddr => "rapidshare.com:80") || die "Unable to open port: $!\n";
    print $socket qq|GET /cgi-bin/rsapi.cgi?sub=nextuploadserver_v1 HTTP/1.0\r\n\r\n|;
    ($uploadserver) = <$socket> =~ /\r\n\r\n(\d+)/;
    unless ($uploadserver) { die "Uploadserver invalid? Internal error!\n" }
    print "Uploading to ul$uploadserver$uploadpath.rapidshare.com\n";
    
    
    
    # This chapter opens our file and the TCP socket to the upload server.
    foreach $x (@plirof) {
    
    $file = $x;
    sysopen($fh, $file, O_RDONLY) || die "Unable to open file: $!\n";
    $socket = IO::Socket::INET->new(PeerAddr => "ul$uploadserver$uploadpath.rapidshare.com:80") || die "Unable to open port: $!\n";}
    
    
    
    # This chapter constructs a (somewhat RFC valid) HTTP header. See how we pass rsapi_v1=1 to the server to get a program-friendly output.
    $boundary = "---------------------632865735RS4EVER5675865";
    $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="rsapi_v1"\r\n\r\n1\r\n|;
    
    if ($zone eq "prem" and $login and $password) {
      $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="login"\r\n\r\n$login\r\n|;
      $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="password"\r\n\r\n$password\r\n|;
    }
    
    if ($zone eq "col" and $login and $password) {
      $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="freeaccountid"\r\n\r\n$login\r\n|;
      $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="password"\r\n\r\n$password\r\n|;
    }
    
    $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="filecontent"; filename="$filename"\r\n\r\n|;
    $contenttail = "\r\n$boundary--\r\n";
    $contentlength = length($contentheader) + $size + length($contenttail);
    $header = qq|POST /cgi-bin/upload.cgi HTTP/1.0\r\nContent-Type: multipart/form-data; boundary=$boundary\r\nContent-Length: $contentlength\r\n\r\n|;
    
    
    
    #This chapter actually sends all the data, header first, to the upload server.
    print $socket "$header$contentheader";
    
    while ($cursize < $size) {
      $bufferlen = sysread($fh, $buffer, $maxbufsize, 0) || 0;
      unless ($bufferlen) { die "Error while sending data: $!\n" }
      print "$cursize of $size bytes sent.\n";
      $cursize += $bufferlen;
      print $socket $buffer;
    }
    
    print $socket $contenttail;
    
    
    
    # OK, all is sent. Now lets fetch the server's reponse and analyze it.
    print "All $size bytes sent to server. Fetching result:\n";
    ($result) = <$socket> =~ /\r\n\r\n(.+)/s;
    unless ($result) { die "Ooops! Did not receive any valid server results?\n" }
    print "$result >>> Verifying MD5...\n";
    
    foreach (split(/\n/, $result)) {
      if ($_ =~ /([^=]+)=(.+)/) { $key_val{$1} = $2 }
    }
    
    
    
    # Now lets check if the result contains (and it should contain) the MD5HEX of the uploaded file and check if its identical to our MD5HEX.
    unless ($key_val{"File1.4"}) { die "Ooops! Result did not contain MD5? Maybe you entered invalid login data.\n" }
    if ($md5hex ne $key_val{"File1.4"}) { die qq|Upload FAILED! Your MD5HEX is $md5hex, while the uploaded file has MD5HEX $key_val{"File1.4"}!\n| }
    print "MD5HEX value correct. Upload completed without errors. Saving links to rsulres.txt\n\n\n";
    
    
    
    # Maybe you want the links saved to a logfile? Here we go.
    open(O, ">>rsulres.txt");
    print O $result . "\n";
    close(O);
    
    
    
    # Thats it. Have fun experimenting with this script. Now lets say...
    exit;

    I will be happy if anyone can help.
    Thanks in advance :)
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Welcome

    Don't modify their script. Create a new script that will wrap around theirs and give you this increased functionality. By modifying their code you risk introducing errors, and you potentially force yourself to have to make updates again if they update their software and protocol.

    Simply create a new perl script that parses a text file for file names and then simply calls the rapid script for each file individually.

    If you have any problems doing this, feel free to ask here.

    Comment

    • 8anos
      New Member
      • Feb 2007
      • 3

      #3
      Originally posted by miller

      Simply create a new perl script that parses a text file for file names and then simply calls the rapid script for each file individually.

      If you have any problems doing this, feel free to ask here.
      Thank you for your answer.
      I' ll try it. :)

      Comment

      • 8anos
        New Member
        • Feb 2007
        • 3

        #4
        Originally posted by miller

        Simply create a new perl script that parses a text file for file names and then simply calls the rapid script for each file individually.

        If you have any problems doing this, feel free to ask here.
        well now i need your help.

        How i call the original rs script?

        here is a script that makes multiple text files with file names
        Code:
        
        my ($filepath, $file3);
        print "Give the path of the directory\n\n ";
        
        $filepath=<STDIN>;
        chop $filepath;
        
        my $count=1;
        my $new1= "new1.txt";
        
        #Open and read the directory
        opendir (FILEPATH, $filepath) || die "cannot open $filepath!!";
        while ($file3=readdir FILEPATH) {
        
        open (NF, ">$new1");
        print NF "$filepath/$file3\n" ;
        
        
        close NF;
        
        
        $new1++
        
        }
        any help?
        thanks in advance :)

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          There are lots of different methods to call another script. There's always backticks or a system call. I would recommend that you call do though



          Code:
          {
             local @ARGV = ("free", "$filepath/$file3"); # Set parameters
             do "rsapi.pl";
          }

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            [
            Code:
            {
               local @ARGV = ("free", "$filepath/$file3"); # Set parameters
               do "rsapi.pl";
            }
            Isn't that just too cool! :)

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              maybe if you comment out this line in the rsapi.pl file it will work for multiple files:

              Code:
              $/ = undef;
              change it to:

              Code:
              #$/ = undef;
              and chomp the array to be safe:

              Code:
              my @plirof=<INFO>;
              chomp(@plirof);

              Comment

              • jdportales
                New Member
                • Nov 2007
                • 1

                #8
                Im confused

                Does anyone has the complete Script for multiple files upload Rapidshare???

                because I read many little parts in this thread .. but not a final

                Thanks !!

                Comment

                • pnocti
                  New Member
                  • Nov 2007
                  • 1

                  #9
                  I took the shortcut and wrapped rsapi.pl inside a bash script. I didn't want to mess with their code.

                  for i in *;do rsapi "$i" col username passwd;done

                  lame I know but it works.

                  Comment

                  • wildjohn
                    New Member
                    • Jan 2008
                    • 1

                    #10
                    Sorry for bringing it up but i really would like to know if someone has managed to make the perl script for multiple files and post it here.

                    thank you in advance

                    Comment

                    Working...