| 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
| |||||||||||||||||||||||||||||
| 36 | 0 | my $filename = $self->{file}; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return unless $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $file_content and &bytes::length($file_content)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | 0 | File::Path::make_path($directory); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # also verify directory is writable | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless (-w $directory) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 0 | print $fh $file_content; | |||||||||||||||||||||||||||
| 67 | 0 | close $fh; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | return $download_location; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | 1; | |||||||||||||||||||||||||||