aboutsummaryrefslogtreecommitdiffstats
path: root/bin/open311-populate-service-list
blob: f9f183f26c364d44f7824e3b28a7d17a7df9afc5 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl

use strict;
use warnings;
use LWP::Simple;
use XML::Simple;
use FixMyStreet::App;
use Open311;

use Data::Dumper;

my $council_list = FixMyStreet::App->model('DB::Open311conf');

while ( my $council = $council_list->next ) {

    my $open311 = Open311->new(
        endpoint => $council->endpoint,
        jurisdiction => $council->jurisdiction,
        api_key => $council->api_key
    );

    my $list = $open311->get_service_list;

    my @found_contacts;

    # print Dumper $list;

    foreach my $service ( @{ $list->{service} } ) {
        print $service->{service_code} . ': ' . $service->{service_name} .  "\n";
        my $contacts = FixMyStreet::App->model( 'DB::Contact')->search(
            {
                area_id => $council->area_id,
                -OR => [
                    email => $service->{service_code},
                    category => $service->{service_name}
                ]
            }
        );

        my $contact = $contacts->first;

        # FIXME - handle change of service name or service code
        if ( $contact ) {

            print $council->area_id . " already has a contact for service code " . $service->{service_code} . "\n";
            push @found_contacts, $service->{service_code};

            if ( $contact->deleted ) {
                $contact->update(
                    {
                        category => $service->{service_name},
                        email => $service->{service_code},
                        confirmed => 1,
                        deleted => 0,
                        editor => $0,
                        whenedited => \'ms_current_timestamp()',
                        note => 'automatically undeleted by script',
                    }
                );
            }
        } else {
            my $contact = FixMyStreet::App->model( 'DB::Contact')->create(
                {
                    email => $service->{service_code},
                    area_id => $council->area_id,
                    category => $service->{service_name},
                    confirmed => 1,
                    deleted => 0,
                    editor => $0,
                    whenedited => \'ms_current_timestamp()',
                    note => 'created automatically by script',
                }
            );

            if ( lc( $service->{metadata} ) eq 'true' ) {
                print "Fetching meta data for $service->{service_code}\n";
                my $meta_data = $open311->get_service_meta_info( $service->{service_code} );

                # turn the data into something a bit more friendly to use
                my %meta = ();
                foreach my $attribute ( @{ $meta_data->{attribute} } ) {
                    $meta{ $attribute->{code} } = $attribute;
                }

                $contact->extra( \%meta );
                $contact->update;
            }
            print "created contact for service code " . $service->{service_code} . " for council @{[$council->area_id]}\n";
        }
    }

    my $found_contacts = FixMyStreet::App->model( 'DB::Contact')->search(
        {
            email => { -not_in => \@found_contacts },
            area_id => $council->area_id,
            deleted => 0,
        }
    );

    $found_contacts->update(
        {
            deleted => 1,
            editor  => $0,
            whenedited => \'ms_current_timestamp()',
            note => 'automatically marked as deleted by script'
        }
    );
}