| 1 | 5 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package App::MP4Meta::Base; | ||||||||||||||||||||||||||||
| 6 | { | ||||||||||||||||||||||||||||
| 7 | 5 | $App::MP4Meta::Base::VERSION = '1.153340'; | |||||||||||||||||||||||||||
| 8 | } | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Base class. Contains common functionality. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Module::Load (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use AtomicParsley::Command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 18 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 19 | 0 | my $self = {}; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # file suffixes we support | ||||||||||||||||||||||||||||
| 22 | 0 | my @suffixes = qw/mp4 m4a m4p m4v m4b/; | |||||||||||||||||||||||||||
| 23 | 0 | $self->{suffixes} = \@suffixes; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # the path to AtomicParsley | ||||||||||||||||||||||||||||
| 26 | 0 | $self->{'ap'} = AtomicParsley::Command->new( { ap => $args->{'ap'} } ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # if true, replace file | ||||||||||||||||||||||||||||
| 29 | 0 | $self->{'noreplace'} = $args->{'noreplace'}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # if true, add to itunes | ||||||||||||||||||||||||||||
| 32 | 0 | $self->{'itunes'} = $args->{'itunes'}; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # if true, print verbosely | ||||||||||||||||||||||||||||
| 35 | 0 | $self->{'verbose'} = $args->{'verbose'}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # internet sources | ||||||||||||||||||||||||||||
| 38 | 0 | $self->{'sources'} = $args->{'sources'}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # common attributes for a media file | ||||||||||||||||||||||||||||
| 41 | 0 | $self->{'genre'} = $args->{'genre'}; | |||||||||||||||||||||||||||
| 42 | 0 | $self->{'title'} = $args->{'title'}; | |||||||||||||||||||||||||||
| 43 | 0 | $self->{'coverfile'} = $args->{'coverfile'}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # create sources now so they are in scope for as long as we are | ||||||||||||||||||||||||||||
| 48 | 0 | $self->{'sources_objects'} = []; | |||||||||||||||||||||||||||
| 49 | 0 | for my $source ( @{ $self->{'sources'} } ) { | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 0 | push( @{ $self->{'sources_objects'} }, | |||||||||||||||||||||||||||
| 52 | $self->_new_source($source) ); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 55 | 0 | say STDERR "could not load source: $_"; | |||||||||||||||||||||||||||
| 56 | 0 | }; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Calls AtomicParsley and writes the tags to the file | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _write_tags { | ||||||||||||||||||||||||||
|
Subroutine
_write_tags: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 64 | 0 | my ( $self, $path, $tags ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $tempfile = $self->{ap}->write_tags( $path, $tags, !$self->{noreplace} ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( !$self->{ap}->{success} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return $self->{ap}->{'stdout_buf'}[0] // $self->{ap}->{'full_buf'}[0]; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"ap"}{"stdout_buf"}[0] // $self->{"ap"}{"full_buf"}[0]
MC/DC0%0/2
$self->{"ap"}{"stdout_buf"}[0] // $self->{"ap"}{"full_buf"}[0]
$self->{"ap"}{"stdout_buf"}[0]
$self->{"ap"}{"full_buf"}[0]
Truth table0%0/3
A // B
A= $self->{"ap"}{"stdout_buf"}[0]B= $self->{"ap"}{"full_buf"}[0]
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ( !$tempfile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | return "Error writing to file"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub _strip_suffix { | ||||||||||||||||||||||||||
|
Subroutine
_strip_suffix: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 0 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $regex = sprintf( '\.(%s)$', join( '|', @{ $self->{suffixes} } ) ); | |||||||||||||||||||||||||||
| 83 | 0 | $file =~ s/$regex//; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | return $file; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Converts 'THE_OFFICE' to 'The Office' | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _clean_title { | ||||||||||||||||||||||||||
|
Subroutine
_clean_title: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 90 | 0 | my ( $self, $title ) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | $title =~ s/(-|_)/ /g; | |||||||||||||||||||||||||||
| 93 | 0 | $title =~ s/(?<=\w)\.(?=\w)/ /g; | |||||||||||||||||||||||||||
| 94 | 0 | $title = lc($title); | |||||||||||||||||||||||||||
| 95 | 0 | $title = join ' ', map( { ucfirst() } split /\s/, $title ); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | return $title; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # adds file to itunes | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _add_to_itunes { | ||||||||||||||||||||||||||
|
Subroutine
_add_to_itunes: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 102 | 0 | my ( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return unless $self->{'itunes'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | unless ( $^O eq 'darwin' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | say STDERR 'can only add to iTunes on Mac OSX'; | |||||||||||||||||||||||||||
| 108 | 0 | return 1; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | $path =~ s!/!:!g; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $cmd = sprintf( | |||||||||||||||||||||||||||
| 114 | "osascript -e 'tell application \"iTunes\" to add file \"%s\" to playlist \"Library\" of source \"Library\"'", | ||||||||||||||||||||||||||||
| 115 | $path ); | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my $result = `$cmd`; | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($?) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | say STDERR "error adding to iTunes: $result"; | |||||||||||||||||||||||||||
| 120 | 0 | return 1; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( $self->{'verbose'} and $result ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"verbose"} and $result
MC/DC0%0/2
$self->{"verbose"} and $result
$self->{"verbose"}
$result
Truth table0%0/3
A and B
A= $self->{"verbose"}B= $result
| |||||||||||||||||||||||||||||
| 123 | 0 | say $result; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | return; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # load a module as a new source | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub _new_source { | ||||||||||||||||||||||||||
|
Subroutine
_new_source: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 131 | 0 | my ( $self, $source ) = @_; | |||||||||||||||||||||||||||
| 132 | 0 | my $module = 'App::MP4Meta::Source::' . $source; | |||||||||||||||||||||||||||
| 133 | 0 | Module::Load::load($module); | |||||||||||||||||||||||||||
| 134 | 0 | return $module->new; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||