| 1 | package App::DrivePlayer::MetadataFetcher; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use App::DrivePlayer::Setup; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use File::Temp qw( tempfile ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON::PP qw( decode_json ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use URI::Escape qw( uri_escape_utf8 ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Time::HiRes qw( sleep time usleep ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my $log = do { eval { require Log::Log4perl; Log::Log4perl->get_logger(__PACKAGE__) } }; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | Readonly my $USER_AGENT => 'DrivePlayer/1.0 (https://github.com/mvsjes2/drive_player)'; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | Readonly my $ITUNES_BASE => 'https://itunes.apple.com/search'; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | Readonly my $MB_BASE => 'https://musicbrainz.org/ws/2'; | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | Readonly my $AID_BASE => 'https://api.acoustid.org/v2/lookup'; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | Readonly my $DRIVE_URL => 'https://www.googleapis.com/drive/v3/files/%s?alt=media'; | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | Readonly my $MB_MIN_GAP => 1.1; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | Readonly my $DOWNLOAD_MB => 5; # MB to download for fingerprinting | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | my $last_mb_req = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | has yield => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | isa => Maybe[CodeRef], | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | has acoustid_key => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | default => sub { '' }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | has token_fn => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => Maybe[CodeRef], | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | has _fp_stage => ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # Returns a short description of where the last fingerprint lookup stopped. | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub last_fp_stage { $_[0]->_fp_stage } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_fp_stage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # Public: text-based lookup (iTunes -> MusicBrainz, with title cleaning) | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $title = $args{title} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my $artist = _clean_field($args{artist} // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"artist"} // ""
MC/DC0%0/1
$args{"artist"} // ""
$args{"artist"}
Truth table0%0/2
A
A= $args{"artist"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my $album = _clean_field($args{album} // ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"album"} // ""
MC/DC0%0/1
$args{"album"} // ""
$args{"album"}
Truth table0%0/2
A
A= $args{"album"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $log->debug("Text search: title='$title' artist='$artist'") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # 1. Try with original values | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my $meta = $self->_fetch_itunes($title, $artist, $album) | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->_fetch_itunes($title, $artist, $album) // $self->_fetch_musicbrainz($title, $artist, $album)
MC/DC0%0/2
$self->_fetch_itunes($title, $artist, $album) // $self->_fetch_musicbrainz($title, $artist, $album)
$self->_fetch_itunes($title, $artist, $album)
$self->_fetch_musicbrainz($title, $artist, $album)
Truth table0%0/3
A // B
A= $self->_fetch_itunes($title, $artist, $album)B= $self->_fetch_musicbrainz($title, $artist, $album)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | // $self->_fetch_musicbrainz($title, $artist, $album); | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($meta) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $log->debug("Text search hit for '$title'") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return $meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # 2. Try with cleaned title | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $clean = _clean_title($title); | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ($clean eq $title) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $log->debug("Text search miss for '$title' (no clean variant)") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $log->debug("Retrying with cleaned title '$clean'") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $meta = $self->_fetch_itunes($clean, $artist, $album) | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->_fetch_itunes($clean, $artist, $album) // $self->_fetch_musicbrainz($clean, $artist, $album)
MC/DC0%0/2
$self->_fetch_itunes($clean, $artist, $album) // $self->_fetch_musicbrainz($clean, $artist, $album)
$self->_fetch_itunes($clean, $artist, $album)
$self->_fetch_musicbrainz($clean, $artist, $album)
Truth table0%0/3
A // B
A= $self->_fetch_itunes($clean, $artist, $album)B= $self->_fetch_musicbrainz($clean, $artist, $album)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | // $self->_fetch_musicbrainz($clean, $artist, $album); | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $log->debug($meta ? "Text search hit (cleaned) for '$title'" : "Text search miss for '$title'") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | return $meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Public: AcoustID fingerprint lookup | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub fetch_by_fingerprint { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch_by_fingerprint: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $drive_id = $args{drive_id} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->_fp_stage(undef); | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return unless $self->acoustid_key && $self->token_fn; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->acoustid_key and $self->token_fn
MC/DC0%0/2
$self->acoustid_key and $self->token_fn
$self->acoustid_key
$self->token_fn
Truth table0%0/3
A and B
A= $self->acoustid_keyB= $self->token_fn
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return unless fpcalc_available(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->_fp_stage('downloading audio'); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $log->info("Fingerprint: downloading audio for drive_id=$drive_id") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $tmpfile = $self->_download_partial($drive_id); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | unless ($tmpfile) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $self->_fp_stage('download failed (token expired or Drive error)'); | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $log->warn("Fingerprint: download failed for drive_id=$drive_id") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $self->_fp_stage('running fpcalc'); | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $log->debug("Fingerprint: running fpcalc on $tmpfile") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my $fp = _fingerprint($tmpfile); | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | unlink $tmpfile; | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | unless ($fp) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->_fp_stage('fpcalc produced no fingerprint'); | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $log->warn("Fingerprint: fpcalc produced no output for drive_id=$drive_id") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $self->_fp_stage('querying AcoustID'); | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $log->debug("Fingerprint: querying AcoustID (duration=$fp->{duration}s)") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $aid = $self->_query_acoustid($fp); | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | unless ($aid) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->_fp_stage('no AcoustID match (score too low or track unknown)'); | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | $log->info("Fingerprint: no AcoustID match for drive_id=$drive_id") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $self->_fp_stage('fetching MusicBrainz metadata'); | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | $log->info("Fingerprint: AcoustID matched MusicBrainz recording $aid") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $meta = $self->_fetch_musicbrainz_by_id($aid); | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $self->_fp_stage($meta ? undef : 'MusicBrainz returned no data'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $log->info($meta ? "Fingerprint: metadata found for drive_id=$drive_id" : "Fingerprint: MusicBrainz returned no data for $aid") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | return $meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # Title cleaning | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub _clean_field { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clean_field: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | $s =~ s/_/ /g; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $s =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $s =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _clean_title { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clean_title: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my ($t) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | # Normalise underscores and whitespace before any pattern matching | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $t =~ s/_/ /g; | |||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $t =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $t =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # Strip leading track number: "01.", "01 -", "(01)", "[01]", "01 " | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $t =~ s/^\s*[\(\[]?\d{1,3}[\)\]]?[\s.\-]+//; | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # Strip feat/ft/featuring/with credits | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | $t =~ s/\s*[\(\[](feat|ft|featuring|with)\.?\s+[^\)\]]+[\)\]]//ig; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | $t =~ s/\s+(feat|ft|featuring)\s+.+$//ig; | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # Strip common parenthetical junk (order matters: specific before generic) | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my @strip = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | qr/\s*[\(\[]\s*(?:\d{4}\s+)?(?:digital\s+)?remaster(?:ed)?(?:\s+\d{4})?\s*[\)\]]/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | qr/\s*[\(\[]\s*live(?:\s+at\s+[^\)\]]+)?\s*[\)\]]/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | qr/\s*[\(\[]\s*(?:official\s+)?(?:audio|video|lyric\s+video|music\s+video)\s*[\)\]]/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | qr/\s*[\(\[]\s*(?:radio|single|album|extended|original|instrumental|acoustic|demo|mono|stereo)\s+(?:edit|version|mix|take)?\s*[\)\]]/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | qr/\s*[\(\[]\s*(?:explicit|clean|censored)\s*[\)\]]/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | qr/\s*[\(\[]\s*(?:hd|hq|\d+hz|\d+\s*hz|4k)\s*[\)\]]/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | qr/\s*-\s*(?:single|ep|soundtrack)\s*$/i, | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | qr/\s*[\(\[]\s*(?:19|20)\d{2}\s*[\)\]]/, | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | for my $re (@strip) { $t =~ s/$re//g } | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | return _clean_field($t); | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # iTunes | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub _fetch_itunes { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_itunes: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my ($self, $title, $artist, $album) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | my $term = join(' ', grep { length } $artist, $title); | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my $url = $ITUNES_BASE . '?term=' . uri_escape_utf8($term) | |||||||||||||||||||||||||||||||||||||||||||||
| 182 | . '&entity=song&media=music&limit=5'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | my $data = $self->_get_plain($url) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | my $results = $data->{results} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | return unless @$results; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $best = _best_itunes_match($results, $title, $artist, $album); | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | return unless $best; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | my %meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | $meta{title} = $best->{trackName} if $best->{trackName}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | $meta{artist} = $best->{artistName} if $best->{artistName}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | $meta{album} = $best->{collectionName} if $best->{collectionName}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | $meta{genre} = $best->{primaryGenreName} if $best->{primaryGenreName}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | $meta{track_number} = $best->{trackNumber} if $best->{trackNumber}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | ($meta{year}) = ($best->{releaseDate} // '') =~ /^(\d{4})/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$best->{"releaseDate"} // ""
MC/DC0%0/1
$best->{"releaseDate"} // ""
$best->{"releaseDate"}
Truth table0%0/2
A
A= $best->{"releaseDate"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | return \%meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | sub _best_itunes_match { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_best_itunes_match: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my ($results, $want_title, $want_artist, $want_album) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my $score = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my ($r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | my $s = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $s += 3 if $want_title && _fuzzy($r->{trackName}, $want_title); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$want_title and _fuzzy($r->{"trackName"}, $want_title)
MC/DC0%0/2
$want_title and _fuzzy($r->{"trackName"}, $want_title)
$want_title
_fuzzy($r->{"trackName"}, $want_title)
Truth table0%0/3
A and B
A= $want_titleB= _fuzzy($r->{"trackName"}, $want_title)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | $s += 2 if $want_artist && _fuzzy($r->{artistName}, $want_artist); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$want_artist and _fuzzy($r->{"artistName"}, $want_artist)
MC/DC0%0/2
$want_artist and _fuzzy($r->{"artistName"}, $want_artist)
$want_artist
_fuzzy($r->{"artistName"}, $want_artist)
Truth table0%0/3
A and B
A= $want_artistB= _fuzzy($r->{"artistName"}, $want_artist)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | $s += 1 if $want_album && _fuzzy($r->{collectionName}, $want_album); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$want_album and _fuzzy($r->{"collectionName"}, $want_album)
MC/DC0%0/2
$want_album and _fuzzy($r->{"collectionName"}, $want_album)
$want_album
_fuzzy($r->{"collectionName"}, $want_album)
Truth table0%0/3
A and B
A= $want_albumB= _fuzzy($r->{"collectionName"}, $want_album)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my ($best) = sort { $score->($b) <=> $score->($a) } @$results; | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | return unless $want_title && _fuzzy($best->{trackName}, $want_title); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$want_title and _fuzzy($best->{"trackName"}, $want_title)
MC/DC0%0/2
$want_title and _fuzzy($best->{"trackName"}, $want_title)
$want_title
_fuzzy($best->{"trackName"}, $want_title)
Truth table0%0/3
A and B
A= $want_titleB= _fuzzy($best->{"trackName"}, $want_title)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | return $best; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub _fuzzy { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fuzzy: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my ($hay, $needle) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | return unless defined $hay && defined $needle && length $needle; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $hay and defined $needle and length $needle
defined $hay and defined $needle
MC/DC0%0/3
defined $hay and defined $needle and length $needle
defined $hay
defined $needle
length $needle
Truth table0%0/4
A and B and C
A= defined $hayB= defined $needleC= length $needle
| |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | return index(lc($hay), lc($needle)) >= 0 | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
index(lc $hay, lc $needle) != -1 || index(lc $needle, lc $hay) != -1
MC/DC0%0/2
index(lc $hay, lc $needle) != -1 || index(lc $needle, lc $hay) != -1
index(lc $hay, lc $needle) != -1
index(lc $needle, lc $hay) != -1
Truth table0%0/3
A || B
A= index(lc $hay, lc $needle) != -1B= index(lc $needle, lc $hay) != -1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | || index(lc($needle), lc($hay)) >= 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # MusicBrainz (text search, fuzzy, progressive relaxation) | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub _fetch_musicbrainz { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_musicbrainz: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my ($self, $title, $artist, $album) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my @attempts; | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | push @attempts, 'recording:' . _mb_q($title) . '~ AND artist:' . _mb_q($artist) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$artist and $album
MC/DC0%0/2
$artist and $album
$artist
$album
Truth table0%0/3
A and B
A= $artistB= $album
| |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | . '~ AND release:' . _mb_q($album) . '~' | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | if $artist && $album; | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | push @attempts, 'recording:' . _mb_q($title) . '~ AND artist:' . _mb_q($artist) . '~' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | if $artist; | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | push @attempts, 'recording:' . _mb_q($title) . '~'; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | for my $query (@attempts) { | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | my $url = "$MB_BASE/recording?query=" . uri_escape_utf8($query) | |||||||||||||||||||||||||||||||||||||||||||||
| 240 | . '&fmt=json&limit=5&inc=releases+artist-credits+tags+genres'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | my $data = $self->_get_mb($url) or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | my $recs = $data->{recordings} or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | next unless @$recs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my $meta = $self->_parse_mb_with_release($recs->[0]); | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | ▶ | return $meta if $meta && %$meta; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$meta and %$meta
MC/DC0%0/2
$meta and %$meta
$meta
%$meta
Truth table0%0/3
A and B
A= $metaB= %$meta
| |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | sub _fetch_musicbrainz_by_id { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_musicbrainz_by_id: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | my ($self, $mb_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my $url = "$MB_BASE/recording/$mb_id?fmt=json&inc=releases+artist-credits+tags+genres"; | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | ▶ | my $rec = $self->_get_mb($url) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | return $self->_parse_mb_with_release($rec); | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | sub _parse_mb_with_release { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_mb_with_release: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my ($self, $rec) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my %meta = %{ _parse_mb($rec) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # If we didn't get a genre from the recording, try the release-group level | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if (!$meta{genre} && (my $rel = _best_mb_release($rec->{releases} // []))) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
not $meta{"genre"} and my $rel = _best_mb_release($rec->{"releases"} // [])
$rec->{"releases"} // []
MC/DC0%0/2
not $meta{"genre"} and my $rel = _best_mb_release($rec->{"releases"} // [])
not $meta{"genre"}
my $rel = _best_mb_release($rec->{"releases"} // [])
MC/DC0%0/1
$rec->{"releases"} // []
$rec->{"releases"}
Truth table0%0/3
not(A) and B
A= not $meta{"genre"}B= my $rel = _best_mb_release($rec->{"releases"} // [])
Truth table0%0/2
A
A= $rec->{"releases"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | my $rg_url = "$MB_BASE/release/$rel->{id}?fmt=json&inc=release-groups+genres+tags"; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my $rd = $self->_get_mb($rg_url); | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if ($rd) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | $meta{genre} //= _best_mb_genre($rd->{genres}, $rd->{tags}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$meta{"genre"} //= _best_mb_genre($rd->{"genres"}, $rd->{"tags"})
MC/DC0%0/2
$meta{"genre"} //= _best_mb_genre($rd->{"genres"}, $rd->{"tags"})
$meta{"genre"}
_best_mb_genre($rd->{"genres"}, $rd->{"tags"})
Truth table0%0/3
A //= B
A= $meta{"genre"}B= _best_mb_genre($rd->{"genres"}, $rd->{"tags"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if (!$meta{genre} && (my $rg = $rd->{'release-group'})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $meta{"genre"} and my $rg = $rd->{"release-group"}
MC/DC0%0/2
not $meta{"genre"} and my $rg = $rd->{"release-group"}
not $meta{"genre"}
my $rg = $rd->{"release-group"}
Truth table0%0/3
not(A) and B
A= not $meta{"genre"}B= my $rg = $rd->{"release-group"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $rg_full_url = "$MB_BASE/release-group/$rg->{id}?fmt=json&inc=genres+tags"; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | my $rgd = $self->_get_mb($rg_full_url); | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | $meta{genre} //= _best_mb_genre($rgd->{genres}, $rgd->{tags}) if $rgd; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$meta{"genre"} //= _best_mb_genre($rgd->{"genres"}, $rgd->{"tags"})
MC/DC0%0/2
$meta{"genre"} //= _best_mb_genre($rgd->{"genres"}, $rgd->{"tags"})
$meta{"genre"}
_best_mb_genre($rgd->{"genres"}, $rgd->{"tags"})
Truth table0%0/3
A //= B
A= $meta{"genre"}B= _best_mb_genre($rgd->{"genres"}, $rgd->{"tags"})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | return \%meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub _parse_mb { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_mb: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my ($rec) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | my %meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | $meta{title} = $rec->{title} if $rec->{title}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if (my $credits = $rec->{'artist-credit'}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | $meta{artist} = join(', ', | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | map { $_->{name} // $_->{artist}{name} // () } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
($_->{"name"} // $_->{"artist"}{"name"}) // ()
$_->{"name"} // $_->{"artist"}{"name"}
MC/DC0%0/3
($_->{"name"} // $_->{"artist"}{"name"}) // ()
$_->{"name"}
$_->{"artist"}{"name"}
()
Truth table0%0/4
A // B // C
A= $_->{"name"}B= $_->{"artist"}{"name"}C= ()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | grep { ref $_ eq 'HASH' } @$credits | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if (my $rel = _best_mb_release($rec->{releases} // [])) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$rec->{"releases"} // []
MC/DC0%0/1
$rec->{"releases"} // []
$rec->{"releases"}
Truth table0%0/2
A
A= $rec->{"releases"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | $meta{album} = $rel->{title}; | |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | ($meta{year}) = ($rel->{date} // '') =~ /^(\d{4})/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$rel->{"date"} // ""
MC/DC0%0/1
$rel->{"date"} // ""
$rel->{"date"}
Truth table0%0/2
A
A= $rel->{"date"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | $meta{genre} = _best_mb_genre($rec->{genres}, $rec->{tags}); | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | return \%meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # Prefer MB's curated genres over folksonomy tags; pick the highest-count entry. | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | sub _best_mb_genre { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_best_mb_genre: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my ($genres, $tags) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | if ($genres && @$genres) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$genres and @$genres
MC/DC0%0/2
$genres and @$genres
$genres
@$genres
Truth table0%0/3
A and B
A= $genresB= @$genres
| |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my ($top) = sort { $b->{count} <=> $a->{count} } @$genres; | |||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | return ucfirst($top->{name}) if $top; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | if ($tags && @$tags) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tags and @$tags
MC/DC0%0/2
$tags and @$tags
$tags
@$tags
Truth table0%0/3
A and B
A= $tagsB= @$tags
| |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | my ($top) = sort { $b->{count} <=> $a->{count} } @$tags; | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | ▶ | return ucfirst($top->{name}) if $top; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | sub _best_mb_release { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_best_mb_release: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | my ($releases) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | return unless @$releases; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my @dated = grep { $_->{date} } @$releases; | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | return @dated ? $dated[0] : $releases->[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | sub _mb_q { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mb_q: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | $s =~ s/["\\+\-&|!(){}\[\]^~*?:\/]/\\$&/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # AcoustID | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | ▶ | sub fpcalc_available { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fpcalc_available: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | return -x '/usr/bin/fpcalc' || -x '/usr/local/bin/fpcalc'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
-x '/usr/bin/fpcalc' || -x '/usr/local/bin/fpcalc'
MC/DC0%0/2
-x '/usr/bin/fpcalc' || -x '/usr/local/bin/fpcalc'
-x '/usr/bin/fpcalc'
-x '/usr/local/bin/fpcalc'
Truth table0%0/3
A || B
A= -x '/usr/bin/fpcalc'B= -x '/usr/local/bin/fpcalc'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | sub ffprobe_available { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ffprobe_available: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | return -x '/usr/bin/ffprobe' || -x '/usr/local/bin/ffprobe'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
-x '/usr/bin/ffprobe' || -x '/usr/local/bin/ffprobe'
MC/DC0%0/2
-x '/usr/bin/ffprobe' || -x '/usr/local/bin/ffprobe'
-x '/usr/bin/ffprobe'
-x '/usr/local/bin/ffprobe'
Truth table0%0/3
A || B
A= -x '/usr/bin/ffprobe'B= -x '/usr/local/bin/ffprobe'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | ▶ | sub flac_available { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flac_available: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | return eval { require Audio::FLAC::Header; 1 } // 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
require Audio::FLAC::Header;
1
}
} // 0
MC/DC0%0/1
eval {
do {
require Audio::FLAC::Header;
1
}
} // 0
eval {
do {
require Audio::FLAC::Header;
1
}
}
Truth table0%0/2
A
A= eval {
do {
require Audio::FLAC::Header;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # Read embedded Vorbis Comment tags + duration from a FLAC file on Drive. | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | # Downloads only the first 512 KB (enough for any FLAC header). | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # Returns a hashref of metadata fields, or undef on failure/no tags. | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | sub read_embedded_tags { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_embedded_tags: not called CC=19 SCAR=59.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my ($self, $drive_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | return unless flac_available(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | my $tmpfile = $self->_download_partial($drive_id, 512 * 1024); | |||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | return unless $tmpfile; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | my $flac = eval { Audio::FLAC::Header->new($tmpfile) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | unlink $tmpfile; | |||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | return unless $flac; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | ▶ | my $raw = $flac->tags() // {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$flac->tags // {}
MC/DC0%0/1
$flac->tags // {}
$flac->tags
Truth table0%0/2
A
A= $flac->tags
| |||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | my %tags = map { lc($_) => $raw->{$_} } keys %$raw; | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | my %meta; | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | $meta{title} = $tags{title} if $tags{title}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | $meta{artist} = $tags{artist} if $tags{artist}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | $meta{album} = $tags{album} if $tags{album}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | $meta{genre} = $tags{genre} if $tags{genre}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | $meta{comment} = $tags{comment} if $tags{comment}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | ▶ | if ($tags{tracknumber} && $tags{tracknumber} =~ /^(\d+)/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tags{"tracknumber"} and $tags{"tracknumber"} =~ /^(\d+)/
MC/DC0%0/2
$tags{"tracknumber"} and $tags{"tracknumber"} =~ /^(\d+)/
$tags{"tracknumber"}
$tags{"tracknumber"} =~ /^(\d+)/
Truth table0%0/3
A and B
A= $tags{"tracknumber"}B= $tags{"tracknumber"} =~ /^(\d+)/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | $meta{track_number} = $1 + 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | if ($tags{date} && $tags{date} =~ /^(\d{4})/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tags{"date"} and $tags{"date"} =~ /^(\d{4})/
MC/DC0%0/2
$tags{"date"} and $tags{"date"} =~ /^(\d{4})/
$tags{"date"}
$tags{"date"} =~ /^(\d{4})/
Truth table0%0/3
A and B
A= $tags{"date"}B= $tags{"date"} =~ /^(\d{4})/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $meta{year} = $1 + 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 368 | # Duration from FLAC stream info block | ||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | my $info = $flac->info(); | |||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | if ($info && $info->{TOTALSAMPLES} && $info->{SAMPLERATE}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/6
$info and $info->{"TOTALSAMPLES"} and $info->{"SAMPLERATE"}
$info and $info->{"TOTALSAMPLES"}
MC/DC0%0/3
$info and $info->{"TOTALSAMPLES"} and $info->{"SAMPLERATE"}
$info
$info->{"TOTALSAMPLES"}
$info->{"SAMPLERATE"}
Truth table0%0/4
A and B and C
A= $infoB= $info->{"TOTALSAMPLES"}C= $info->{"SAMPLERATE"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | $meta{duration_ms} = int($info->{TOTALSAMPLES} / $info->{SAMPLERATE} * 1000); | |||||||||||||||||||||||||||||||||||||||||||||
| 372 | } elsif ($flac->{trackTotalLengthSeconds}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | $meta{duration_ms} = int($flac->{trackTotalLengthSeconds} * 1000); | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | return %meta ? \%meta : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # Probe a Drive file for duration (milliseconds) using ffprobe. | ||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # Returns undef if ffprobe is unavailable or the probe fails. | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | sub probe_duration_ms { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
probe_duration_ms: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | my ($class, $drive_id, $token) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | return unless ffprobe_available(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | my $ffprobe = -x '/usr/bin/ffprobe' ? '/usr/bin/ffprobe' : 'ffprobe'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | my $url = sprintf($DRIVE_URL, $drive_id); | |||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | my $header = "Authorization: $token\r\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | my $out = q{}; | |||||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | my $pid = open(my $fh, '-|', $ffprobe, | |||||||||||||||||||||||||||||||||||||||||||||
| 391 | '-v', 'error', | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | '-headers', $header, | ||||||||||||||||||||||||||||||||||||||||||||||
| 393 | '-show_entries', 'format=duration', | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | '-of', 'default=noprint_wrappers=1:nokey=1', | ||||||||||||||||||||||||||||||||||||||||||||||
| 395 | $url, | ||||||||||||||||||||||||||||||||||||||||||||||
| 396 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | ▶ | if ($pid) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | $out = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | ▶ | return unless $out =~ /^(\d+(?:\.\d+)?)/m; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | return int($1 * 1000); | |||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub _download_partial { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_download_partial: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | my ($self, $drive_id, $max_bytes) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my $token = $self->token_fn->(); | |||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | unless ($token) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | $log->warn("Fingerprint: no bearer token available") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | my $url = sprintf $DRIVE_URL, uri_escape_utf8($drive_id); | |||||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | my $max = ($max_bytes // ($DOWNLOAD_MB * 1024 * 1024)) - 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$max_bytes // $DOWNLOAD_MB * 1024 * 1024
MC/DC0%0/1
$max_bytes // $DOWNLOAD_MB * 1024 * 1024
$max_bytes
Truth table0%0/2
A
A= $max_bytes
| |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | my $ua = HTTP::Tiny->new(agent => $USER_AGENT, timeout => 30); | |||||||||||||||||||||||||||||||||||||||||||||
| 416 | 0 | my ($fh, $tmpfile) = tempfile(SUFFIX => '.audio', UNLINK => 0); | |||||||||||||||||||||||||||||||||||||||||||||
| 417 | 0 | binmode $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||
| 419 | my $res = $ua->request('GET', $url, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 420 | headers => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 421 | Authorization => $token, | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | Range => "bytes=0-$max", | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | ▶ | data_callback => sub { print {$fh} $_[0] }, | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | if ($res->{success} || $res->{status} == 206) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->{"success"} or $res->{"status"} == 206
MC/DC0%0/2
$res->{"success"} or $res->{"status"} == 206
$res->{"success"}
$res->{"status"} == 206
Truth table0%0/3
A or B
A= $res->{"success"}B= $res->{"status"} == 206
| |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | $log->debug("Fingerprint: downloaded " . (-s $tmpfile) . " bytes for drive_id=$drive_id") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | return $tmpfile; | |||||||||||||||||||||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | $log->warn("Fingerprint: HTTP $res->{status} downloading drive_id=$drive_id: $res->{reason}") if $log; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | unlink $tmpfile; | |||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | ▶ | sub _fingerprint { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fingerprint: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | my ($tmpfile) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | my $fpcalc = -x '/usr/bin/fpcalc' ? '/usr/bin/fpcalc' : 'fpcalc'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | my $json = qx($fpcalc -json -length 120 \Q$tmpfile\E 2>/dev/null); | |||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | ▶ | return unless $json; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | my $data = eval { decode_json($json) } or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | return unless $data->{fingerprint} && $data->{duration}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data->{"fingerprint"} and $data->{"duration"}
MC/DC0%0/2
$data->{"fingerprint"} and $data->{"duration"}
$data->{"fingerprint"}
$data->{"duration"}
Truth table0%0/3
A and B
A= $data->{"fingerprint"}B= $data->{"duration"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | return { fingerprint => $data->{fingerprint}, duration => int($data->{duration}) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | sub _query_acoustid { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_query_acoustid: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | my ($self, $fp) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 450 | my $url = $AID_BASE | ||||||||||||||||||||||||||||||||||||||||||||||
| 451 | . '?client=' . uri_escape_utf8($self->acoustid_key) | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | . '&meta=recordings+compress' | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | . '&duration=' . $fp->{duration} | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | . '&fingerprint=' . uri_escape_utf8($fp->{fingerprint}); | |||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | ▶ | my $data = $self->_get_plain($url) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | ▶ | my $results = $data->{results} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | ▶ | return unless @$results; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # Pick the result with the highest score | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | my ($best) = sort { $b->{score} <=> $a->{score} } @$results; | |||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | ▶ | return unless $best->{score} && $best->{score} > 0.5; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$best->{"score"} and $best->{"score"} > "0.5"
MC/DC0%0/2
$best->{"score"} and $best->{"score"} > "0.5"
$best->{"score"}
$best->{"score"} > "0.5"
Truth table0%0/3
A and B
A= $best->{"score"}B= $best->{"score"} > "0.5"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | ▶ | my $recordings = $best->{recordings} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | return unless @$recordings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | return $recordings->[0]{id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||
| 469 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # HTTP helpers | ||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | sub _get_plain { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_plain: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | my $ua = HTTP::Tiny->new(agent => $USER_AGENT, timeout => 5); | |||||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | my $res = $ua->get($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | return unless $res->{success}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | return eval { decode_json($res->{content}) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | sub _get_mb { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_mb: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | my $gap = $MB_MIN_GAP - (time() - $last_mb_req); | |||||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | $self->_yield_sleep($gap) if $gap > 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 0 | $last_mb_req = time(); | |||||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | return $self->_get_plain($url); | |||||||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | sub _yield_sleep { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_yield_sleep: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | my ($self, $secs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | my $yield = $self->yield; | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | if ($yield) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | my $end = time() + $secs; | |||||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | ▶ | while (time() < $end) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | $yield->(); | |||||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | usleep(50_000); | |||||||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 0 | sleep($secs); | |||||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 505 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||