| blib/lib/OpenInteract2/Action/FlickrFeed.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 6 | 6 | 100.0 |
| branch | n/a | ||
| condition | n/a | ||
| subroutine | 2 | 2 | 100.0 |
| pod | n/a | ||
| total | 8 | 8 | 100.0 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package OpenInteract2::Action::FlickrFeed; | ||||||
| 2 | |||||||
| 3 | 1 | 1 | 705 | use strict; | |||
| 1 | 2 | ||||||
| 1 | 36 | ||||||
| 4 | 1 | 1 | 5 | use base qw( OpenInteract2::Action::RSS ); | |||
| 1 | 1 | ||||||
| 1 | 786 | ||||||
| 5 | |||||||
| 6 | $OpenInteract2::Action::FlickrFeed::VERSION = '0.02'; | ||||||
| 7 | |||||||
| 8 | my $URL_TEMPLATE = 'http://www.flickr.com/services/feeds/photos_public.gne?id=%s&format=%s'; | ||||||
| 9 | my $DEFAULT_FORMAT = 'atom_03'; | ||||||
| 10 | |||||||
| 11 | sub _get_feed_url { | ||||||
| 12 | my ( $self ) = @_; | ||||||
| 13 | my $feed_id = $self->param( 'feed_id' ); | ||||||
| 14 | unless ( $feed_id ) { | ||||||
| 15 | die "To download a Flickr feed you must specify the parameter ", | ||||||
| 16 | "'feed_id' in the configuration for action ", $self->name, "\n"; | ||||||
| 17 | } | ||||||
| 18 | my $format = $self->param( 'feed_format' ) || $DEFAULT_FORMAT; | ||||||
| 19 | return sprintf( $URL_TEMPLATE, $feed_id, $format ); | ||||||
| 20 | } | ||||||
| 21 | |||||||
| 22 | sub _modify_template_params { | ||||||
| 23 | my ( $self, $params ) = @_; | ||||||
| 24 | my $num_photos = $self->param( 'num_photos' ) || 0; | ||||||
| 25 | my $feed = $params->{feed}; | ||||||
| 26 | my @entries = (); | ||||||
| 27 | foreach my $entry ( $feed->entries ) { | ||||||
| 28 | my $content = $entry->content->body; # already escaped | ||||||
| 29 | my $photo_data = $self->_parse_flickr_content( $content ); | ||||||
| 30 | push @entries, $photo_data; | ||||||
| 31 | last if ( $num_photos and scalar( @entries ) == $num_photos ); | ||||||
| 32 | } | ||||||
| 33 | $params->{photos} = \@entries; | ||||||
| 34 | } | ||||||
| 35 | |||||||
| 36 | # We use simple regexes to extract the data from the content Flickr | ||||||
| 37 | # emits; therefore this is very dependent on the format, which is: | ||||||
| 38 | |||||||
| 39 | # USER NAME posted a photo: |
||||||
| 40 | # | ||||||
| 41 | # | ||||||
| 42 | |||||||
| 43 | # Or with some data filled in: | ||||||
| 44 | # | ||||||
| 45 | # Chris Winters posted a photo: |
||||||
| 46 | # | ||||||
| 47 | # | ||||||
| 48 | |||||||
| 49 | sub _parse_flickr_content { | ||||||
| 50 | my ( $self, $content ) = @_; | ||||||
| 51 | $content =~ s|^.*?||; | ||||||
| 52 | my ( $link ) = $content =~ m| | ||||||
| 53 | my ( $title ) = $content =~ m|title="([^"]+)"|; | ||||||
| 54 | my ( $img_src ) = $content =~ m|src="([^"]+)"|; | ||||||
| 55 | my ( $width, $height ) = $content =~ m|width="(\d+)"\s+height="(\d+)"|; | ||||||
| 56 | return { | ||||||
| 57 | link => $link, title => $title, img_src => $img_src, | ||||||
| 58 | width => $width, height => $height | ||||||
| 59 | }; | ||||||
| 60 | } | ||||||
| 61 | |||||||
| 62 | 1; | ||||||
| 63 | |||||||
| 64 | __END__ |