Radreise-Wiki:DownloadRoute.pl: Unterschied zwischen den Versionen
Aus Radreise-Wiki
Jmages (Diskussion | Beiträge) |
Jmages (Diskussion | Beiträge) |
||
Zeile 94: | Zeile 94: | ||
die "URL is not existent: $routeURL\n"; | die "URL is not existent: $routeURL\n"; | ||
} | } | ||
#=========================== | |||
# Update KMZ File from Wiki | |||
my $dir_kmz = "$baseDir/kmz"; | |||
$mech->get("http://www.radreise-wiki.de/images/$routeURL.kmz", ":content_file" => "$dir_kmz/$routeFileName.kmz"); | |||
my $zip = Archive::Zip->new(); | |||
my $status = $zip->read("$dir_kmz/$routeFileName.kmz"); | |||
if ($status != 'AZ_OK') { | |||
die "Error while reading KMZ file! $!"; | |||
} else { | |||
my @members = $zip->memberNames(); | |||
if ($#members != 0) { die "Error: More than one File within kmz!\n"; } | |||
print encode $encOut, " Renaming $members[0] -> $routeFileName.kml\n"; | |||
$zip->extractTree(undef, "$dir_kmz/"); | |||
rename("$dir_kmz/$members[0]","$dir_kmz/$routeFileName.kml") || die "Error while Renaming File: $!"; | |||
my $ucontent = ""; | |||
open FILE, encode $encAnsi, "$dir_kmz/$routeFileName\.kml" or die $!; | |||
while (<FILE>) { $ucontent .= $_; } | |||
close FILE; | |||
my $content = decode $encoding, $ucontent; | |||
if ($content =~ /<\/tessellate>\s*<coordinates>\s*(.*?)\s*<\/coordinates>/s) { | |||
my $track = $1; | |||
if ($track =~ /\n/) { | |||
die "Error: Linebreak within Track\n"; | |||
} | |||
print encode $encOut, " Saving $routeFileName.txt\n"; | |||
open FILE, ">$dir_kmz/$routeFileName\.txt" or die $!; | |||
print FILE $track; | |||
close FILE; | |||
} else { | |||
print " Error: No Match in $routeFileName\n"; | |||
die; | |||
} | |||
} | |||
exit; | exit; | ||
</nowiki> | </nowiki> |
Version vom 18. Mai 2012, 10:16 Uhr
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.
Quellcode des Skripts:
use strict; # Encoding Stuff use Encode qw(encode decode); # Mechanize Stuff use WWW::Mechanize; use HTTP::Cookies; # Default Settings require "ini.pl"; my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni(); my $routeFileName = ""; my $routeURL = ""; if ($#ARGV != -1) { $routeFileName = join " ", @ARGV; if ($routeFileName =~ / \($/) { $routeFileName .= "retour)"; } $routeURL = encodeRouteNameURL($routeFileName); } 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]; chomp $name; $routeFileName = $name; $routeURL = encodeRouteNameURL($routeFileName); } print encode $encOut, "\nGetting $routeFileName\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"; } #=========================== # Update KMZ File from Wiki my $dir_kmz = "$baseDir/kmz"; $mech->get("http://www.radreise-wiki.de/images/$routeURL.kmz", ":content_file" => "$dir_kmz/$routeFileName.kmz"); my $zip = Archive::Zip->new(); my $status = $zip->read("$dir_kmz/$routeFileName.kmz"); if ($status != 'AZ_OK') { die "Error while reading KMZ file! $!"; } else { my @members = $zip->memberNames(); if ($#members != 0) { die "Error: More than one File within kmz!\n"; } print encode $encOut, " Renaming $members[0] -> $routeFileName.kml\n"; $zip->extractTree(undef, "$dir_kmz/"); rename("$dir_kmz/$members[0]","$dir_kmz/$routeFileName.kml") || die "Error while Renaming File: $!"; my $ucontent = ""; open FILE, encode $encAnsi, "$dir_kmz/$routeFileName\.kml" or die $!; while (<FILE>) { $ucontent .= $_; } close FILE; my $content = decode $encoding, $ucontent; if ($content =~ /<\/tessellate>\s*<coordinates>\s*(.*?)\s*<\/coordinates>/s) { my $track = $1; if ($track =~ /\n/) { die "Error: Linebreak within Track\n"; } print encode $encOut, " Saving $routeFileName.txt\n"; open FILE, ">$dir_kmz/$routeFileName\.txt" or die $!; print FILE $track; close FILE; } else { print " Error: No Match in $routeFileName\n"; die; } } exit;