Radreise-Wiki:DownloadRoute.pl: Unterschied zwischen den Versionen

Aus Radreise-Wiki
Zeile 8: Zeile 8:


  <nowiki>
  <nowiki>
use strict;
use strict;
 
# Encoding Stuff
# Encoding Stuff
use Encode qw(encode decode);
use Encode qw(encode decode);
 
# Mechanize Stuff
# Mechanize Stuff
use WWW::Mechanize;
use WWW::Mechanize;
use HTTP::Cookies;
use HTTP::Cookies;
 
# Default Settings
# Default Settings
require "ini.pl";
require "ini.pl";
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();
 
my $routeFileName = "";
my $routeFileName = "";
my $routeURL      = "";
my $routeURL      = "";
 
if ($#ARGV != -1) {
if ($#ARGV != -1) {
 
$routeFileName = join " ", @ARGV;
$routeFileName = join " ", @ARGV;
 
if ($routeFileName =~ / \($/) {  
if ($routeFileName =~ / \($/) {  
$routeFileName .= "retour)";
$routeFileName .= "retour)";
}
}
 
$routeURL = encodeRouteNameURL($routeFileName);
$routeURL = encodeRouteNameURL($routeFileName);
 
} else {
} else {
 
my @content;
my @content;
 
open FIN, "$baseDir/RouteList.txt" or die "$! : $baseDir/RouteList.txt";
open FIN, "$baseDir/RouteList.txt" or die "$! : $baseDir/RouteList.txt";
while (<FIN>) { push @content, $_; }
while (<FIN>) { push @content, $_; }
close (FIN);
close (FIN);
my @content_s = reverse sort @content;
my @content_s = reverse sort @content;
my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];
my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];
 
chomp $name;
chomp $name;
$routeFileName = $name;
$routeFileName = $name;
 
$routeURL = encodeRouteNameURL($routeFileName);
$routeURL = encodeRouteNameURL($routeFileName);
 
}
}
 
print encode $encOut, "\nGetting $routeFileName\n";
print encode $encOut, "\nGetting $routeFileName\n";
 
# Initialisieren von Mechanize-Browser und Cookie-Jar
# Initialisieren von Mechanize-Browser und Cookie-Jar
my $mech = WWW::Mechanize->new( autocheck => 1 );
my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech->agent_alias( 'Windows Mozilla' );
$mech->agent_alias( 'Windows Mozilla' );
my $cookie_jar = HTTP::Cookies->new( file => "cookies.txt", autosave => 1 );
my $cookie_jar = HTTP::Cookies->new( file => "cookies.txt", autosave => 1 );
$mech->cookie_jar($cookie_jar);
$mech->cookie_jar($cookie_jar);
 
# Einloggen ins Radreise-Wiki um in den Edit-Modus zu kommen
# Einloggen ins Radreise-Wiki um in den Edit-Modus zu kommen
$mech->get("http://www.radreise-wiki.de/Spezial:Userlogin");
$mech->get("http://www.radreise-wiki.de/Spezial:Userlogin");
$mech->form_number('1');
$mech->form_number('1');
$mech->field("wpName", $username);
$mech->field("wpName", $username);
$mech->field("wpPassword", $password);
$mech->field("wpPassword", $password);
$mech->tick('wpRemember', '1');
$mech->tick('wpRemember', '1');
$mech->click();
$mech->click();
 
$mech->get("http://www.radreise-wiki.de/$routeURL");
$mech->get("http://www.radreise-wiki.de/$routeURL");
my $website = $mech->content;
my $website = $mech->content;
 
# Gehe zum Editieren der Route
# Gehe zum Editieren der Route
$mech->follow_link( url => "/index.php?title=$routeURL&action=edit" );
$mech->follow_link( url => "/index.php?title=$routeURL&action=edit" );
$website = $mech->content;
$website = $mech->content;
 
$website =~ /<textarea.*>(.*)<\/textarea>/s;
$website =~ /<textarea.*>(.*)<\/textarea>/s;
 
my $wikiText = $1;
my $wikiText = $1;
 
$wikiText =~ s/&lt;/</g;
$wikiText =~ s/&lt;/</g;
$wikiText =~ s/&gt;/>/g;
$wikiText =~ s/&gt;/>/g;
$wikiText =~ s/&quot;/\"/g;
$wikiText =~ s/&quot;/\"/g;
$wikiText =~ s/&amp;/\&/g;
$wikiText =~ s/&amp;/\&/g;
 
if (length $wikiText > 1) {
if (length $wikiText > 1) {
 
&saveFileIfChangedOrNew ("$baseDir/text/", "$routeFileName.txt", encode $encoding, $wikiText, 0);
&saveFileIfChangedOrNew ("$baseDir/text/", "$routeFileName.txt", encode $encoding, $wikiText, 0);
 
} else {
} else {
die "URL is not existent: $routeURL\n";
die "URL is not existent: $routeURL\n";
}
}
 
exit;
exit;
</nowiki>
</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;