Radreise-Wiki:DownloadRoute.pl: Unterschied zwischen den Versionen
Aus Radreise-Wiki
Jmages (Diskussion | Beiträge) |
Jmages (Diskussion | Beiträge) |
||
Zeile 8: | Zeile 8: | ||
<nowiki> | <nowiki> | ||
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"; | |||
} | |||
exit; | |||
</nowiki> |
Version vom 19. April 2012, 05:57 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"; } exit;