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 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 [-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:


Here is the code...

#!/usr/bin/perl -w

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

my $mech = WWW::Mechanize->new();
my $pwd = `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`;
else {
die "Error in option? $!";

while (<>) {
# 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 "$_";

print "Trying to download $_\n";


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

$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`;
else {
print "Not happy hour :-(\nRetrying after 10 minutes\n";
for ($i = 1; $i <= 10; $i++) {
`sleep 1m`;
print " $i";
print "\n";


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...


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 );

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.

Alan Haggai Alavi.

Anonymous 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.