Perl :: Aufgabe #213

1 Lösung Lösung öffentlich

Geografische Daten von Städten abfragen und ausgeben bzw abspeichern

Fortgeschrittener - Perl von ZRX88 - 01.01.2019 um 13:43 Uhr
Die API https://www.distance24.org/api.xhtml bietet einen API Endpunkt zum Abfragen von Daten zu Städten.

Das Ziel ist, dass mehrere Städte ( beispielsweise Berlin, Hamburg und München) abgefragt werden, dann die Entfernung zwischen den Städten in der Console ausgegeben wird. Dazu soll für jede Stadt zusätzliche Infos wie Bevölkerung und die unterschiedlichen Travelguides ausgegeben werden.

Optional sollen die aufgelisteten Punkte als Datei abgespeichert werden. Wer will kann es auch als drei separate html Dateien abspeichern.

Lösungen:

vote_ok
von Gustl (6560 Punkte) - 03.01.2019 um 15:47 Uhr
Quellcode ausblenden Perl-Code
#!/usr/bin/perl

use strict;
use POSIX; 
use LWP::UserAgent;
use HTTP::Request;
use JSON;
use Data::Dumper; 

# Stätde festlegen deren Entfernung und Daten abgefragt werden soll
my @city = ("Hamburg", "Berlin" , "München", "Hamburg");                

# URL der Schnittstelle von distance24
my $url     = 'https://www.distance24.org/route.json?stops='.join("|",@city);

my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $header = HTTP::Request->new(GET => $url);
my $request = HTTP::Request->new('GET', $url, $header);
my $response = $ua->request($request);

if ($response->is_success){
  my $json_array = decode_json( $response->content );
  
  # Distance Array und Stops Array holen
  my @distances = @{$json_array->{distances}};
  my @stops     = @{$json_array->{stops}}; 
  my $index = scalar(@stops) - 1;
  foreach( 0..$index ){
    if( $_ < $index ){
      print "von ".$stops[$_]->{city}." nach ".$stops[($_+1)]->{city}." ".$distances[$_]." km\n";
    } 
    
    print "\nStadt: ".$stops[$_]->{city}."\n";
    print "Bevölkerung: ".$stops[$_]->{wikipedia}->{population}."\n";
    print "TravelGuides:\n";
    foreach( @{$stops[$_]->{travelGuides}} ){
      print "title: ".$_->{title}."\n";
      print "thumbnail: ".$_->{thumbnail}."\n";
    }
    print "\n";
  }
  
  #open DEBUG, ">debug.txt" or die ("Fehler beim schreiben $!");
  #print DEBUG Dumper $json_array; 
  #close DEBUG;
  
}
elsif ($response->is_error){
   print "Error:$url\n";
   print $response->error_as_HTML;
}


Ausgabe:

Konsolenausgabe:

von Hamburg nach Berlin 256 km

Stadt: Hamburg
Bevölkerung: 1796077
TravelGuides:
title: Discover Germany (Full Color Country Travel Guide)
thumbnail: http://ecx.images-amazon.com/images/I/51HDVZMiD3L._SL160_.jpg
title: Cool Hamburg (City Guides)
thumbnail: http://ecx.images-amazon.com/images/I/51qOwAWDulL._SL160_.jpg
title: Germany (Country Travel Guide)
thumbnail: http://ecx.images-amazon.com/images/I/51kMd0h1ssL._SL160_.jpg

von Berlin nach Munich 505 km

Stadt: Berlin
Bevölkerung: 3499879
TravelGuides:
title: DK Eyewitness Travel Guide: Berlin
thumbnail: http://ecx.images-amazon.com/images/I/51rA6J6kjFL._SL160_.jpg
title: Top 10 Berlin (EYEWITNESS TOP 10 TRAVEL GUIDE)
thumbnail: http://ecx.images-amazon.com/images/I/51Mfo4uVplL._SL160_.jpg
title: Top 10 Berlin (EYEWITNESS TOP 10 TRAVEL GUIDE)
thumbnail: http://ecx.images-amazon.com/images/I/515FFLJAxnL._SL160_.jpg

von Munich nach Hamburg 613 km

Stadt: Munich
Bevölkerung: 1420000
TravelGuides:
title: Lonely Planet Munich Bavaria & the Black Forest (Travel Guide)
thumbnail: http://ecx.images-amazon.com/images/I/51njKAH2keL._SL160_.jpg
title: Top 10 Munich (EYEWITNESS TOP 10 TRAVEL GUIDE)
thumbnail: http://ecx.images-amazon.com/images/I/51ZA0mc5SYL._SL160_.jpg
title: Discover Germany (Full Color Country Travel Guide)
thumbnail: http://ecx.images-amazon.com/images/I/51HDVZMiD3L._SL160_.jpg


Stadt: Hamburg
Bevölkerung: 1796077
TravelGuides:
title: Discover Germany (Full Color Country Travel Guide)
thumbnail: http://ecx.images-amazon.com/images/I/51HDVZMiD3L._SL160_.jpg
title: Cool Hamburg (City Guides)
thumbnail: http://ecx.images-amazon.com/images/I/51qOwAWDulL._SL160_.jpg
title: Germany (Country Travel Guide)
thumbnail: http://ecx.images-amazon.com/images/I/51kMd0h1ssL._SL160_.jpg
1800742

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.