Created
November 1, 2010 13:39
-
-
Save pitti/658179 to your computer and use it in GitHub Desktop.
studip-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -l | |
use WWW::Mechanize; | |
use Getopt::Std; | |
getopts('u:p:vdns:'); | |
$opt_u and $opt_p or usage(); | |
my $m = WWW::Mechanize->new(); | |
login($m); | |
$opt_d and download(); | |
$opt_n and news($m); | |
# $opt_t and termine(); | |
# STUDIP SETTINGS | |
# | |
sub login { | |
$login_page = "https://cas.uni-oldenburg.de/cas/login?service=https://elearning.uni-oldenburg.de/index.php%3Fagain%3Dyes%26cancel_login%3D1%26sso%3Dcas"; | |
$m = shift; | |
$m->get($login_page); | |
unless ($m->success()) { print "Could not surf to login page!\n"; exit; } | |
$opt_v and print "Successfully surfed to login page."; | |
$m->field('username',$opt_u); | |
$m->field('password', $opt_p); | |
my $res = $m->submit(); | |
if ( $res->content() =~ /Der Dienst oder die Authentifizierung konnten nicht beglaubigt werden/) { print "Wrong Username/Password!\n"; exit; } | |
$opt_v and print "Login OK, getting file list..."; | |
} | |
# sub news { | |
# $m = shift; | |
# | |
# my @lecture_links = $m->find_all_links( text_regex => qr/.*semester/, url_regex => qr/^seminar_main\.php\?auswahl=(\d*)$/); | |
# foreach (@lecture_links) { | |
# # only get lectures with the little disk icon | |
# $_->url() =~ /^seminar_main\.php\?auswahl=(\d*)$/; | |
# my $link = $m->find_link( url_regex => qr/auswahl=$1&redirect_to=folder\.php&cmd=(all|tree)$/); | |
# | |
# if(defined($link)) { # this lecture has data do download... | |
# $opt_v and print "Lecture $link will be downloaded.\n"; | |
# push(@lectures_with_data, $link); | |
# $_->text() =~ /^(.+) \((.+)\)$/; | |
# $sem = $2; | |
# $vl = $1; | |
# $sem =~ s/\// /; | |
# push(@term, $sem); | |
# push(@lecture,$vl); | |
# } | |
# } | |
# } | |
sub download { | |
my @lectures_with_data = (); | |
my @term = (); | |
my @lecture = (); | |
$m->get("https://elearning.uni-oldenburg.de/meine_seminare.php"); | |
my @lecture_links = $m->find_all_links( text_regex => qr/.*$opt_s/, url_regex => qr/^seminar_main\.php\?auswahl=([0-9a-f]+).*$/); | |
foreach (@lecture_links) { | |
$opt_v and print "Searching " . $_->text() . " for dl content..."; | |
$_->url() =~ /^seminar_main\.php\?auswahl=([0-9a-f]+)$/; | |
my $link = $m->find_link( url_regex => qr/auswahl=$1.*folder\.php/);# redirect_to=folder\.php.*$/); # &cmd=(all|tree)$/); | |
if(defined($link) && $link->url() =~ m/folder\.php/) { # this lecture has data do download... | |
$opt_v and print "Will update " . $_->text() . "..."; | |
push(@lectures_with_data, $link); | |
$_->text() =~ /^(.+) \((.+)\)$/; | |
$sem = $2; | |
$vl = $1; | |
$sem =~ s/\// /; | |
push(@term, $sem); | |
push(@lecture,$vl); | |
} | |
} | |
foreach(@lectures_with_data) { | |
# get lecture and term with regex... | |
my $v = shift(@lecture); | |
my $s = shift(@term); | |
print "Updating '$v ($s)'..."; | |
# create the directorys, if they dont exist | |
unless (-e $s) { mkdir($s); } | |
unless (-e "$s/$v") { mkdir("$s/$v"); } | |
$m->get($_->url . "&cmd=all"); # open page with files to download | |
@download_links = $m->find_all_links( url_regex => qr/^\/sendfile.php.*/); | |
foreach (@download_links) { | |
# download every data that does not exist | |
$_->url() =~ /file_name=(.*)$/; | |
unless(-e "$s/$v/$1") { | |
print "leeching $s/$v/$1..."; | |
$m->get($_->url); | |
my $file = open(FILE, "> $s/$v/$1") or die "Cannot create $s/$v/$1!"; | |
print FILE $m->content(); | |
close(FILE); | |
} else { | |
$opt_v and print "$s/$v/$1 exists locally, skipping..."; | |
} | |
} | |
} | |
} | |
sub usage { | |
$dir = `pwd`; | |
chomp($dir); | |
print STDERR << "EOF"; | |
This programm downloads every File available on the StudIP lectures you have subscribed. | |
When started, it creates a directory hierarchy with '<term> <year(s)>/<lecture>/<files>' | |
where the files will be stored. The base directory of this hierarchy is the same directory | |
where you execute this script from (in this case it would be '$dir'). | |
usage: $0 -u [username] -p [password] | |
-d : download files | |
-u [username] : specify the username used to log in | |
-p [password] : specify the password used to log in | |
-v : turn on verbose mode | |
example: $0 -u 1234567 -p abcdef | |
EOF | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment