summary refs log tree commit diff
path: root/clean-country.pl
blob: 460b29f31c763307a3b5cc0371a267c2a144c46e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env perl
use strict;
use warnings;
use lib ('lib');
use 5.010;
use Geo::GeoNames;
use githubexplorer::Schema;
use YAML::Syck;

my $conf = LoadFile(shift);

my $schema = githubexplorer::Schema->connect( @{ $conf->{connect_info} } );

my $profiles = $schema->resultset('Profiles')->search(
    {
        id       => { '>'  => 55781 },
        location => { '!=' => undef },
        location => { '!=' => '' }
    }
);

my $geo = Geo::GeoNames->new();

while ( my $pr = $profiles->next ) {
    next if $pr->location =~ /^http/;
    next if $pr->country;
    next if $pr->location =~ /earth/i;
    say "-> process " . $pr->login . " with " . $pr->location;
    my $result = $geo->search( q => $pr->location, maxRows => 1 );
    my $res = shift @$result;
    if ($res) {
        eval {
            $pr->update(
                { city => $res->{name}, country => $res->{countryName} } );
        };
        next if $@;
        say "** fix with " . $pr->city . " in " . $pr->country;
    }
    if (++$i == 10) {
        sleep(2);
        $i = 0;
    }
}