| 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 name="wmode" value="transparent".*?>(.*?)</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->cache->get_callback( |
|---|
| 31 |
$url, |
|---|
| 32 |
sub { $self->_thumbnail_url($dev_id, $self->_video_id($url)) }, |
|---|
| 33 |
60 * 60 * 24, |
|---|
| 34 |
); |
|---|
| 35 |
$context->log(info => "Thumbnail for $url => $thumb_url"); |
|---|
| 36 |
qq{<a href="$url"><img src="$thumb_url" /></a>} |
|---|
| 37 |
} else { |
|---|
| 38 |
$context->log(warn => "No dev_id found. Just use the text replacement."); |
|---|
| 39 |
qq{<a href="$url">YouTube Movie</a>} |
|---|
| 40 |
} |
|---|
| 41 |
}ge; |
|---|
| 42 |
$args->{entry}->body($body); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
sub _thumbnail_url { |
|---|
| 46 |
my ($self, $dev_id, $video_id) = @_; |
|---|
| 47 |
|
|---|
| 48 |
my $api = WebService::YouTube->new({dev_id => $dev_id}); |
|---|
| 49 |
my $video = $api->videos->get_details($video_id); |
|---|
| 50 |
return $video->thumbnail_url; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
sub _video_id { |
|---|
| 54 |
my ($self, $url) = @_; |
|---|
| 55 |
($url =~ m[/v/([^/]+)$])[0]; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
1; |
|---|
| 59 |
__END__ |
|---|
| 60 |
|
|---|
| 61 |
=for stopwords IMG |
|---|
| 62 |
|
|---|
| 63 |
=head1 NAME |
|---|
| 64 |
|
|---|
| 65 |
Plagger::Plugin::Filter::DegradeYouTube - Degrade YouTube object tags |
|---|
| 66 |
|
|---|
| 67 |
=head1 SYNOPSIS |
|---|
| 68 |
|
|---|
| 69 |
- module: Filter::DegradeYouTube |
|---|
| 70 |
config: |
|---|
| 71 |
dev_id: YOUR-YOUTUBE-DEVID |
|---|
| 72 |
|
|---|
| 73 |
=head1 DESCRIPTION |
|---|
| 74 |
|
|---|
| 75 |
This plugin, when YouTube object tags are found in the entry body, |
|---|
| 76 |
replaces the object tags into the degraded HTML, e.g. A link with IMG |
|---|
| 77 |
to the thumbnail. |
|---|
| 78 |
|
|---|
| 79 |
=head1 CONFIG |
|---|
| 80 |
|
|---|
| 81 |
=over 4 |
|---|
| 82 |
|
|---|
| 83 |
=item dev_id |
|---|
| 84 |
|
|---|
| 85 |
Your YouTube developer ID. If set, it tries to fetch the thumbnail |
|---|
| 86 |
image using YouTube API. Optional. |
|---|
| 87 |
|
|---|
| 88 |
=back |
|---|
| 89 |
|
|---|
| 90 |
=head1 AUTHOR |
|---|
| 91 |
|
|---|
| 92 |
Tokuhiro Matsuno |
|---|
| 93 |
|
|---|
| 94 |
=head1 SEE ALSO |
|---|
| 95 |
|
|---|
| 96 |
L<Plagger> |
|---|
| 97 |
|
|---|
| 98 |
=cut |
|---|
| 99 |
|
|---|