aboutsummaryrefslogtreecommitdiffstats
path: root/bin/import-flickr
blob: 992a6ac8e6cba468c07feaf08ca0d03b71e6372a (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
109
110
111
112
113
#!/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 =~ /<photo id="([^"]*)" owner="([^"]*)" secret="([^"]*)" server="([^"]*)" farm="([^"]*)" title="([^"]*)".*?latitude="([^"]*)" longitude="([^"]*)".*?machine_tags="([^"]*)"/g) {
    my ($id, $owner, $secret, $server, $farm, $title, $lat, $lon, $machine) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
    next if $ids{$id};
    if ($machine =~ /geo:/ && !$lat && !$lon) {
        # Have to fetch raw tags, as otherwise can't tell if it's negative, or how many decimal places
        my $url = 'http://api.flickr.com/services/rest/?method=flickr.tags.getListPhoto&api_key=' . $key . '&photo_id=' . $id;
        my $tags = get($url);
        ($lon) = $tags =~ /raw="geo:lon=([^"]*)"/i;
        ($lat) = $tags =~ /raw="geo:lat=([^"]*)"/i;
    }
    my $url = "http://farm$farm.static.flickr.com/$server/".$id.'_'.$secret.'_m.jpg';
    my $image = get($url);
    problem_create($id, $owner, $title, $lat, $lon, $image);
}

sub problem_create {
    my ($photo_id, $owner, $title, $lat, $lon, $image) = @_;
    my ($name, $email) = dbh()->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();
    }
}