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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package GitHub::Collector::Command::country;
use Moose;
use boolean;
extends qw(MooseX::App::Cmd::Command);
has geo_conf => (
is => 'rw',
isa => 'HashRef',
required => 1,
documentation => 'SPORE configuration for Geo API',
);
with
'GitHub::Collector::Role::Logger',
'GitHub::Collector::Role::Context',
'GitHub::Collector::Role::MongoDB',
'Net::HTTP::Spore::Role' =>
{ spore_clients => [ { name => 'geo', config => 'geo_conf' } ] };
sub execute {
my $self = shift;
$self->log("start to tag user using country");
my $profiles = $self->db_profiles->find({country_done => false});
while ( my $profile = $profiles->next ) {
$self->_tag_profile_by_country($profile);
}
$self->log("done tagging users");
}
sub _tag_profile_by_country{
my ($self, $profile) = @_;
if ( !defined $profile->{location} ) {
$self->_update_country($profile->{login}, false);
return;
}
$self->log( "searching for "
. $profile->{login}
. " based in "
. $profile->{location} );
my $res = $self->geo->search(
q => $profile->{location},
username => $self->geo_conf->{api_username},
)->body;
die "no more requests" if $res->{status} && $res->{status}->{value} == 19;
if (my $country = $res->{geonames}->[0]->{countryName}){
$self->_update_country($profile->{login}, $country);
}else{
$self->_update_country($profile->{login}, false)
}
}
sub _update_country {
my ( $self, $login, $country ) = @_;
$self->db_profiles->update( { login => $login },
{ '$set' => { country => $country, country_done => true } } );
}
1;
|