aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2011-08-09 18:14:54 +0100
committerStruan Donald <struan@exo.org.uk>2011-08-09 18:14:54 +0100
commita7106bd754ba67ec28bcf5aaf0a56968efa899a2 (patch)
tree95b9e4f8f5685139437440bc6590d5e94e79d4ab
parent77a6b78dd4732d6943963e4ed9b0775ffe615db6 (diff)
initial update from open311 script
-rw-r--r--bin/open311-update-reports124
-rw-r--r--perllib/Open311.pm16
2 files changed, 137 insertions, 3 deletions
diff --git a/bin/open311-update-reports b/bin/open311-update-reports
new file mode 100644
index 000000000..00242a094
--- /dev/null
+++ b/bin/open311-update-reports
@@ -0,0 +1,124 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Open311;
+use FixMyStreet::App;
+
+# FIXME - make this configurable and/or better
+my $system_user = FixMyStreet::App->model('DB::User')->find_or_create(
+ {
+ email => 'fixmystreet@mysociety.org',
+ name => 'System User',
+ }
+);
+
+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 $area_id = $council->area_id;
+
+ my $council_details = mySociety::MaPit::call( 'area', $area_id );
+
+ my $reports = FixMyStreet::App->model('DB::Problem')->search(
+ {
+ council => { like => "\%$area_id\%" },
+ external_id => { '!=', undef },
+ external_id => { '!=', '' },
+
+ # FIXME - only care about visible states
+ }
+ );
+
+ my @report_ids = ();
+ while ( my $report = $reports->next ) {
+ push @report_ids, $report->external_id;
+ }
+
+ next unless @report_ids;
+
+ my $service_requests = $open311->get_service_requests( \@report_ids );
+
+ my $requests;
+
+ # XML::Simple is a bit inconsistent in how it structures
+ # things depending on the number of children an element has :(
+ if ( ref $service_requests->{request} eq 'ARRAY' ) {
+ $requests = $service_requests->{request};
+ }
+ else {
+ $requests = [ $service_requests->{request} ];
+ }
+
+ for my $request (@$requests) {
+ my $request_id = $request->{service_request_id};
+
+ my $problem =
+ FixMyStreet::App->model('DB::Problem')
+ ->search( { external_id => $request_id, } );
+
+ if (my $p = $problem->first) {
+ my ( $updated, $status_notes );
+
+ if ( ! ref $request->{updated_datetime} ) {
+ $updated = $request->{updated_datetime};
+ }
+
+ if ( ! ref $request->{status_notes} ) {
+ $status_notes = $request->{status_notes};
+ }
+
+ my $update = FixMyStreet::App->model('DB::Comment')->new(
+ {
+ problem_id => $p->id,
+ state => 'confirmed',
+ created => $updated || \'ms_current_timestamp()',
+ confirmed => \'ms_current_timestamp()',
+ text => $status_notes,
+ mark_open => 0,
+ mark_fixed => 0,
+ user => $system_user,
+ anonymous => 0,
+ name => $council_details->{name},
+ }
+ );
+ if ( $request->{status} eq 'closed' ) {
+ if ( $p->state ne 'fixed' ) {
+ $p->state('fixed');
+ $update->mark_fixed(1);
+
+ if ( !$status_notes ) {
+
+ # FIXME - better text here
+ $update->text( 'Closed by council' );
+ }
+
+ $p->update;
+ $update->insert;
+ }
+ }
+ else {
+ if ( $p->state eq 'fixed' ) {
+ $p->state('confirmed');
+ $update->mark_open(1);
+
+ if ( !$status_notes ) {
+
+ # FIXME - better text here
+ $update->text( 'Re-opened by council' );
+ }
+
+ $p->update;
+ $update->insert;
+ }
+ }
+ }
+ }
+}
diff --git a/perllib/Open311.pm b/perllib/Open311.pm
index 0c8c3fde6..9a9ac5eb4 100644
--- a/perllib/Open311.pm
+++ b/perllib/Open311.pm
@@ -87,8 +87,16 @@ EOT
sub get_service_requests {
my $self = shift;
+ my $report_ids = shift;
- my $service_request_xml = $self->_get( 'requests.xml' );
+ my $params = {};
+
+ if ( $report_ids ) {
+ $params->{service_request_id} = join ',', @$report_ids;
+ }
+
+ my $service_request_xml = $self->_get( 'requests.xml', $params || undef );
+ return $self->_get_xml_object( $service_request_xml );
}
sub get_service_request_id_from_token {
@@ -109,11 +117,13 @@ sub get_service_request_id_from_token {
sub _get {
my $self = shift;
my $path = shift;
- my $params = shift;
+ my $params = shift || {};
my $uri = URI->new( $self->endpoint );
+
+ $params->{ jurisdiction_id } = $self->jurisdiction;
$uri->path( $uri->path . $path );
- $uri->query_form( jurisdiction_id => $self->jurisdiction );
+ $uri->query_form( $params );
my $content = get( $uri->as_string );