root/plagger/trunk/lib/Plagger/Plugin/CustomFeed/GoogleVideo.pm

Revision 109, 3.2 kB (checked in by anonymous, 2 years ago)

support enclosure.

  • Property svn:executable set to *
Line 
1 package Plagger::Plugin::CustomFeed::GoogleVideo;
2 use strict;
3 use warnings;
4 use base qw( Plagger::Plugin );
5
6 use Plagger::Enclosure;
7 use Plagger::UserAgent;
8 use XML::Simple;
9
10 sub register {
11     my($self, $context) = @_;
12     $context->register_hook(
13         $self,
14         'subscription.load' => \&load,
15     );
16 }
17
18 sub load {
19     my($self, $context) = @_;
20
21     my $feed = Plagger::Feed->new;
22     $feed->aggregator(sub { $self->aggregate(@_) });
23     $context->subscription->add($feed);
24 }
25
26 sub aggregate {
27     my($self, $context, $args) = @_;
28
29     my $q = $self->conf->{query};
30     $q =~ s/\s/\+/g;
31
32     my $ua = Plagger::UserAgent->new;
33     my $res = $ua->fetch("http://video.google.com/videofeed?type=search&q=$q&output=rss");
34     $context->log( error => $res->http_response->status_line ) if $res->is_error;
35
36     my $rss = XMLin( $res->content );
37
38     my $feed = Plagger::Feed->new;
39     my $channel = $rss->{channel};
40     $feed->type('GoogleVideo');
41     $feed->title($channel->{title});
42     $feed->link($channel->{link});
43     $feed->image({
44         url        => $channel->{image}->{url},
45         thumb_link => $channel->{image}->{url},
46         width      => $channel->{image}->{width},
47         height     => $channel->{image}->{height},
48     });
49     $feed->description($channel->{description});
50
51     my %type = (
52         flav => 0,
53         wmv  => 1,
54         ipod => 2,
55         psp  => 3,
56     );
57
58     for my $item (@{$rss->{channel}->{item}}){
59         my $entry = Plagger::Entry->new;
60         $entry->title($item->{title});
61         $entry->body($item->{description});
62         $entry->id($item->{guid});
63         $entry->link($item->{link});
64         $entry->date( Plagger::Date->parse('Mail', $item->{pubDate}) );
65         $entry->icon({
66             url        => $item->{'media:group'}->{'media:thumbnail'}->{url},
67             thumb_link => $item->{'media:group'}->{'media:thumbnail'}->{url},
68             width      => $item->{'media:group'}->{'media:thumbnail'}->{width},
69             height     => $item->{'media:group'}->{'media:thumbnail'}->{height},
70         });
71
72         if(ref($item->{'media:group'}->{'media:content'}) eq 'ARRAY'){
73             #$entry->meta->{enclosure}->{url} = $item->{'media:group'}->{'media:content'}->[$type{$self->conf->{type}}]->{url};
74             #$entry->meta->{enclosure}->{type} = $item->{'media:group'}->{'media:content'}->[$type{$self->conf->{type}}]->{type};
75             my $enclosure = Plagger::Enclosure->new;
76             $enclosure->url($item->{'media:group'}->{'media:content'}->[$type{$self->conf->{type}}]->{url});
77             $enclosure->type($item->{'media:group'}->{'media:content'}->[$type{$self->conf->{type}}]->{type});
78             $entry->add_enclosure($enclosure);
79         }
80         $feed->add_entry($entry);
81     }
82     $context->update->add($feed);
83 }
84
85 1;
86 __END__
87
88 =head1 NAME
89
90 Plagger::Plugin::CustomFeed::GoogleVideo - Get Google Video search result
91
92 =head1 SYNOPSIS
93
94   - module: CustomFeed::GoogleVideo
95     config:
96       query: nba allstar
97       type: ipod
98
99 =head1 DESCRIPTION
100
101 This plugin fetches the result of Google Video search.
102
103 =head1 CONFIG
104
105 =over 4
106
107 =item query
108
109 Search query strings.
110
111 =item type
112
113 Choose a type of video file from flav, wmv, ipod or psp.
114
115 =back
116
117 =head1 AUTHOR
118
119 Gosuke Miyashita
120
121 =head1 SEE ALSO
122
123 L<Plagger>
124
125 =cut
126
Note: See TracBrowser for help on using the browser.