Changeset 327

Show
Ignore:
Timestamp:
03/06/06 07:38:22
Author:
cwest
Message:

Functional CustomFeed?::FlickrSearch?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger/Plugin/CustomFeed/FlickrSearch.pm

    r324 r327  
    66use Flickr::API; 
    77use XML::LibXML; 
     8use DateTime::Format::Epoch; 
    89 
    910sub register { 
     
    2122    my $feed = Plagger::Feed->new; 
    2223    $feed->type('flickr.search'); 
     24    $feed->id('flickr:search'); 
    2325    $context->subscription->add($feed); 
    2426} 
     
    3032    $feed->type('flickr.search'); 
    3133    $feed->title("Flickr Search"); 
     34    $feed->id('flickr:search'); 
    3235 
    3336    my $flickr = Flickr::API->new({key => $self->conf->{api_key}}); 
    3437    my $search = $flickr->execute_method('flickr.photos.search' => $self->conf); 
    3538 
     39    my $parser = XML::LibXML->new; 
     40 
    3641    $context->error("flickr.photos.search failed: $search->{error_text}") 
    3742      unless $search->{success}; 
    38  
    39     my $parser = XML::LibXML->new; 
    4043    my $search_doc = $parser->parse_string($search->{_content}); 
    4144 
    4245    foreach my $search_photo ( $search_doc->findnodes('/rsp/photos/photo') ) { 
    43         my $sizes = $flickr->execute_method('flickr.photos.getSizes', { 
    44             photo_id => $search_photo->findvalue('@id'), 
    45         }); 
    46         next unless $sizes->{success}; 
    47          
    48         my $sizes_doc = $parser->parse_string($sizes->{_content}); 
    49         my $image_src = 
    50           $sizes_doc->findvalue(q[/rsp/sizes/size[@label='Square']/@url]); 
    51  
    52         my $entry = Plagger::Entry->new; 
    53          
    54         # get url of photo page 
    55         # get related details such as title, date, author(?) 
     46        my $entry = $self->_create_entry($context, $flickr, $parser, $search_photo); 
     47        $feed->add_entry($entry); 
    5648    } 
    5749 
     
    5951} 
    6052 
     53sub _create_entry { 
     54    my ($self, $context, $flickr, $parser, $search_photo) = @_; 
     55    my $sizes = $flickr->execute_method('flickr.photos.getSizes', { 
     56        photo_id => $search_photo->findvalue('@id'), 
     57    }); 
     58    next unless $sizes->{success}; 
     59     
     60    my $sizes_doc = $parser->parse_string($sizes->{_content}); 
     61    my $size      = $self->conf->{size} || 'Square'; 
     62    my $image_src = 
     63      $sizes_doc->findvalue(qq[/rsp/sizes/size[\@label='$size']/\@url]); 
     64 
     65    my $info = $flickr->execute_method('flickr.photos.getInfo', { 
     66        photo_id => $search_photo->findvalue('@id'), 
     67    }); 
     68    next unless $info->{success}; 
     69     
     70    my $info_doc = $parser->parse_string($info->{_content}); 
     71    my $link     = $info_doc->findvalue(q[/rsp/photo/urls/url[@type='photopage']]); 
     72    my $author   = $info_doc->findvalue(q[/rsp/photo/owner/@realname]); 
     73       $author   = $info_doc->findvalue(q[/rsp/photo/owner/@username]) unless $author; 
     74    my $title    = $info_doc->findvalue(q[/rsp/photo/title]); 
     75    my $date     = $info_doc->findvalue(q[/rsp/photo/dates/@posted]); 
     76 
     77    my $description = $context->templatize($self, 'entry-description.tt', { 
     78        image_src => $image_src, 
     79        title     => $title, 
     80        link      => $link, 
     81    }); 
     82    my $epoch = DateTime->from_epoch(epoch => 0, time_zone => '+0000'); 
     83 
     84    my $entry = Plagger::Entry->new; 
     85    $entry->title($title); 
     86    $entry->link($link); 
     87    $entry->body($description); 
     88    $entry->date(Plagger::Date->parse('Epoch::Unix', $date)); 
     89 
     90    return $entry; 
     91} 
     92 
    61931;