#!/usr/bin/perl -w # import-flickr: # Get new Flickr photos (uploaded from cameras, hopefully!) # # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # # $Id: import-flickr,v 1.9 2008-10-09 17:18:02 matthew Exp $ use strict; require 5.8.0; # Horrible boilerplate to set up appropriate library paths. use FindBin; use lib "$FindBin::Bin/../perllib"; use lib "$FindBin::Bin/../commonlib/perllib"; use File::Slurp; use LWP::Simple; use Utils; use mySociety::AuthToken; use mySociety::Config; use mySociety::DBHandle qw(dbh select_all); use mySociety::EmailUtil; use mySociety::Email; use mySociety::GeoUtil; BEGIN { mySociety::Config::set_file("$FindBin::Bin/../conf/general"); mySociety::DBHandle::configure( Name => mySociety::Config::get('BCI_DB_NAME'), User => mySociety::Config::get('BCI_DB_USER'), Password => mySociety::Config::get('BCI_DB_PASS'), Host => mySociety::Config::get('BCI_DB_HOST', undef), Port => mySociety::Config::get('BCI_DB_PORT', undef) ); } my $key = mySociety::Config::get('FLICKR_API'); my $url = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&tags=fixmystreet&extras=geo,machine_tags&api_key=' . $key . '&user_id='; my $ids = select_all("select nsid from partial_user where service='flickr'"); my $result = ''; foreach (@$ids) { my $api_lookup = get($url . $_->{nsid}); next unless $api_lookup; $result .= $api_lookup; } my %ids; my $st = select_all('select id from flickr_imported'); foreach (@$st) { $ids{$_->{id}} = 1; } # XXX: Hmm... Use format=perl now Cal has added it for me! :) while ($result =~ /selectrow_array("select name, email from partial_user where service='flickr' and nsid=?", {}, $owner); my ($easting, $northing) = (0,0); $name ||= ''; if ($lat && $lon) { # XXX This appears to be going wrong :( ($easting, $northing) = mySociety::GeoUtil::wgs84_to_national_grid($lat, $lon, 'G'); } my $id = dbh()->selectrow_array("select nextval('problem_id_seq')"); Utils::workaround_pg_bytea("insert into problem (id, postcode, easting, northing, title, detail, name, email, phone, photo, state, used_map, anonymous, category, areas) values (?, '', ?, ?, ?, '', ?, ?, '', ?, 'partial', 't', 'f', '', '')", 7, $id, $easting, $northing, $title, $name, $email, $image ); dbh()->do('insert into flickr_imported (id, problem_id) values (?, ?)', {}, $photo_id, $id); # XXX: Needs to only send email once to user per batch of photos, not one per photo? my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/flickr-submit"); my %h = (); my $token = mySociety::AuthToken::store('partial', $id); $h{name} = $name; $h{url} = mySociety::Config::get('BASE_URL') . '/L/' . $token; my $body = mySociety::Email::construct_email({ _template_ => $template, _parameters_ => \%h, To => $name ? [ [ $email, $name ] ] : $email, From => [ mySociety::Config::get('CONTACT_EMAIL'), 'FixMyStreet' ], }); my $result = mySociety::EmailUtil::send_email($body, mySociety::Config::get('CONTACT_EMAIL'), $email); if ($result == mySociety::EmailUtil::EMAIL_SUCCESS) { dbh()->commit(); } else { dbh()->rollback(); } }