#!/usr/bin/perl
$|=1;

use strict;
use LWP::Simple qw(get getprint $ua);
$ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

my %requested = ();

my $wikipedia_url = "http://en.wikipedia.org/wiki/";
my $wikimedia_url = "http://tools.wikimedia.de/~magnus/geo/geohack.php?pagename=";

while(my $line = <STDIN>){
    my @lines = split(/\t/,$line);
    my $country = $lines[0];
    $country =~ s/ /_/g;
#    next if(defined($requested{$country}));
    print $wikipedia_url.$country,"\n";
    if(my $result = get($wikipedia_url.$country)){
#	print $result;
	my %geo = ();
	while($result =~ m{http://tools\.wikimedia\.de/~magnus/geo/geohack\.php\?pagename=[^&]+&amp;params=([-0-9NEWS_.']+)type:country\(}g){
	    next if(defined($geo{$1}));
	    print $country,"\t",$lines[2],"\t",$lines[5],"\t",$1,"\n";
	    $geo{$1} = 1;
	}
    }
    $requested{$country} = 1;
}
