#!/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 = # remove trailing colon as we add this when we display so we don't want 2 map { $_->{description} =~ s/:\s*//; $_ } # there is a display order and we only want to sort once sort { $a->{order} <=> $b->{order} } @{ $meta_data->{attributes}->{attribute} }; $contact->extra( \@meta ); $contact->update; } push @found_contacts, $service->{service_code}; 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' } ); }