line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MusicBrainz::Collection; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
22969
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
69
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1553
|
use Audio::Scan; |
|
2
|
|
|
|
|
3646
|
|
|
2
|
|
|
|
|
55
|
|
6
|
2
|
|
|
2
|
|
1509
|
use File::Next; |
|
2
|
|
|
|
|
4197
|
|
|
2
|
|
|
|
|
59
|
|
7
|
2
|
|
|
2
|
|
119351
|
use HTTP::Request::Common qw(POST); |
|
2
|
|
|
|
|
787208
|
|
|
2
|
|
|
|
|
176
|
|
8
|
2
|
|
|
2
|
|
111931
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
156724
|
|
|
2
|
|
|
|
|
1253
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
1
|
|
|
1
|
1
|
210
|
my ( $class, %opts ) = @_; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
50
|
|
|
20
|
my $self = { |
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
16
|
|
|
|
|
|
|
user => $opts{user} || '', |
17
|
|
|
|
|
|
|
pass => $opts{pass} || '', |
18
|
|
|
|
|
|
|
albums => {}, |
19
|
|
|
|
|
|
|
verbose => $opts{verbose} || 0, |
20
|
|
|
|
|
|
|
}; |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
|
|
3
|
bless $self, $class; |
23
|
|
|
|
|
|
|
|
24
|
1
|
50
|
|
|
|
7
|
if ( !$self->{user} ) { |
25
|
0
|
|
|
|
|
0
|
$self->_load_auth; |
26
|
|
|
|
|
|
|
|
27
|
0
|
0
|
0
|
|
|
0
|
unless ( $self->{user} && $self->{pass} ) { |
28
|
0
|
|
|
|
|
0
|
die "No login information found\n"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
4
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub process { |
36
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $dir ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
0
|
if ( my $albums = $self->_find_albums($dir) ) { |
39
|
0
|
|
|
|
|
0
|
print "Uploading " . scalar( @{$albums} ) . " albums to collection...\n"; |
|
0
|
|
|
|
|
0
|
|
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
0
|
my $ua = LWP::UserAgent->new; |
42
|
0
|
|
|
|
|
0
|
$ua->credentials( |
43
|
|
|
|
|
|
|
'musicbrainz.org:80', |
44
|
|
|
|
|
|
|
'musicbrainz.org', |
45
|
|
|
|
|
|
|
$self->{user} => $self->{pass} |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
0
|
my $req = POST "http://musicbrainz.org/ws/1/collection/", [ |
49
|
0
|
|
|
|
|
0
|
addAlbums => join( ',', @{$albums} ), |
50
|
|
|
|
|
|
|
]; |
51
|
|
|
|
|
|
|
|
52
|
0
|
0
|
|
|
|
0
|
$self->{verbose} && print $req->as_string; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
0
|
my $response = $ua->request($req); |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
0
|
if ( $response->is_success ) { |
57
|
0
|
|
|
|
|
0
|
print "Done!\n"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
0
|
print "Error: " . $response->status_line . "\n"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
else { |
64
|
0
|
|
|
|
|
0
|
print "No album ID tags found\n"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _load_auth { |
69
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
0
|
require File::Spec; |
72
|
0
|
|
|
|
|
0
|
my $file = File::Spec->catfile( $ENV{HOME}, '.musicbrainz' ); |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
0
|
if ( -e $file ) { |
75
|
0
|
0
|
|
|
|
0
|
open my $fh, '<', $file or die "Unable to read .musicbrainz file: $!\n"; |
76
|
0
|
|
|
|
|
0
|
my $prefs = do { local $/; <$fh> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
77
|
0
|
|
|
|
|
0
|
close $fh; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
0
|
($self->{user}) = $prefs =~ m/user\s+(.+)/; |
80
|
0
|
|
|
|
|
0
|
($self->{pass}) = $prefs =~ m/pass\s+(.+)/; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _find_albums { |
85
|
1
|
|
|
1
|
|
21
|
my ( $self, $dir ) = @_; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $iter = File::Next::files( { |
88
|
5
|
|
|
5
|
|
389
|
file_filter => sub { Audio::Scan->is_supported( $File::Next::name ) }, |
89
|
1
|
|
|
|
|
10
|
}, $dir ); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Speed up scanning a bit by ignoring artwork |
92
|
1
|
|
|
|
|
115
|
local $ENV{AUDIO_SCAN_NO_ARTWORK} = 1; |
93
|
|
|
|
|
|
|
|
94
|
1
|
|
|
|
|
4
|
while ( defined ( my $file = $iter->() ) ) { |
95
|
5
|
50
|
|
|
|
46
|
$self->{verbose} && print "$file\n"; |
96
|
|
|
|
|
|
|
|
97
|
5
|
|
|
|
|
17
|
my $s = Audio::Scan->scan_tags($file); |
98
|
5
|
|
|
|
|
7834
|
my $tags = $s->{tags}; |
99
|
|
|
|
|
|
|
|
100
|
5
|
|
66
|
|
|
43
|
my $albumid |
101
|
|
|
|
|
|
|
= $tags->{'MusicBrainz Album Id'} |
102
|
|
|
|
|
|
|
|| $tags->{'MUSICBRAINZ ALBUM ID'} |
103
|
|
|
|
|
|
|
|| $tags->{'MUSICBRAINZ_ALBUMID'} |
104
|
|
|
|
|
|
|
|| $tags->{'MusicBrainz/Album Id'}; |
105
|
|
|
|
|
|
|
|
106
|
5
|
50
|
|
|
|
12
|
if ( $albumid ) { |
107
|
5
|
|
|
|
|
72
|
$self->{albums}->{$albumid} = 1; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
1
|
|
|
|
|
6
|
return [ keys %{ $self->{albums} } ]; |
|
1
|
|
|
|
|
18
|
|
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
__END__ |