aboutsummaryrefslogtreecommitdiffstats
path: root/bin/populate_bing_cache
blob: 17c8911d0bdd0780ee52e38c2ce517a9ceb94f2c (plain)
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
#!/usr/bin/perl

use strict;
use warnings;
require 5.8.0;

use Data::Dumper;

use FixMyStreet::App;
use FixMyStreet::Geocode::Bing;

my $bing_culture = 'en-GB';

my $reports = FixMyStreet::App->model('DB::Problem')->search(
    {
        geocode   => undef,
        confirmed => { '!=', undef },
        latitude  => { '!=', 0 },
        longitude => { '!=', 0 },
    },
    {
        select   => [qw/id geocode confirmed latitude longitude/],
        order_by => { -desc => 'confirmed' }
    }
);

my $num_reports = $reports->count();
print "Found $num_reports lacking geocode information\n";

my $time_to_do = ( $num_reports * 10 ) / 60 / 60;
if ( $time_to_do > 24 ) {
    my $days  = $time_to_do / 24;
    my $hours = $time_to_do % 24;
    printf( "Should take %d days and %d hours to finish\n", $days, $hours );
}
elsif ( $time_to_do < 1 ) {
    printf( "Should take %d minutes to finish\n", $time_to_do * 60 );
}
else {
    my $mins = ( $num_reports * 10 ) / 60 % 60;
    printf( "Should take %d hours and %d minutes to finish\n",
        $time_to_do, $mins );
}

while ( my $report = $reports->next ) {
    $num_reports--;
    next unless $report->latitude && $report->longitude;
    next if $report->geocode;

    my $j = FixMyStreet::Geocode::Bing::reverse( $report->latitude,
        $report->longitude, $bing_culture );

    $report->geocode($j);
    $report->update;

    print "$num_reports left to populate\n" unless $num_reports % 100;
    sleep 10;
}

print "done\n";