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

Aus Radreise-Wiki
Zeile 4: Zeile 4:


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.
'''Quellcode des Skripts:'''


  <nowiki>
  <nowiki>
Zeile 10: Zeile 12:
  # Encoding Stuff
  # Encoding Stuff
  use Encode qw(encode decode);
  use Encode qw(encode decode);
my $encoding = 'utf-8';
my $encOut;
$encOut  = 'cp1252'; # Windows Ansi
$encOut  = 'cp850' ; # DOS Fenster
use URI::Escape qw( uri_escape_utf8 );
   
   
  # Mechanize Stuff
  # Mechanize Stuff
Zeile 20: Zeile 17:
  use HTTP::Cookies;
  use HTTP::Cookies;
   
   
# MD5 Checksum und Zip-Funktionalität
  # Default Settings
use Digest::MD5
qw(md5_hex);
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
  # Default Directories
  require "ini.pl";
  require "ini.pl";
  my ($baseDir, $outDir, $username, $password) = getIni();
  my ($baseDir, $outDir, $username, $password, $encoding, $encAnsi, $encOut) = getIni();
   
   
  my $routeFileName = "";
  my $routeFileName = "";
Zeile 41: Zeile 33:
  }
  }
   
   
  my $urlstring  = uri_escape_utf8($routeFileName);
  $routeURL = encodeRouteNameURL($routeFileName);
$urlstring =~ s/%20/\_/g;
$urlstring =~ s/\(/%28/g;
$urlstring =~ s/\)/%29/g;
$urlstring =~ s/\'/%27/g;
$routeURL = $urlstring;
   
   
  } else {
  } else {
Zeile 61: Zeile 46:
 
 
  my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];
  my ($dateTXT, $dateKML, $type, $name) = split " :: ", $content_s[0];
chomp $name;
 
 
  $routeFileName = $name;
  $routeFileName = $name;
   
   
  my $urlstring  = uri_escape_utf8($routeFileName);
  $routeURL = encodeRouteNameURL($routeFileName);
$urlstring =~ s/%20/\_/g;
$urlstring =~ s/\(/%28/g;
$urlstring =~ s/\)/%29/g;
$urlstring =~ s/\'/%27/g;
$routeURL = $urlstring;
   
   
  }
  }
   
   
  print encode $encOut, "\nGetting $routeFileName\n";
  print encode $encOut, "\nGetting $routeFileName\n";
print encode $encOut,  "Getting $routeURL\n";
   
   
  # Initialisieren von Mechanize-Browser und Cookie-Jar
  # Initialisieren von Mechanize-Browser und Cookie-Jar
Zeile 115: Zeile 96:
   
   
  exit;
  exit;
#---------------------------------------------------------
sub saveFileIfChangedOrNew {
(my $path, my $fileName, my $file, my $kmz) = @_;
my $change = 0;
if (open(FILE, "< $path$fileName")) {
my $md5 = Digest::MD5->new;
while (<FILE>) {
$md5->add($_);
}
close(FILE);
my $md5_old = $md5->hexdigest;
$md5 = Digest::MD5->new;
$md5->add($file);
my $md5_new = $md5->hexdigest;
unless ($md5_old eq $md5_new) {
$change = 1;
print encode 'cp850', "****************** Overwriting file $fileName ******************\n";
open  FT,"> $path$fileName";
print FT $file;
close FT;
if ($kmz) {
my $zip = Archive::Zip->new();
$zip->addFile("$path$fileName",$fileName);
$fileName =~ s/\.kml/\.kmz/;
unless ( $zip->writeToFileNamed("$path$fileName") == AZ_OK ) {
  die 'write error';
}
}
}
} else {
$change = 1;
print encode 'cp850', "****************** Creating new file $fileName ******************\n";
open  FT,"> $path$fileName";
print FT $file;
close FT;
if ($kmz) {
my $zip = Archive::Zip->new();
$zip->addFile("$path$fileName",$fileName);
$fileName =~ s/\.kml/\.kmz/;
unless ( $zip->writeToFileNamed("$path$fileName") == AZ_OK ) {
  die 'write error';
}
}
}
return $change;
}
  </nowiki>
  </nowiki>

Version vom 9. September 2011, 18:17 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;