blib/lib/XML/FeedPP/Plugin/AddMP3.pm

filter: Statementstmt 27.5% 22 / 80 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 8 / 12 Podpod n/a totaltotal 22.7% 30 / 132 CC 20 SCAR 49.7
CC
20
Cov
19%
add_mp3
272.0
rewrite_value
20.0
is_invalid
6.0
CRAP 143.7
↑ Summary
1 package XML::FeedPP::Plugin::AddMP3;
2
3 =head1 NAME
4
5 XML::FeedPP::Plugin::AddMP3 - FeedPP Plugin for adding MP3 as item.
6
7 =head1 SYNOPSIS
8
9 use XML::FeedPP;
10 my $feed = XML::FeedPP->new( 'index.rss' );
11 $feed->call(AddMP3 => './mp3/test.mp3');
12 $feed->to_file('rss.xml');
13
14 =head1 DESCRIPTION
15
16 This plugin generate new feed item for specified MP3 file.
17
18 $feed->call(AddMP3 => './mp3/test.mp3');
19
20 And set default value for the item's title, author, enclosure url,
21 enclosure length, enclosure type with MP3 TAGs and INFOs.
22
23 If use_itune option is specified, xmlns:itunes is added to feed.
24 And additional default value for itunes:author, itunes:subtitle,
25 itunes:duration, itunes:keywords are set.
26
27 Returns added item, or undef.
28
29 B<NOTE:> If those values includes non-UTF-8 characters, it tries to
30 convert with Encode, or Jcode module. When both of them are avaliable,
31 It calls Carp::carp, and continues process.
32
33 =head1 OPTIONS
34
35 This plugin allows some optoinal arguments following:
36
37 =over 4
38
39 =item base_dir
40
41 By default, url attribute of enclosure tag is set to file argument.
42 If base_dir is specified, url attribute is converted to relative path from base_dir.
43
44 =item base_url
45
46 If base_url is specified, url attribute is converted as relative path from base_url.
47
48 =item link_url
49
50 By default, link value is set to the feed's link value.
51 If link_url is specified, link value is set to link_url.
52
53 =item use_itunes
54
55 Use itunes name space, and add tags in the name space.
56 See also http://www.apple.com/itunes/store/podcaststechspecs.html
57
58 =back
59
60 For example,
61
62 my %opt = (
63 base_dir => './mp3'
64 base_url => 'http://example.com/podcast/files',
65 link_url => 'http://example.com/podcast',
66 use_itunes => 1,
67 );
68 $feed->call(AddMP3 => './mp3/test.mp3');
69
70 At first, URL is set to './mp3/test.mp3'.
71 Then, base_dir is specified in this case, URL is chenged to 'test.mp3'.
72 Alos base_url is specified in this case, so URL is chenged to 'http://example.com/podcast/files/test.mp3'.
73
74 =head1 MODULE DEPENDENCIES
75
76 L<XML::FeedPP>, L<Path::Class>, L<MP3::Info>
77
78 =head1 MODULE RECOMMENDED
79
80 L<Encode>, or L<Jcode> (for Japanese users)
81
82 =head1 SEE ALSO
83
84 L<XML::FeedPP>
85
86 http://www.apple.com/itunes/store/podcaststechspecs.html (Podcast specification)
87
88 =head1 AUTHOR
89
90 Makio Tsukamoto <tsukamoto@gmail.com>
91
92 =head1 COPYRIGHT AND LICENSE
93
94 Copyright (c) 2007 Makio Tsukamoto. All rights reserved.
95 This program is free software; you can redistribute it
96 and/or modify it under the same terms as Perl itself.
97
98 =cut
99
100 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 4 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
102 @ISA = qw( XML::FeedPP::Plugin );
103
104 4 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
105 $VERSION = "0.02";
106
107 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 4 ▶use MP3::Info;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 4 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
110
111 my $re_utf8 = qr/(?:[\x00-\x7f]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})/;
112 my $use_encode = undef;
113 my $use_jcode = undef;
114
115 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
116 0 my $class = shift;
117 0 my $feed = shift;
118 0 &add_mp3( $feed, @_ );
119 }
120
121 0 ▶sub add_mp3 {
Subroutine add_mp3: not called CC=16 SCAR=56.1
122 0 my $feed = shift;
123 0 my $file = shift;
124 0 my %opt = @_;
125 # check link
126 0 ▶ my $link = ($opt{'link_url'}) ? $opt{'link_url'} : $feed->link;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'link_url'} ? : 0 0 0
127 0 ▶ return unless (defined($link) and length($link));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $link and length $link 0 0 0
Condition0%0/3
defined $link and length $link
!ll&&!rl&&r
000
MC/DC0%0/2
defined $link and length $link
defined $link length $link
Truth table0%0/3
A and B
A= defined $linkB= length $link
ABresult
0X0
100
111
128 # check file and get its information
129 0 my $path = Path::Class::file($file);
130 0 ▶ Carp::croak "File not exists - $file" if (not -f $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
131 0 ▶ my $stat = File::stat::stat("$path") or Carp::croak "Failed to get file stat - $file";
Branch: 1 branch
BranchTrueFalseTotal
my $stat = &File::stat::stat("$path") or Carp::croak("Failed to get file stat - $file") 0 0 0
132 0 ▶ my $tags = MP3::Info::get_mp3tag("$path") or Carp::croak "Failed to get mp3 tags - $file";
Branch: 1 branch
BranchTrueFalseTotal
my $tags = MP3::Info::get_mp3tag("$path") or Carp::croak("Failed to get mp3 tags - $file") 0 0 0
133 0 ▶ my $info = MP3::Info::get_mp3info("$path") or Carp::croak "Failed to get mp3 info - $file";
Branch: 1 branch
BranchTrueFalseTotal
my $info = MP3::Info::get_mp3info("$path") or Carp::croak("Failed to get mp3 info - $file") 0 0 0
134 # define url
135 0 my $url = Path::Class::file($file);
136 0 ▶ $url = $url->relative($opt{'base_dir'}) if ($opt{'base_dir'});
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"base_dir"} 0 0 0
137 0 $url = $url->as_foreign('Unix')->stringify;
138 0 ▶ if ($opt{'base_url'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"base_url"}) 0 0 0
139 0 my $base_url = $opt{'base_url'};
140 0 $base_url =~ s/\/$//;
141 0 $url =~ s/^\///;
142 0 $url = "$base_url/$url";
143 }
144 # add item
145 0 my $item = $feed->add_item($link);
146 0 $item->guid($url);
147 0 $item->pubDate($stat->mtime);
148 my $podcast = {
149 'title' => $tags->{'TITLE'},
150 0 'author' => $tags->{'ARTIST'},
151 'description' => '', # CDATA is allowed
152 'enclosure@url' => $url,
153 'enclosure@length' => $stat->size,
154 'enclosure@type' => 'audio/mpeg',
155 };
156 0 foreach my $key (%{$podcast}) {
157 0 my $value = &rewrite_value($podcast->{$key});
158 0 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
159 0 ▶ if (my $error = &is_invalid($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &is_invalid($value)) 0 0 0
160 0 Carp::carp "$error - $file->$key, '$value'";
161 }
162 0 $item->set($key => $value);
163 }
164 }
165 # for itunes
166 0 ▶ if ($opt{'use_itunes'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"use_itunes"}) 0 0 0
167 0 $feed->xmlns('xmlns:itunes' => 'http://www.itunes.com/DTDs/Podcast-1.0.dtd');
168 my $itunes = {
169 'itunes:author' => $tags->{'ARTIST'},
170 'itunes:subtitle' => [$tags->{'ALBUM'}, $tags->{'TRACKNUM'}],
171 'itunes:summary' => '', # CDATA is disallowed
172 'itunes:duration' => $info->{'TIME'},
173 0 'itunes:keywords' => [$tags->{'ARTIST'}, $tags->{'YEAR'}],
174 };
175 0 foreach my $key (%{$itunes}) {
176 0 my $value = &rewrite_value($itunes->{$key});
177 0 ▶ $item->set($key => $value) if (defined($value));
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
178 }
179 }
180 0 return $item;
181 }
182
183 0 ▶sub is_invalid {
Subroutine is_invalid: not called CC=2 SCAR=17.9
184 0 my $value = shift;
185 0 ▶ return "Not utf8 character, ignored" unless ($value =~ /^(?:$re_utf8)*$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(?:$re_utf8)*$/ 0 0 0
186 0 return;
187 }
188
189 0 ▶sub rewrite_value {
Subroutine rewrite_value: not called CC=4 SCAR=30.0
190 0 my $value = shift;
191 0 ▶ if (UNIVERSAL::isa($value, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($value, 'ARRAY')) { } 0 0 0
192 0 my @values = map { s/^\s+//s; s/\s+$//s; $_ } @{$value}; #}
193 0 @values = map { &encode_value($_) } grep { length($_) } @values;
194 0 $value = join(', ', @values);
195 } else {
196 0 $value = &encode_value($value);
197 }
198 0 ▶ return (defined($value) and length($value)) ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && length $value ? : 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
199 }
200
201 sub encode_value {
202 my $value = shift;
203 return $value if (defined($use_encode) and defined($use_jcode) and not ($use_encode or $use_jcode)); # Can't encode (already tried).
204 return $value if (not defined($value)); # $value is null.
205 return $value if ($value =~ /^(?:$re_utf8)*$/); # $value is utf8.
206 # try Encode
207 if (not defined($use_encode)) {
208 4 ▶ eval { use Encode; };
Subroutine BEGIN: called CC=1 SCAR=0.0
209 $use_encode = $@ ? 0 : 1;
210 }
211 return encode_utf8($value) if ($use_encode);
212 # try Jcode
213 if (not defined($use_jcode)) {
214 0 ▶ eval { use Jcode; };
Subroutine BEGIN: called CC=1 SCAR=2.6
215 $use_jcode = $@ ? 0 : 1;
216 }
217 return Jcode->new($value)->utf8 if ($use_jcode);
218 # can't encode
219 return $value;
220 }
221
222 1;
↑ Summary