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

Aus Radreise-Wiki
 
(8 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:'''
'''Quellcode des Skripts:'''


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

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;