Parser/Section/Pronunciation/Audio.pm

filter: Statementstmt 28.9% 11 / 38 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 42.8% 3 / 7 Podpod 0.0% 0 / 5 totaltotal 20.2% 14 / 69 CC 9 SCAR 40.0
CC
9
Cov
20%
download_file
90.0
get_file
2.0
get_text
2.0
CRAP 54.6
« Parser/Document.pm ↑ Summary Parser/Section/WikisaurusSection.pm »
1 package Wiktionary::Parser::Section::Pronunciation::Audio;
2  
3 3 ▶use MediaWiki::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # keys:
7 # file: name of the audio file
8 # text: text label for this file
9 # context: e.g. en(US), en(UK)
10 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11 5 my $class = shift;
12 5 my %args = @_;
13 5 my $self = bless \%args, $class;
14 5 return $self;
15 }
16  
17 0 ▶sub get_file {
Subroutine get_file: not called CC=1 SCAR=6.9
Pod undocumented
18 0 my $self = shift;
19 0 return $self->{file};
20 }
21  
22 0 ▶sub get_text {
Subroutine get_text: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my $self = shift;
24 0 return $self->{text};
25 }
26  
27 0 ▶sub get_context {
Subroutine get_context: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my $self = shift;
29 0 return $self->{context};
30 }
31  
32 0 ▶sub download_file {
Subroutine download_file: not called CC=9 SCAR=45.0
Pod undocumented
33 0 my $self = shift;
34 0 my %args = @_;
35 0 ▶ my $directory = $args{directory} or die 'you need to specify a directory to download to';
Branch: 1 branch
BranchTrueFalseTotal
my $directory = $args{"directory"} or die "you need to specify a directory to download to" 0 0 0
36 0 my $filename = $self->{file};
37  
38 0 ▶ return unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
39  
40 0 my $api = MediaWiki::API->new({
41 api_url => 'http://en.wiktionary.org/w/api.php',
42 });
43  
44 0 my $file_content = $api->download({title => "File:$filename"});
45  
46 0 ▶ if ($api->{error}->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api->{"error"}{"code"}) 0 0 0
47 0 print STDERR $api->{error}->{code} . ': ' . $api->{error}->{details};
48 }
49  
50 0 ▶ unless (defined $file_content && bytes::length($file_content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file_content and &bytes::length($file_content)) 0 0 0
Condition0%0/3
defined $file_content and &bytes::length($file_content)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file_content and &bytes::length($file_content)
defined $file_content &bytes::length($file_content)
Truth table0%0/3
A and B
A= defined $file_contentB= &bytes::length($file_content)
ABresult
0X0
100
111
51 0 print STDERR "'$filename' doesn't exist or failed to download";
52 0 return;
53 }
54  
55 # create local directory if it doesn't exist
56 0 ▶ unless (-e $directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $directory) 0 0 0
57 0 File::Path::make_path($directory);
58 }
59  
60 # also verify directory is writable
61 0 ▶ unless (-w $directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $directory) 0 0 0
62 0 die "unable to write to '$directory'";
63 }
64 0 my $download_location = "$directory/$filename";
65 0 ▶ open (my $fh, ">",$download_location) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $download_location or die $! 0 0 0
66 0 print $fh $file_content;
67 0 close $fh;
68  
69 0 return $download_location;
70 }
71  
72 3 1;
« Parser/Document.pm ↑ Summary Parser/Section/WikisaurusSection.pm »