lib/App/DrivePlayer/Scanner.pm

filter: Statementstmt 96.9% 96 / 99 Branchbran 79.5% 35 / 44 Conditioncond 45.4% 15 / 33 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 78.4% 164 / 209 CC 38 SCAR 21.4
CC
38
Cov
83%
_scan_dir
12.1
_store_track
11.3
_parse_filename
9.8
CRAP 8.5
« lib/App/DrivePlayer/Player.pm ↑ Summary lib/App/DrivePlayer/DB.pm »
1 package App::DrivePlayer::Scanner;
2  
3 2 ▶use App::DrivePlayer::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Encode qw( decode_utf8 is_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 my $FOLDER_MIME = 'application/vnd.google-apps.folder';
7 2 my $DRIVE_FIELDS = 'files(id,name,mimeType,size,modifiedTime,parents,videoMediaMetadata)';
8  
9 # Google::RestApi returns HTTP bodies as raw UTF-8 byte strings (no utf8
10 # flag). Passing those directly to DBIx::Class with sqlite_unicode=1 causes
11 # double-encoding: each byte is re-encoded as if it were Latin-1. Decode
12 # once at the boundary so the rest of the code sees proper Unicode.
13 15 ▶sub _u8 {
Subroutine _u8: called CC=5 SCAR=18.9
14 15 my ($s) = @_;
15 15 ▶ return $s unless defined $s && length $s && !is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s and not &is_utf8($s) 0 15 15
Condition33%2/6
defined $s and length $s and not &is_utf8($s)
!ll&&!rl&&r
0015
defined $s and length $s
!ll&&!rl&&r
0015
MC/DC0%0/3
defined $s and length $s and not &is_utf8($s)
defined $s length $s not &is_utf8($s)
Truth table25%1/4
A and B and not(C)
A= defined $sB= length $sC= not &is_utf8($s)
ABCresult
0XX0
10X0
1100
1111
16 15 my $d = eval { decode_utf8($s, Encode::FB_CROAK) };
17 15 ▶ return $@ ? $s : $d;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 15 15
18 }
19  
20 2 Readonly my $LARGE_DELETION_THRESHOLD => 10;
21  
22 2 has drive => (
23 is => 'ro',
24 isa => HasMethods['list'],
25 required => 1,
26 );
27  
28 2 has db => (
29 is => 'ro',
30 isa => HasMethods['upsert_scan_folder', 'upsert_folder', 'upsert_track'],
31 required => 1,
32 );
33  
34 2 has on_progress => (
35 is => 'ro',
36 isa => CodeRef,
37 predicate => 1,
38 );
39  
40 2 has on_track_found => (
41 is => 'ro',
42 isa => CodeRef,
43 predicate => 1,
44 );
45  
46 has on_large_deletion => (
47 is => 'ro',
48 isa => Maybe[CodeRef],
49 8 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 2 );
51  
52 2 has _stop => (
53 is => 'rw',
54 isa => Bool,
55 default => 0,
56 );
57  
58 8 ▶has _seen_track_ids => ( is => 'rw', isa => HashRef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
59 8 ▶has _seen_folder_ids => ( is => 'rw', isa => HashRef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
60  
61 1 ▶sub stop { $_[0]->_stop(1) }
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
62  
63 8 ▶sub scan_folder {
Subroutine scan_folder: called CC=5 SCAR=16.5
Pod documented
64 8 my ($self, $drive_id, $name) = @_;
65  
66 8 $self->_stop(0);
67 8 $self->_seen_track_ids({});
68 8 $self->_seen_folder_ids({});
69  
70 8 my $scan_folder = $self->db->upsert_scan_folder($drive_id, $name);
71  
72 $self->_scan_dir(
73 folder_drive_id => $drive_id,
74 folder_name => $name,
75 parent_drive_id => undef,
76 path => $name,
77 scan_folder_id => $scan_folder->{id},
78 8 );
79  
80 8 my ($removed_tracks, $removed_folders) = (0, 0);
81 8 ▶ unless ($self->_stop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_stop) 7 1 8
82 7 my $pending = $self->db->count_unseen_tracks($scan_folder->{id}, $self->_seen_track_ids);
83 7 ▶ my $confirmed = $pending <= $LARGE_DELETION_THRESHOLD
Condition33%2/6
$pending <= $LARGE_DELETION_THRESHOLD || !$self->on_large_deletion || $self->on_large_deletion->($pending, $name)
l!l&&r!l&&!r
700
$pending <= $LARGE_DELETION_THRESHOLD or not $self->on_large_deletion
l!l&&r!l&&!r
700
MC/DC0%0/3
$pending <= $LARGE_DELETION_THRESHOLD || !$self->on_large_deletion || $self->on_large_deletion->($pending, $name)
$pending <= $LARGE_DELETION_THRESHOLD not $self->on_large_deletion $self->on_large_deletion->($pending, $name)
Truth table25%1/4
A or not(B) || C
A= $pending <= $LARGE_DELETION_THRESHOLDB= not $self->on_large_deletionC= $self->on_large_deletion->($pending, $name)
ABCresult
1XX1
01X1
0011
0000
84 || !$self->on_large_deletion
85 || $self->on_large_deletion->($pending, $name);
86 7 ▶ if ($confirmed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($confirmed) 7 0 7
87 7 $removed_tracks = $self->db->remove_unseen_tracks($scan_folder->{id}, $self->_seen_track_ids);
88 7 $removed_folders = $self->db->remove_unseen_folders($scan_folder->{id}, $self->_seen_folder_ids);
89 }
90 }
91  
92 8 return { removed_tracks => $removed_tracks, removed_folders => $removed_folders };
93 }
94  
95 12 ▶sub _progress {
Subroutine _progress: called CC=2 SCAR=6.9
96 12 my ($self, $msg) = @_;
97 12 ▶ $self->on_progress->($msg) if $self->has_on_progress;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_on_progress 4 8 12
98 }
99  
100 11 ▶sub _scan_dir {
Subroutine _scan_dir: called CC=12 SCAR=24.9
101 11 my ($self, %args) = @_;
102 11 ▶ return if $self->_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stop 0 11 11
103  
104 11 my $drive_id = $args{folder_drive_id};
105 11 my $folder_name = $args{folder_name};
106 11 my $path = $args{path};
107 11 my $scan_folder_id = $args{scan_folder_id};
108  
109 11 $self->_progress("Scanning: $path");
110 11 ▶ return if $self->_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stop 1 10 11
111  
112 10 $self->_seen_folder_ids->{$drive_id} = 1;
113  
114 my $folder = $self->db->upsert_folder(
115 drive_id => $drive_id,
116 name => $folder_name,
117 parent_drive_id => $args{parent_drive_id},
118 10 path => $path,
119 scan_folder_id => $scan_folder_id,
120 );
121  
122 10 my @items = eval {
123 10 $self->drive->list(
124 filter => "'$drive_id' in parents and trashed=false",
125 params => { fields => $DRIVE_FIELDS, pageSize => 1000 },
126 );
127 };
128 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 9 10
129 1 $self->_progress("Error scanning $path: $@");
130 1 return;
131 }
132  
133 # Normalize Drive response bytes to decoded Unicode so SQLite doesn't
134 # re-encode them as Latin-1 → UTF-8 (double-encoding).
135 9 $_->{name} = _u8($_->{name}) for @items;
136  
137 9 my (@subfolders, @audio_files);
138 9 for my $item (@items) {
139 15 ▶ if ($item->{mimeType} eq $FOLDER_MIME) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item->{'mimeType'} eq $FOLDER_MIME) { } 3 12 15
elsif ($item->{'mimeType'} =~ m[^audio/]i) { } 10 2 12
140 3 push @subfolders, $item;
141 } elsif ($item->{mimeType} =~ m{^audio/}i) {
142 10 push @audio_files, $item;
143 }
144 }
145  
146 9 for my $file (@audio_files) {
147 10 ▶ return if $self->_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stop 0 10 10
148 10 $self->_store_track($file, $folder, $path);
149 }
150  
151 9 for my $subfolder (@subfolders) {
152 3 ▶ return if $self->_stop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stop 0 3 3
153 $self->_scan_dir(
154 folder_drive_id => $subfolder->{id},
155 folder_name => $subfolder->{name},
156 3 parent_drive_id => $drive_id,
157 path => "$path/$subfolder->{name}",
158 scan_folder_id => $scan_folder_id,
159 );
160 }
161 }
162  
163 10 ▶sub _store_track {
Subroutine _store_track: called CC=10 SCAR=24.3
164 10 my ($self, $file, $folder, $folder_path) = @_;
165  
166 10 $self->_seen_track_ids->{$file->{id}} = 1;
167  
168 10 my ($title, $artist, $album, $track_num, $year) = _parse_filename($file->{name});
169  
170 # Infer artist/album from folder path depth: Root/Artist/Album/track
171 10 my @parts = split m{/}, $folder_path;
172 10 ▶ if (!$artist && @parts >= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $artist and @parts >= 3) { } 1 9 10
elsif (not $album and @parts >= 2) { } 2 7 9
Condition100%3/3
not $artist and @parts >= 3
!ll&&!rl&&r
271
MC/DC100%2/2
not $artist and @parts >= 3
not $artist @parts >= 3
Truth table100%3/3
not(A) and B
A= not $artistB= @parts >= 3
ABresult
0X0
100
111
173 1 $artist = $parts[-2];
174 1 $album = $parts[-1];
175 ▶ } elsif (!$album && @parts >= 2) {
Condition66%2/3
not $album and @parts >= 2
!ll&&!rl&&r
072
MC/DC50%1/2
not $album and @parts >= 2
not $album @parts >= 2
Truth table66%2/3
not(A) and B
A= not $albumB= @parts >= 2
ABresult
0X0
100
111
176 2 $album = $parts[-1];
177 }
178  
179 # "YYYY-AlbumName" folder convention: strip the year prefix and use the
180 # captured year as the track's year when one wasn't parsed from the file.
181 10 ▶ if ($album && $album =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if ($album and $album =~ s/ ^ ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx - //x) 0 10 10
Condition66%2/3
$album and $album =~ s/ ^ ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx - //x
!ll&&!rl&&r
730
MC/DC0%0/2
$album and $album =~ s/ ^ ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx - //x
$album $album =~ s/ ^ ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx - //x
Truth table66%2/3
A and B
A= $albumB= $album =~ s/ ^ ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx - //x
ABresult
0X0
100
111
182 ^
183 ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx
184 -
185 }{}x) {
186 0 ▶ $year //= $1;
Condition0%0/3
$year //= $1
l!l&&r!l&&!r
000
MC/DC0%0/2
$year //= $1
$year $1
Truth table0%0/3
A //= B
A= $yearB= $1
ABresult
1X1
011
000
187 }
188  
189 10 my $duration_ms;
190 10 ▶ if (my $meta = $file->{videoMediaMetadata}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = $file->{"videoMediaMetadata"}) 0 10 10
191 0 $duration_ms = $meta->{durationMillis};
192 }
193  
194 my %track = (
195 drive_id => $file->{id},
196 title => $title,
197 artist => $artist,
198 album => $album,
199 track_number => $track_num,
200 year => $year,
201 duration_ms => $duration_ms,
202 size => $file->{size},
203 mime_type => $file->{mimeType},
204 modified_time => $file->{modifiedTime},
205 folder_id => $folder->{id},
206 10 folder_path => $folder_path,
207 );
208  
209 10 $self->db->upsert_track(%track);
210 10 ▶ $self->on_track_found->(\%track) if $self->has_on_track_found;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_on_track_found 2 8 10
211 }
212  
213 # Parse filename into (title, artist, album, track_number, year).
214 # Handles: "NN - Artist - Title", "Artist - Title", "NN - Title", "Title"
215 19 ▶sub _parse_filename {
Subroutine _parse_filename: called CC=9 SCAR=22.9
216 19 my ($filename) = @_;
217 19 (my $base = $filename) =~ s/\.[^.]+$//;
218  
219 19 my ($title, $artist, $album, $track_num, $year);
220  
221 # "YYYY-..." prefix: capture the year and strip it before running the
222 # track-number patterns below (otherwise 2001 would match \d+ as track#).
223 19 ▶ if ($base =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if ($base =~ s/ ^ ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx - //x) 0 19 19
224 ^
225 ( (?: 19 | 20) \d{2} ) # 4-digit year, 19xx or 20xx
226 -
227 }{}x) {
228 0 $year = $1;
229 }
230  
231 19 ▶ if ($base =~ /^(\d+)[\s.\-]+(.+?)\s+[-–]\s+(.+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base =~ /^(\d+)[\s.\-]+(.+?)\s+[-\x{2013}]\s+(.+)$/) { } 2 17 19
elsif ($base =~ /^(\d+)[\s.\-]+(.+)$/) { } 3 14 17
elsif ($base =~ /^(.+?)\s+[-\x{2013}]\s+(.+)$/) { } 4 10 14
232 2 ($track_num, $artist, $title) = ($1 + 0, $2, $3);
233 } elsif ($base =~ /^(\d+)[\s.\-]+(.+)$/) {
234 3 ($track_num, $title) = ($1 + 0, $2);
235 } elsif ($base =~ /^(.+?)\s+[-–]\s+(.+)$/) {
236 4 ($artist, $title) = ($1, $2);
237 } else {
238 10 $title = $base;
239 }
240  
241 19 ▶ if (!$year && $title && $title =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if (not $year and $title and $title =~ s/ \s* [ \( \[ ] # opening paren or bracket ( (?: 19 | 20) \d{2} ) # 4-digit year [ \) \] ] # closing paren or bracket \s* $ //x) 2 17 19
Condition50%3/6
not $year and $title and $title =~ s/ \s* [ \( \[ ] # opening paren or bracket ( (?: 19 | 20) \d{2} ) # 4-digit year [ \) \] ] # closing paren or bracket \s* $ //x
!ll&&!rl&&r
0172
not $year and $title
!ll&&!rl&&r
0019
MC/DC33%1/3
not $year and $title and $title =~ s/ \s* [ \( \[ ] # opening paren or bracket ( (?: 19 | 20) \d{2} ) # 4-digit year [ \) \] ] # closing paren or bracket \s* $ //x
not $year $title $title =~ s/ \s* [ \( \[ ] # opening paren or bracket ( (?: 19 | 20) \d{2} ) # 4-digit year [ \) \] ] # closing paren or bracket \s* $ //x
Truth table50%2/4
not(A) and B and C
A= not $yearB= $titleC= $title =~ s/ \s* [ \( \[ ] # opening paren or bracket ( (?: 19 | 20) \d{2} ) # 4-digit year [ \) \] ] # closing paren or bracket \s* $ //x
ABCresult
0XX0
10X0
1100
1111
242 \s*
243 [ \( \[ ] # opening paren or bracket
244 ( (?: 19 | 20) \d{2} ) # 4-digit year
245 [ \) \] ] # closing paren or bracket
246 \s*
247 $
248 }{}x) {
249 2 $year = $1;
250 }
251  
252 19 ▶ return ($title // $base, $artist, $album, $track_num, $year);
Condition33%1/3
$title // $base
l!l&&r!l&&!r
1900
MC/DC0%0/2
$title // $base
$title $base
Truth table33%1/3
A // B
A= $titleB= $base
ABresult
1X1
011
000
253 }
254  
255 2 1;
256  
257 __END__
« lib/App/DrivePlayer/Player.pm ↑ Summary lib/App/DrivePlayer/DB.pm »