Get Geo Location

Here’s, let’s call it what it is, a hack to get geographical location (longitude & latitude).

Code:

#!/bin/env perl
use warnings;
use strict;
use LWP::Simple qw ($ua get);
$ua->timeout(15);

my $geo_url  = "https://geoiptool.com";
my $junk     = get($geo_url) || die "Problems connecting to URL!";
my $lon_line = 0;
my $lat_line = 0;
my $lon      = 0;
my $lat      = 0;
if ($junk) {
    my @lines = split( /\n/, $junk );
    my $i = 1;
    foreach my $line (@lines) {
        chomp($line);
        if ( $line =~ /Longitude/ ) {
            if ( $lon_line == 0 ) {
                $lon_line = ($i);
            }
        } elsif ( $line =~ /Latitude/ ) {
            if ( $lat_line == 0 ) {
                $lat_line = ($i);
            }
        }
        $i++;
    }
    $lon = $lines[$lon_line];
    $lon =~ s/\s+//;
    $lon =~ s/\<span\>//;
    $lon =~ s/\<\/span\>//;
    $lat = $lines[$lat_line];
    $lat =~ s/\s+//;
    $lat =~ s/\<span\>//;
    $lat =~ s/\<\/span\>//;

    if ( $lon && $lat ) {
        print "longitude: " . $lon . ", latitude: " . $lat . "\n";
    }
}

Output:

./geoloc.pl 
longitude: -80.135, latitude: 26.937

And that’s all I have to say about that.