Commit | Line | Data |
---|---|---|
2c2dffe6 E |
1 | #!/usr/bin/perl |
2 | # Copyright (c) 2012 Steve McIntyre <93sam@debian.org> | |
3 | # This code is hereby licensed for public consumption under either the | |
4 | # GNU GPL v2 or greater, or Larry Wall's Artistic license - your choice. | |
5 | # | |
6 | # You should have received a copy of the GNU General Public License along | |
7 | # with this program; if not, write to the Free Software Foundation, Inc., | |
8 | # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
9 | # | |
10 | # abcde-musicbrainz-tool | |
11 | # | |
12 | # Helper script for abcde to work with the MusicBrainz WS API (v2) | |
13 | ||
14 | use strict; | |
15 | use encoding "utf8"; | |
4942842f E |
16 | use POSIX qw(ceil); |
17 | use Digest::SHA; | |
2c2dffe6 E |
18 | use MusicBrainz::DiscID; |
19 | use WebService::MusicBrainz::Release; | |
20 | use WebService::MusicBrainz::Artist; | |
21 | use WebService::MusicBrainz::Response::Track; | |
22 | use WebService::MusicBrainz::Response::TrackList; | |
23 | use Getopt::Long; | |
24 | ||
25 | my $FRAMES_PER_SEC = 75; | |
26 | ||
4942842f | 27 | my ($device, $command, $discid, @discinfo, $workdir); |
2c2dffe6 E |
28 | Getopt::Long::Configure ('no_ignore_case'); |
29 | Getopt::Long::Configure ('no_auto_abbrev'); | |
4942842f E |
30 | GetOptions ("device=s" => \$device, |
31 | "command=s" => \$command, | |
32 | "discid=s" => \$discid, | |
33 | "discinfo=i{5,}" => \@discinfo, | |
34 | "workdir=s" => \$workdir); | |
2c2dffe6 E |
35 | |
36 | if (!defined($device)) { | |
37 | $device = "/dev/cdrom"; | |
38 | } | |
39 | if (!defined($command)) { | |
40 | $command = "id"; | |
41 | } | |
42 | if (!defined($workdir)) { | |
43 | $workdir = "/tmp"; | |
44 | } | |
45 | ||
efe23140 E |
46 | sub calc_sha1($) { |
47 | my $filename = shift; | |
48 | my $s = Digest::SHA->new(1); | |
49 | $s->addfile($filename); | |
50 | return $s->hexdigest; | |
51 | } | |
2c2dffe6 | 52 | |
4942842f E |
53 | if ($command =~ m/^id/) { |
54 | my $disc = new MusicBrainz::DiscID($device); | |
2c2dffe6 | 55 | |
4942842f E |
56 | # read the disc in the default disc drive */ |
57 | if ( $disc->read() == 0 ) { | |
58 | printf STDERR "Error: %s\n", $disc->error_msg(); | |
59 | exit(1); | |
60 | } | |
2c2dffe6 E |
61 | |
62 | printf("%s ", $disc->id()); | |
63 | printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num()); | |
64 | ||
65 | for ( my $i = $disc->first_track_num; | |
66 | $i <= $disc->last_track_num; $i++ ) { | |
67 | printf("%d ", $disc->track_offset($i)); | |
68 | } | |
69 | printf("%d\n", $disc->sectors() / $FRAMES_PER_SEC); | |
4942842f | 70 | undef $disc; |
2c2dffe6 E |
71 | |
72 | } elsif ($command =~ m/data/) { | |
73 | my $ws = WebService::MusicBrainz::Release->new(); | |
4942842f | 74 | my $response = $ws->search({ DISCID => $discid }); |
2c2dffe6 E |
75 | my @releases = $response->release_list(); |
76 | my $releasenum = 0; | |
efe23140 | 77 | my @sums; |
2c2dffe6 E |
78 | |
79 | foreach my $release (@releases) { | |
faf17565 | 80 | #print Dumper( $release->release_event_list() ); |
2c2dffe6 | 81 | my $a_artist = $release->artist()->name(); |
faf17565 | 82 | my $a_date = substr (@{$release->release_event_list()->events()}[0]->date(), 0, 4); |
2c2dffe6 E |
83 | my $va = 0; |
84 | if ($a_artist =~ /Various Artists/) { | |
85 | $va = 1; | |
86 | } | |
87 | $releasenum++; | |
88 | open (OUT, "> $workdir/cddbread.$releasenum"); | |
89 | binmode OUT, ":utf8"; | |
90 | print OUT "# xmcd style database file\n"; | |
91 | print OUT "#\n"; | |
92 | print OUT "# Track frame offsets:\n"; | |
4942842f E |
93 | # Assume standard pregap |
94 | my $total_len = 2000; | |
95 | my @tracks = @{$release->track_list()->tracks()}; | |
96 | for (my $i = 0; $i < scalar(@tracks); $i++) { | |
97 | printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_SEC / 1000.0); | |
98 | $total_len += $tracks[$i]->duration(); | |
2c2dffe6 E |
99 | } |
100 | print OUT "#\n"; | |
4942842f | 101 | printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0; |
2c2dffe6 E |
102 | print OUT "#\n"; |
103 | print OUT "# Submitted via: XXXXXX\n"; | |
104 | print OUT "#\n"; | |
105 | print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n"; | |
106 | print OUT "#CATEGORY=none\n"; | |
4942842f | 107 | print OUT "DISCID=" . $discid . "\n"; |
2c2dffe6 | 108 | print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n"; |
faf17565 | 109 | print OUT "DYEAR=" . $a_date . "\n"; |
2c2dffe6 E |
110 | print OUT "DGENRE=\n"; |
111 | ||
112 | my @tracks = @{$release->track_list()->tracks()}; | |
113 | for (my $i = 0; $i < scalar(@tracks); $i++) { | |
114 | my $track = $tracks[$i]; | |
115 | my $t_name = $track->title; | |
116 | if ($va) { | |
117 | my $t_artist = $track->artist->name; | |
118 | printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name; | |
119 | } else { | |
120 | printf OUT "TTITLE%d=%s\n", $i, $t_name; | |
121 | } | |
122 | } | |
123 | ||
217b25e3 CE |
124 | print OUT "EXTD=barcode=" . @{$release->release_event_list()->events()}[0]->barcode() |
125 | . " catalognumber=" . @{$release->release_event_list()->events()}[0]->catalog_number() . "\n"; | |
2c2dffe6 E |
126 | for (my $i = 0; $i < scalar(@tracks); $i++) { |
127 | printf OUT "EXTT%d=\n", $i; | |
128 | } | |
129 | print OUT "PLAYORDER=\n"; | |
130 | print OUT ".\n"; | |
131 | close OUT; | |
efe23140 E |
132 | |
133 | # Check to see that this entry is unique; generate a checksum | |
134 | # and compare to any previous checksums | |
135 | my $checksum = calc_sha1("$workdir/cddbread.$releasenum"); | |
136 | foreach my $sum (@sums) { | |
137 | if ($checksum eq $sum) { | |
138 | unlink("$workdir/cddbread.$releasenum"); | |
139 | $releasenum--; | |
140 | last; | |
141 | } | |
142 | } | |
143 | push (@sums, $checksum); | |
2c2dffe6 | 144 | } |
4942842f E |
145 | } elsif ($command =~ m/calcid/) { |
146 | # Calculate MusicBrainz ID from disc offsets; see | |
147 | # http://musicbrainz.org/doc/DiscIDCalculation | |
148 | ||
149 | my ($first, $last, $leadin, $leadout, @offsets) = @discinfo; | |
150 | ||
151 | my $s = Digest::SHA->new(1); | |
152 | $s->add(sprintf "%02X", int($first)); | |
153 | $s->add(sprintf "%02X", int($last)); | |
154 | ||
155 | my @a; | |
156 | for (my $i = 0; $i < 100; $i++) { | |
157 | $a[$i] = 0; | |
158 | } | |
159 | my $i = 0; | |
160 | foreach my $o ($leadout, @offsets) { | |
161 | $a[$i++] = int($o) + int($leadin); | |
162 | } | |
163 | for (my $i = 0; $i < 100; $i++) { | |
164 | $s->add(sprintf "%08X", $a[$i]); | |
165 | } | |
166 | ||
167 | my $id = $s->b64digest; | |
168 | # CPAN Digest modules do not pad their Base64 output, so we have to do it. | |
169 | while (length($id) % 4) { | |
170 | $id .= '='; | |
171 | } | |
172 | ||
173 | $id =~ tr#+#.#; | |
174 | $id =~ tr#/#_#; | |
175 | $id =~ tr#=#-#; | |
176 | ||
177 | print $id; | |
2c2dffe6 E |
178 | } |
179 |