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

Aus Radreise-Wiki
(kat)
Zeile 18: Zeile 18:


# Default Settings
# Default Settings
push @INC, '.';
require "ini.pl";
require "ini.pl";
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();

Version vom 12. Januar 2018, 18:24 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. Ausserdem wird das zugehörige kmz-File ins kmz-Verzeichnis heruntergeladen, entpackt und der reine GPS-Track in eine txt-Datei extrahiert und ins tracks-Verzeichnis verschoben.

Quellcode des Skripts:

use strict;

# Encoding Stuff
use Encode qw(encode decode);

# Mechanize Stuff
use WWW::Mechanize;
use HTTP::Cookies;

# Default Settings
push @INC, '.';
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>) { 

		my $line = decode $encoding, $_;

		push @content, $line; 
	}

	close (FIN);
	
	my @content_s = reverse sort @content;
	
	my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];

	chomp $name;
	
	$routeFileName = encode $encAnsi, $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/index.php?title=$routeURL&action=edit");
my $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";
my $dir_tracks = "$baseDir/tracks";

$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 =~ /<LineString>.*?<coordinates>\s*(.*?)\s*<\/coordinates>/s) {

		my $track = $1;

		if ($track =~ /\n/) {

			die "Error: Linebreak within Track\n";
		}
		
		&saveFileIfChangedOrNew ("$dir_tracks/", "$routeFileName.txt", encode $encoding, $track, 0);

	} else {

		print "   Error: No Match in $routeFileName\n";
		die;
	}
} 

exit;