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

Aus Radreise-Wiki
 
 
(11 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 3: Zeile 3:
  perl downloadRoute.pl Rhein
  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.
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:'''


  <nowiki>
  <nowiki>
use strict;
use strict;
 
# Encoding Stuff
# Encoding Stuff
use Encode qw(encode decode);
use Encode qw(encode decode);
my $encoding = 'utf-8';
 
my $encOut;
# Mechanize Stuff
$encOut  = 'cp1252'; # Windows Ansi
use WWW::Mechanize;
$encOut  = 'cp850' ; # DOS Fenster
use HTTP::Cookies;
use URI::Escape qw( uri_escape_utf8 );
 
# Default Settings
# Mechanize Stuff
push @INC, '.';
use WWW::Mechanize;
require "ini.pl";
use HTTP::Cookies;
my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();
 
# MD5 Checksum und Zip-Funktionalität
my $routeFileName = "";
use Digest::MD5
my $routeURL      = "";
qw(md5_hex);
 
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
if ($#ARGV != -1) {
 
# Default Directories
$routeFileName = join " ", @ARGV;
require "ini.pl";
 
my ($baseDir, $eigDir, $username, $password) = getIni();
if ($routeFileName =~ / \($/) {  
my $routeFileName = "";
$routeFileName .= "retour)";
my $routeURL      = "";
}
 
if ($#ARGV != -1) {
$routeURL = encodeRouteNameURL($routeFileName);
 
$routeFileName = join " ", @ARGV;
} else {
 
if ($routeFileName =~ / \($/) {  
my @content;
 
$routeFileName .= "retour)";
open FIN, "$baseDir/RouteList.txt" or die "$! : $baseDir/RouteList.txt";
}
 
while (<FIN>) {  
my $urlstring  = uri_escape_utf8($routeFileName);
 
my $line = decode $encoding, $_;
$urlstring =~ s/%20/\_/g;
 
$urlstring =~ s/\(/%28/g;
push @content, $line;  
$urlstring =~ s/\)/%29/g;
}
$urlstring =~ s/\'/%27/g;
 
close (FIN);
$routeURL = $urlstring;
my @content_s = reverse sort @content;
} else {
my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];
my @content;
 
chomp $name;
open FIN, "$baseDir/RouteList.txt" or die "$! : $baseDir/RouteList.txt";
while (<FIN>) { push @content, $_; }
$routeFileName = encode $encAnsi, $name;
close (FIN);
 
$routeURL = encodeRouteNameURL($routeFileName);
my @content_s = reverse sort @content;
}
 
my ($dateTXT, $dateKML, $type, $name, $url) = split " :: ", $content_s[0];
print encode $encOut, "\nGetting $routeFileName\n";
 
$routeFileName = $name;
#=====================================================
$routeURL     = $url;
# Initialisieren von Mechanize-Browser und Cookie-Jar
 
}
use WWW::Mechanize;
use HTTP::Cookies;
print encode $encOut, "\nGetting $routeFileName\n";
my $mech = WWW::Mechanize->new(  
print encode $encOut,  "Getting $routeURL\n";
ssl_opts => {
SSL_verify_mode => '0',
# Initialisieren von Mechanize-Browser und Cookie-Jar
verify_hostname => 0
my $mech = WWW::Mechanize->new( autocheck => 1 );
}
$mech->agent_alias( 'Windows Mozilla' );
);
my $cookie_jar = HTTP::Cookies->new( file => "cookies.txt", autosave => 1 );
$mech->agent_alias( 'Windows Mozilla' );
$mech->cookie_jar($cookie_jar);
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");
# Einloggen ins Radreise-Wiki um in den Edit-Modus zu kommen
$mech->form_number('1');
$mech->get("http://www.radreise-wiki.de/Spezial:Userlogin");
$mech->field("wpName", $username);
$mech->form_number('1');
$mech->field("wpPassword", $password);
$mech->field("wpName", $username);
$mech->tick('wpRemember', '1');
$mech->field("wpPassword", $password);
$mech->click();
$mech->tick('wpRemember', '1');
$mech->click();
$mech->get("http://www.radreise-wiki.de/$routeURL");
 
my $website = $mech->content;
$mech->get("http://www.radreise-wiki.de/index.php?title=$routeURL&action=edit");
my $website = $mech->content;
# Gehe zum Editieren der Route
 
$mech->follow_link( url => "/index.php?title=$routeURL&action=edit" );
$website =~ /<textarea.*?>(.*)<\/textarea>/s;
$website = $mech->content;
 
my $wikiText = $1;
$website =~ /<textarea.*>(.*)<\/textarea>/s;
 
$wikiText =~ s/&lt;/</g;
my $wikiText = $1;
$wikiText =~ s/&gt;/>/g;
$wikiText =~ s/&quot;/\"/g;
$wikiText =~ s/&lt;/</g;
$wikiText =~ s/&amp;/\&/g;
$wikiText =~ s/&gt;/>/g;
 
$wikiText =~ s/&quot;/\"/g;
if (length $wikiText > 1) {
$wikiText =~ s/&amp;/\&/g;
 
&saveFileIfChangedOrNew ("$baseDir/text/", "$routeFileName.txt", encode $encoding, $wikiText, 0);
if (length $wikiText > 1) {
 
} else {
&saveFileIfChangedOrNew ("$baseDir/text/", "$routeFileName.txt", encode $encoding, $wikiText, 0);
die "URL is not existent: $routeURL\n";
}
} else {
 
die "URL is not existent: $routeURL\n";
#===========================
}
# Update KMZ File from Wiki
 
exit;
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");
sub saveFileIfChangedOrNew {
 
my $zip = Archive::Zip->new();
(my $path, my $fileName, my $file, my $kmz) = @_;
 
my $status = $zip->read("$dir_kmz/$routeFileName.kmz");
my $change = 0;
 
if ($status != 'AZ_OK') {
if (open(FILE, "< $path$fileName")) {
 
die "Error while reading KMZ file! $!";
my $md5 = Digest::MD5->new;
 
while (<FILE>) {
} else {
$md5->add($_);
 
}
my @members = $zip->memberNames();
close(FILE);
 
if ($#members != 0) { die "Error: More than one File within kmz!\n"; }
my $md5_old = $md5->hexdigest;
 
print encode $encOut, "     Renaming $members[0] -> $routeFileName.kml\n";
$md5 = Digest::MD5->new;
 
$md5->add($file);
$zip->extractTree(undef, "$dir_kmz/");
my $md5_new = $md5->hexdigest;
 
rename("$dir_kmz/$members[0]","$dir_kmz/$routeFileName.kml") || die "Error while Renaming File: $!";
unless ($md5_old eq $md5_new) {
 
my $ucontent = "";
$change = 1;
open FILE, encode $encAnsi, "$dir_kmz/$routeFileName\.kml" or die $!;
print encode 'cp850', "****************** Overwriting file $fileName ******************\n";
while (<FILE>) { $ucontent .= $_; }
open  FT,"> $path$fileName";
close FILE;
print FT $file;
my $content = decode $encoding, $ucontent;
close FT;
 
if ($content =~ /<LineString>.*?<coordinates>\s*(.*?)\s*<\/coordinates>/s) {
if ($kmz) {
 
my $track = $1;
my $zip = Archive::Zip->new();
 
$zip->addFile("$path$fileName",$fileName);
if ($track =~ /\n/) {
$fileName =~ s/\.kml/\.kmz/;
 
unless ( $zip->writeToFileNamed("$path$fileName") == AZ_OK ) {
die "Error: Linebreak within Track\n";
  die 'write error';
}
}
}
&saveFileIfChangedOrNew ("$dir_tracks/", "$routeFileName.txt", encode $encoding, $track, 0);
}
 
} else {
} else {
 
$change = 1;
print "  Error: No Match in $routeFileName\n";
print encode 'cp850', "****************** Creating new file $fileName ******************\n";
die;
open  FT,"> $path$fileName";
}
print FT $file;
}  
close FT;
 
exit;
if ($kmz) {
</nowiki>
 
my $zip = Archive::Zip->new();
[[Kategorie: Verwaltungs-Software]]
$zip->addFile("$path$fileName",$fileName);
$fileName =~ s/\.kml/\.kmz/;
unless ( $zip->writeToFileNamed("$path$fileName") == AZ_OK ) {
  die 'write error';
}
}
}
return $change;
}
</nowiki>

Aktuelle Version vom 5. Juli 2023, 08:33 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

use WWW::Mechanize;
use HTTP::Cookies;
my $mech = WWW::Mechanize->new( 
	ssl_opts => {
		SSL_verify_mode => '0',
		verify_hostname => 0
	} 
);
$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;