Radreise-Wiki:DownloadRoute.pl
Aus Radreise-Wiki
Version vom 8. September 2011, 09:45 Uhr von Jmages (Diskussion | Beiträge)
Das Perl-Skript downloadRoute.pl wird mit dem Streckennamen als Parameter aufgerufen. Ein Beispiel ist
perl downloadRoute.pl Rhein
Daraufhin loggt sich das Skript selbsttätig ins Wiki ein (Internetverbindung vorausgesetzt) und lädt den Source-Text der Strecke ins text-Verzeichnis.
use strict; # Encoding Stuff use Encode qw(encode decode); my $encoding = 'utf-8'; my $encOut; $encOut = 'cp1252'; # Windows Ansi $encOut = 'cp850' ; # DOS Fenster use URI::Escape qw( uri_escape_utf8 ); # Mechanize Stuff use WWW::Mechanize; use HTTP::Cookies; # MD5 Checksum und Zip-Funktionalität use Digest::MD5 qw(md5_hex); use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); # Default Directories require "ini.pl"; my ($baseDir, $outDir, $username, $password) = getIni(); my $routeFileName = ""; my $routeURL = ""; if ($#ARGV != -1) { $routeFileName = join " ", @ARGV; if ($routeFileName =~ / \($/) { $routeFileName .= "retour)"; } my $urlstring = uri_escape_utf8($routeFileName); $urlstring =~ s/%20/\_/g; $urlstring =~ s/\(/%28/g; $urlstring =~ s/\)/%29/g; $urlstring =~ s/\'/%27/g; $routeURL = $urlstring; } else { my @content; open FIN, "$baseDir/RouteList.txt" or die "$! : $baseDir/RouteList.txt"; while (<FIN>) { push @content, $_; } close (FIN); my @content_s = reverse sort @content; my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0]; $routeFileName = $name; my $urlstring = uri_escape_utf8($routeFileName); $urlstring =~ s/%20/\_/g; $urlstring =~ s/\(/%28/g; $urlstring =~ s/\)/%29/g; $urlstring =~ s/\'/%27/g; $routeURL = $urlstring; } print encode $encOut, "\nGetting $routeFileName\n"; print encode $encOut, "Getting $routeURL\n"; # Initialisieren von Mechanize-Browser und Cookie-Jar my $mech = WWW::Mechanize->new( autocheck => 1 ); $mech->agent_alias( 'Windows Mozilla' ); my $cookie_jar = HTTP::Cookies->new( file => "cookies.txt", autosave => 1 ); $mech->cookie_jar($cookie_jar); # Einloggen ins Radreise-Wiki um in den Edit-Modus zu kommen $mech->get("http://www.radreise-wiki.de/Spezial:Userlogin"); $mech->form_number('1'); $mech->field("wpName", $username); $mech->field("wpPassword", $password); $mech->tick('wpRemember', '1'); $mech->click(); $mech->get("http://www.radreise-wiki.de/$routeURL"); my $website = $mech->content; # Gehe zum Editieren der Route $mech->follow_link( url => "/index.php?title=$routeURL&action=edit" ); $website = $mech->content; $website =~ /<textarea.*>(.*)<\/textarea>/s; my $wikiText = $1; $wikiText =~ s/</</g; $wikiText =~ s/>/>/g; $wikiText =~ s/"/\"/g; $wikiText =~ s/&/\&/g; if (length $wikiText > 1) { &saveFileIfChangedOrNew ("$baseDir/text/", "$routeFileName.txt", encode $encoding, $wikiText, 0); } else { die "URL is not existent: $routeURL\n"; } exit; #--------------------------------------------------------- sub saveFileIfChangedOrNew { (my $path, my $fileName, my $file, my $kmz) = @_; my $change = 0; if (open(FILE, "< $path$fileName")) { my $md5 = Digest::MD5->new; while (<FILE>) { $md5->add($_); } close(FILE); my $md5_old = $md5->hexdigest; $md5 = Digest::MD5->new; $md5->add($file); my $md5_new = $md5->hexdigest; unless ($md5_old eq $md5_new) { $change = 1; print encode 'cp850', "****************** Overwriting file $fileName ******************\n"; open FT,"> $path$fileName"; print FT $file; close FT; if ($kmz) { my $zip = Archive::Zip->new(); $zip->addFile("$path$fileName",$fileName); $fileName =~ s/\.kml/\.kmz/; unless ( $zip->writeToFileNamed("$path$fileName") == AZ_OK ) { die 'write error'; } } } } else { $change = 1; print encode 'cp850', "****************** Creating new file $fileName ******************\n"; open FT,"> $path$fileName"; print FT $file; close FT; if ($kmz) { my $zip = Archive::Zip->new(); $zip->addFile("$path$fileName",$fileName); $fileName =~ s/\.kml/\.kmz/; unless ( $zip->writeToFileNamed("$path$fileName") == AZ_OK ) { die 'write error'; } } } return $change; }