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