Wednesday, June 18, 2008

Rapidshare downloader - Perl+Bash



Update: RS redesigned their site and this script no longer works. (July 3, 2008)

Hi frndz,

Today I wrote a Perl script to download a set of files from www.rapidshare.com at "Happy Hours" for free users. This works in Linux/Unix only and only if you have a pppoe connection. It uses WWW::Mechanize module which is not available with standard Perl library. You will need to install it separately. This script works with current design of rapidshare site. If they make changes in site, this may fail. The usage is:

perl RSDownloader.pl [-d download_directory] links.txt

This program will wait till "Happy Hours" (will check every 10 mins) and will start downloading rapidshare links in links.txt. The program will ignore blank lines and lines starting with a '#' in the input file. If -d flag is omitted, files will be downloaded to current working directory. You can also group your downloads simply by putting a name in one line and all following links till next name will be saved to a directory with that name. eg:

In.Harihar.Nagar
htt...rapidshare.com/files/115665/In.Harihar.Nagar...part5.rar
htt...rapidshare.com/files/115685/In.Harihar.Nagar...part6.rar
htt...rapidshare.com/files/115448/In.Harihar.Nagar...part7.rar
Aaram.Thamburan
htt...rapidshare.com/files/101625/Aaram.Thamburan...part01.rar
htt...rapidshare.com/files/101841/Aaram.Thamburan...part02.rar
htt...rapidshare.com/files/101856/Aaram.Thamburan...part03.rar
htt...rapidshare.com/files/101869/Aaram.Thamburan...part04.rar
htt...rapidshare.com/files/102124/Aaram.Thamburan...part05.rar
Punjabi.House
htt...rapidshare.com/files/94088/Punjabi.House...part01.rar
htt...rapidshare.com/files/94095/Punjabi.House...part02.rar


Here is the code...

#!/usr/bin/perl -w

use WWW::Mechanize;
use Getopt::Std;

my $mech = WWW::Mechanize->new();
my $pwd = `pwd`;
chomp($pwd);

if (getopts('d:')) {
for (my $i = 0; $i <= $#ARGV; $i++) {
$ARGV[$i] = $pwd."/".$ARGV[$i] unless ($ARGV[$i] =~ m{^/});
}
chdir "$opt_d" or die "Couldn't cd to $opt_d: $!";
$pwd = `pwd`;
chomp($pwd);
}
else {
die "Error in option? $!";
}

while (<>) {
chomp;
# if input line is not a url, treat it as a directory name to create and cd.
unless (/http/) {
chdir "$pwd";
mkdir "$_", 0755 or warn "Failed to create directory $_: $!";
chdir "$_";
next;
}

print "Trying to download $_\n";

$mech->get("$_");

if ($mech->response()->content() =~ m{h1>Error./h1>}) {
print "Some server error. Skipping file: $_\n";
next;
}

$mech->submit_form("form_number", 2); # click the "Free" button
print "Selected Free download.\n";
$data = $mech->response()->content();
if ($data =~ /Happy Hour/) {
print "It\'s Happy Hour!\n";
if ($data =~ /form name="dl" action="(.*?)"/) { # get file url
print "Got free link.\n";
`wget $1`;
print "Reconnecting pppoe after 1 minutes.\n";
`poff -a;sleep 1m;pon dsl-provider;sleep 4`;
}
}
elsif ($data =~ /reached the limit/) { # didnt get new ip. try again.
print "Reached free user's limit.\n";
print "Reconnecting pppoe after 2 minutes.\n";
`poff -a;sleep 2m;pon dsl-provider;sleep 4`;
redo;
}
else {
print "Not happy hour :-(\nRetrying after 10 minutes\n";
for ($i = 1; $i <= 10; $i++) {
`sleep 1m`;
print " $i";
}
print "\n";
redo;
}
}

4 comments:

condo62 said...
This comment has been removed by the author.
Vivek said...

hey this is a good program.
Good that figured out how rapidshare stuff works and incorporated it into ur program.
U might check out digitalpbk for Arun's download manager.

Alan Haggai Alavi said...

Hi,

The script looks cool. A first glance look at the code showed that you are using backticks (`) to invoke system (Bash) commands. However, you should try to use their Perl counterparts. By using the Perl alternatives, you can be sure that your script will work across different operating systems.

For example:
`pwd` can be replaced by:
use Cwd qw( getcwd );
Cwd::getcwd


As for `sleep 1m`, you could have used Perl's own sleep function:
sleep 60

Also, when you do not need the output of a backtick command, you should use system(command) which discards the output.

The above are just few things I noticed in first glance. Just wanted to let you know.

You can check WWW::Rapidshare::Free Perl Module (which I released yesterday). It should be available at your nearest CPAN mirror in few hours. It uses a similar method, but gives the user control over almost everything.

Regards,
Alan Haggai Alavi.

Swatirocks said...

This one is good. Please visit Jains Website for uniting Jains and support the cause. You may help in it as you are good technically.