aboutsummaryrefslogtreecommitdiffstats
path: root/bin/open311-populate-service-list
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2011-10-10 17:22:55 +0100
committerStruan Donald <struan@exo.org.uk>2011-10-10 17:22:55 +0100
commit100779cc2b4a344d13edd1f47756db926567d69a (patch)
tree2d452a91726423074df1362d71f50c1c6ce1409a /bin/open311-populate-service-list
parent6e7276b843fde5e0490c3859f4beb92538004c0e (diff)
rewrite populate service list to enable testing and add some tests
Diffstat (limited to 'bin/open311-populate-service-list')
-rwxr-xr-xbin/open311-populate-service-list121
1 files changed, 3 insertions, 118 deletions
diff --git a/bin/open311-populate-service-list b/bin/open311-populate-service-list
index cb22d6dad..36e04f5a6 100755
--- a/bin/open311-populate-service-list
+++ b/bin/open311-populate-service-list
@@ -2,126 +2,11 @@
use strict;
use warnings;
-use LWP::Simple;
-use XML::Simple;
use FixMyStreet::App;
-use Open311;
+use Open311::PopulateServiceList;
-use Data::Dumper;
my $council_list = FixMyStreet::App->model('DB::Open311conf');
+my $p = Open311::PopulateServiceList->new( council_list => $council_list );
-while ( my $council = $council_list->next ) {
-
- my $open311 = Open311->new(
- endpoint => $council->endpoint,
- jurisdiction => $council->jurisdiction,
- api_key => $council->api_key
- );
-
- # west berks end point not standard
- if ( $council->area_id == 2619 ) {
- $open311->endpoints(
- {
- services => 'Services',
- requests => 'Requests'
- }
- );
- }
-
- 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;
-
- # remove trailing whitespace as it upsets db queries
- # to look up contact details when creating problem
- my $service_name = $service->{service_name};
- $service_name =~ s/\s+$//;
-
- # 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_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_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'
- }
- );
-}
+$p->process_councils;