Changeset 896
- Timestamp:
- 06/02/06 19:07:12
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plagger/lib/Plagger/Plugin/CustomFeed/FlickrSearch.pm
r430 r896 7 7 use XML::LibXML; 8 8 use DateTime::Format::Epoch; 9 use Plagger::Enclosure; 9 10 10 11 sub register { … … 30 31 my $feed = Plagger::Feed->new; 31 32 $feed->type('flickr.search'); 32 $feed->title("Flickr Search"); 33 $feed->id('flickr:search'); 33 $feed->title("Flickr Search"); # xxx 34 $feed->id('flickr:search'); # xxx 34 35 35 36 my $flickr = Flickr::API->new({key => $self->conf->{api_key}}); 36 my $search = $flickr->execute_method('flickr.photos.search' => $self->conf); 37 my $method = $self->conf->{method} || 'flickr.photos.search'; 38 39 my $params = $self->conf->{params} || {}; 40 $params->{per_page} ||= 20; 41 42 $context->log(info => "calling $method on Flickr API"); 43 my $search = $self->call_method( 44 $flickr, 45 $method, 46 $params, 47 60 * 60, 48 ); 37 49 38 50 my $parser = XML::LibXML->new; 39 51 40 $context->error(" flickr.photos.searchfailed: $search->{error_text}")52 $context->error("$method failed: $search->{error_text}") 41 53 unless $search->{success}; 42 54 my $search_doc = $parser->parse_string($search->{_content}); … … 52 64 sub _create_entry { 53 65 my ($self, $context, $flickr, $parser, $search_photo) = @_; 54 my $sizes = $flickr->execute_method('flickr.photos.getSizes', {55 photo_id => $search_photo->findvalue('@id'),56 });57 next unless $sizes->{success};58 59 my $sizes_doc = $parser->parse_string($sizes->{_content});60 my $size = $self->conf->{size} || 'Square';61 my $image_src =62 $sizes_doc->findvalue(qq[/rsp/sizes/size[\@label='$size']/\@url]);63 66 64 my $info = $flickr->execute_method('flickr.photos.getInfo', { 65 photo_id => $search_photo->findvalue('@id'), 66 }); 67 my $photo_id = $search_photo->findvalue('@id'); 68 my $server_id = $search_photo->findvalue('@server'); 69 my $secret = $search_photo->findvalue('@secret'); 70 71 my $size = $self->conf->{size} || 'm'; 72 my $thumb_src = sprintf "http://static.flickr.com/%s/%s_%s_t.jpg", 73 $server_id, $photo_id, $secret; 74 75 $context->log(info => "calling flickr.photos.getInfo on $photo_id"); 76 my $info = $self->call_method( 77 $flickr, 78 'flickr.photos.getInfo', 79 { photo_id => $photo_id }, 80 60 * 60, 81 ); 67 82 next unless $info->{success}; 68 83 69 84 my $info_doc = $parser->parse_string($info->{_content}); 70 85 my $link = $info_doc->findvalue(q[/rsp/photo/urls/url[@type='photopage']]); 71 my $author = $info_doc->findvalue(q[/rsp/photo/owner/@realname]) ;72 $author = $info_doc->findvalue(q[/rsp/photo/owner/@username]) unless $author;86 my $author = $info_doc->findvalue(q[/rsp/photo/owner/@realname]) 87 || $info_doc->findvalue(q[/rsp/photo/owner/@username]); 73 88 my $title = $info_doc->findvalue(q[/rsp/photo/title]); 74 89 my $date = $info_doc->findvalue(q[/rsp/photo/dates/@posted]); 90 my $format = $info_doc->findvalue(q[/rsp/photo/@originalformat]) || 'jpg'; 91 my $desc = $info_doc->findvalue(q[/rsp/photo/description]); 92 my @tags = map $_->textContent, $info_doc->findnodes('/rsp/photo/tags/tag'); 75 93 76 my $description = $context->templatize($self, 'entry-description.tt', { 77 image_src => $image_src, 78 title => $title, 79 link => $link, 80 }); 94 my $original = sprintf "http://static.flickr.com/%s/%s_%s_o.%s", 95 $server_id, $photo_id, $secret, $format; 81 96 my $epoch = DateTime->from_epoch(epoch => 0, time_zone => '+0000'); 82 97 … … 84 99 $entry->title($title); 85 100 $entry->link($link); 86 $entry->body($description); 101 $entry->author($author); 102 $entry->body($desc); 87 103 $entry->date(Plagger::Date->parse('Epoch::Unix', $date)); 104 $entry->add_tag($_) for @tags; 105 $entry->icon({ url => $thumb_src }); 106 107 my $enclosure = Plagger::Enclosure->new; 108 $enclosure->url($original); 109 $enclosure->auto_set_type; 110 $entry->add_enclosure($enclosure); 88 111 89 112 return $entry; 90 113 } 91 114 115 sub call_method { 116 my($self, $flickr, $method, $param, $cache) = @_; 117 118 my $cache_key = "$method:" . join("|", map "$_=$param->{$_}", sort keys %$param); 119 $self->cache->get_callback( 120 $cache_key, 121 sub { $flickr->execute_method($method, $param) }, 122 $cache, 123 ); 124 } 125 92 126 1; 127 128 __END__ 129 130 =head1 NAME 131 132 Plagger::Plugin::CustomFeed::FlickrSearch - Flickr API as Custom Feed 133 134 =head1 SYNOPSIS 135 136 - module: CustomFeed::FlickrSearch 137 config: 138 api_key: YOUR-FLICKR-APIKEY 139 method: flickr.photos.search 140 params: 141 tags: plagger 142 143 =head1 AUTHOR 144 145 Casey West 146 147 Tatsuhiko Miyagawa 148 149 =head1 SEE ALSO 150 151 L<Plagger>, L<http://www.flickr.com/>, L<Flickr::API> 152 153 =cut
