root/branches/feature-geo/plagger/lib/Plagger/Plugin/Filter/FlickrGeotagged.pm

Revision 1860 (checked in by miyagawa, 14 years ago)

updated links to Flickr Forum

Line 
1 package Plagger::Plugin::Filter::FlickrGeotagged;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Plagger::UserAgent;
6 use XML::LibXML;
7
8 sub register {
9     my($self, $context) = @_;
10     $context->register_hook(
11         $self,
12         'update.entry.fixup' => \&handle,
13         'plugin.init' => \&initialize,
14     );
15 }
16
17 sub initialize {
18     my($self, $context) = @_;
19     my $apikey = $self->conf->{api_key}
20         or $context->error("API key needed");
21 }
22
23 sub handle {
24     my($self, $context, $args) = @_;
25
26     return unless $args->{entry}->has_tag('geotagged');
27     my $photo_id = ($args->{entry}->permalink =~ m!^http://www\.flickr\.com/photos/[^/]+/(\d+)/!)[0]
28         or return;
29
30     my $url = URI->new("http://api.flickr.com/services/rest/");
31     $url->query_form(
32         method   => "flickr.photos.getInfo",
33         api_key  => $self->conf->{api_key},
34         photo_id => $photo_id,
35     );
36
37     $context->log(info => "GET $url to get raw tags for $photo_id");
38     my $ua  = Plagger::UserAgent->new;
39     my $res = $ua->fetch($url, $self, { NoNetwork => 60 * 60 * 3 });
40     if ($res->is_error) {
41         $context->log(error => "Error GET $url: " . $res->status);
42     }
43
44     my $doc = XML::LibXML->new->parse_string($res->content);
45     my @tags = $doc->documentElement->getElementsByTagName('tag');
46
47     my %geo;
48     for my $tag (@tags) {
49         my $attr = $tag->getAttribute('raw');
50         if ($attr =~ /^geo:(lat|lon)=([\-\+\d\.]+)$/) {
51             $geo{$1} = $2;
52         }
53     }
54
55     if ($geo{lat} && $geo{lon}) {
56         $args->{entry}->location($geo{lat}, $geo{lon});
57         return 1;
58     }
59
60     return;
61 }
62
63 1;
64 __END__
65
66 =head1 NAME
67
68 Plagger::Plugin::Filter::FlickrGeotagged - Fetch location information from Flickr geotagged feeds
69
70 =head1 SYNOPSIS
71
72   - module: Filter::FlickrGeotagged
73     config:
74       api_key: YOUR-API-KEY
75
76 =head1 DESCRIPTION
77
78 This plugin calls Flickr API to get raw geo tags from Flickr geotagged
79 feed, when the entry has tag I<geotagged> and the entry links to
80 Flickr photos.
81
82 =head1 CONFIG
83
84 =over 4
85
86 =item api_key
87
88 Specifies your API key for Flickr. Required.
89
90 =back
91
92 =head1 AUTHOR
93
94 Tatsuhiko Miyagawa
95
96 =head1 SEE ALSO
97
98 L<Plagger>, L<http://www.flickr.com/forums/ideas/30005/>
99
100 =cut
Note: See TracBrowser for help on using the browser.