blib/lib/App/Plex/Archiver.pm

filter: Statementstmt 43.4% 40 / 92 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 75.0% 12 / 16 Podpod 100% 4 / 4 totaltotal 33.1% 56 / 169 CC 29 SCAR 46.2
CC
29
Cov
28%
get_tmdb_info
240.0
copy_file
72.0
get_tmbd_api_key
42.0
CRAP 101.7
↑ Summary
1 package App::Plex::Archiver;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Archives movies (mp4, avi, mov, mkv) into a directory structure for the Plex Media server
7  
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Copy qw( copy );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Lingua::EN::Titlecase;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Prompter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 our @EXPORT_OK = qw( title_from_filename copy_file get_tmbd_api_key get_tmdb_info );
20  
21 1 Readonly my $DEFAULT_TMDB_API_KEY_FILENAME => ".tmdb-api-key";
22  
23 ## Version string
24 1 our $VERSION = qq{0.01};
25  
26 #------------------------------------------------------------------------------
27 #------------------------------------------------------------------------------
28 0 ▶sub title_from_filename {
Subroutine title_from_filename: not called CC=3 SCAR=24.8
Pod documented
29 0 ▶ my $filename = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
30  
31 0 ▶ return '' unless ($filename);
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
32  
33 # Strip the path and extension
34 0 my ($basename, $path, undef) = fileparse($filename, qr/\.[^.]*$/);
35  
36 # The $basename now contains the filename without path or extension.
37 # Example: "my_document-v1.0.txt" -> "my_document-v1.0"
38  
39 # Convert underscores and dash to spaces
40 0 $basename =~ s/[_-]/ /g;
41  
42 0 return scalar(Lingua::EN::Titlecase->new($basename)->title());
43 }
44  
45 #------------------------------------------------------------------------------
46 #------------------------------------------------------------------------------
47 0 ▶sub copy_file {
Subroutine copy_file: not called CC=8 SCAR=42.8
Pod documented
48 0 my ( $source, $destination, $delete_source ) = @_;
49  
50 0 ▶ croak "Source file '$source' does not exist" unless -e $source;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $source 0 0 0
51 0 ▶ croak "Source '$source' is not a regular file" unless -f $source;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $source 0 0 0
52 0 ▶ croak "Cannot read source file '$source'" unless -r $source;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $source 0 0 0
53 0 ▶ croak "Destination directory is not writable" unless -w dirname($destination);
Branch: 1 branch
BranchTrueFalseTotal
unless -w dirname($destination) 0 0 0
54  
55 0 ▶ copy( $source, $destination )
Branch: 1 branch
BranchTrueFalseTotal
copy($source, $destination) or croak("Failed to copy '${source}' to '${destination}': $!") 0 0 0
56 or croak "Failed to copy '$source' to '$destination': $!";
57  
58 0 ▶ if ($delete_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delete_source) 0 0 0
59 0 ▶ unlink $source
Branch: 1 branch
BranchTrueFalseTotal
unlink $source or croak("Copied but failed to delete '${source}': $!") 0 0 0
60 or croak "Copied but failed to delete '$source': $!";
61 }
62 }
63  
64 #------------------------------------------------------------------------------
65 #------------------------------------------------------------------------------
66 0 ▶sub get_tmbd_api_key {
Subroutine get_tmbd_api_key: not called CC=6 SCAR=37.4
Pod documented
67 0 ▶ my $filename = shift // "";
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
68  
69 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) 0 0 0
70 0 ▶ if (!-f "$filename") {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f "$filename") 0 0 0
71 0 return "'$filename' does not exist, or is not a regular file";
72 }
73  
74 0 return (undef, read_file($filename, chomp => 1,));
75 }
76  
77 0 my @tried;
78 0 for my $path ('.', File::HomeDir->my_home) {
79 0 my $filename = File::Spec->catfile($path, $DEFAULT_TMDB_API_KEY_FILENAME);
80 0 ▶ if (-f "$filename") {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "$filename") 0 0 0
81 0 return (undef, read_file($filename, chomp => 1,));
82 }
83 0 push(@tried, $filename);
84 }
85  
86 0 return sprintf("Could not locate TMDB API key file: '%s'", join("', '", @tried));
87 }
88  
89 #------------------------------------------------------------------------------
90 #------------------------------------------------------------------------------
91 0 ▶sub get_tmdb_info {
Subroutine get_tmdb_info: not called CC=15 SCAR=54.8
Pod documented
92 0 my $tmdb = shift;
93 0 ▶ my $title = shift // "";
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
94 0 ▶ my $debug = shift // 0;
Condition0%0/2
shift() // 0
l!l
00
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
95  
96 0 ▶ return unless ($tmdb && $title);
Branch: 1 branch
BranchTrueFalseTotal
unless $tmdb and $title 0 0 0
Condition0%0/3
$tmdb and $title
!ll&&!rl&&r
000
MC/DC0%0/2
$tmdb and $title
$tmdb $title
Truth table0%0/3
A and B
A= $tmdbB= $title
ABresult
0X0
100
111
97  
98 0 my @responses = $tmdb->search(max_pages => 1)->movie($title);
99  
100 0 for my $response (@responses) {
101 0 ▶ $response->{release_year} = $response->{release_date} // "";
Condition0%0/2
$response->{"release_date"} // ""
l!l
00
MC/DC0%0/1
$response->{"release_date"} // ""
$response->{"release_date"}
Truth table0%0/2
A
A= $response->{"release_date"}
Aresult
11
00
102 0 my @parts = split(/-/, $response->{release_year});
103 0 ▶ $response->{release_year} = $parts[0] // "";
Condition0%0/2
$parts[0] // ""
l!l
00
MC/DC0%0/1
$parts[0] // ""
$parts[0]
Truth table0%0/2
A
A= $parts[0]
Aresult
11
00
104 }
105  
106 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
107 0 my $idx = 1;
108 0 for my $response (@responses) {
109 printf( "%2d - [id: %s]:\t%s (%s)\n",
110 $idx,
111 $response->{id},
112 $response->{title},
113 $response->{release_year},
114 0 );
115 0 $idx++;
116 }
117 }
118  
119 0 my $menu = [];
120 0 for my $movie (@responses) {
121 0 push(@$menu, sprintf("\"%s\" (%s)", $movie->{title}, $movie->{release_year}));
122 }
123 0 ▶ if (scalar(@$menu)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$menu) { } 0 0 0
124 0 my $selected = prompt(
125 "Matching titles ...",
126 -menu => $menu,
127 -number,
128 "Your choice: "
129 );
130  
131 0 my $idx = 0;
132 0 ▶ while ($idx < scalar(@$menu)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < scalar @$menu) 0 0 0
133 0 ▶ if ($selected eq $menu->[$idx]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selected eq $menu->[$idx]) 0 0 0
134 0 ▶ printf("returning id='%s', release_year='%s'\n", $responses[$idx]->{id}, $responses[$idx]->{release_year}) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
135 0 return ($responses[$idx]->{id}, $responses[$idx]->{release_year});
136 }
137 0 $idx++;
138 }
139 } else {
140 0 printf("No matching titles found!\n");
141 }
142  
143 0 return;
144 }
145  
146 1 1;
147  
148 __END__
↑ Summary