root/trunk/plagger/lib/Plagger/Plugin/Filter/DegradeYouTube.pm

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

add Filter::DegradeYoutube? via http://d.hatena.ne.jp/tokuhirom/20061008/1160271366

Line 
1 package Plagger::Plugin::Filter::DegradeYouTube;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use WebService::YouTube;
6
7 my $regex = <<'...';
8 <object width="\d+" height="\d+"><param name="movie" value="(http://www.youtube.com/[^"]+)"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/[^"]+"  type="application/x-shockwave-flash" wmode="transparent"  width="\d+" height="\d+"></embed></object>
9 ...
10 chomp $regex;
11
12 sub register {
13     my($self, $context) = @_;
14
15     $context->register_hook(
16         $self,
17         'update.entry.fixup' => \&update,
18     );
19 }
20
21 sub update {
22     my($self, $context, $args) = @_;
23
24     my $body  = $args->{entry}->body;
25     $body =~ s{$regex}{
26         my $url = $1;
27         $context->log(info => "Found YouTube video $url");
28         my $body;
29         if (my $dev_id = $self->conf->{dev_id}) {
30             my $thumb_url = $self->_thumbnail_url($dev_id, $self->_video_id($url));
31             qq{<a href="$url"><img src="$thumb_url" /></a>}
32         } else {
33             $context->log(warn => "No dev_id found. Just use the text replacement.");
34             qq{<a href="$url">YouTube Movie</a>}
35         }
36     }ge;
37     $args->{entry}->body($body);
38 }
39
40 sub _thumbnail_url {
41     my ($self, $dev_id, $video_id) = @_;
42
43     my $api = WebService::YouTube->new({dev_id => $dev_id});
44     my $video = $api->videos->get_details($video_id);
45     return $video->thumbnail_url;
46 }
47
48 sub _video_id {
49     my ($self, $url) = @_;
50     ($url =~ m[/v/([^/]+)$])[0];
51 }
52
53 1;
54 __END__
55
56 =for stopwords IMG
57
58 =head1 NAME
59
60 Plagger::Plugin::Filter::DegradeYouTube - Degrade YouTube object tags
61
62 =head1 SYNOPSIS
63
64   - module: Filter::DegradeYouTube
65     config:
66       dev_id: YOUR-YOUTUBE-DEVID
67
68 =head1 DESCRIPTION
69
70 This plugin, when YouTube object tags are found in the entry body,
71 replaces the object tags into the degraded HTML, e.g. A link with IMG
72 to the thumbnail.
73
74 =head1 CONFIG
75
76 =over 4
77
78 =item dev_id
79
80 Your YouTube developer ID. If set, it tries to fetch the thumbnail
81 image using YouTube API. Optional.
82
83 =head1 AUTHOR
84
85 Tokuhiro Matsuno
86
87 =head1 SEE ALSO
88
89 L<Plagger>
90
91 =cut
Note: See TracBrowser for help on using the browser.