Adds YEAR to the OUTPUTFORMAT comments in sample abcde.conf.
[clinton/abcde.git] / abcde-musicbrainz-tool
CommitLineData
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
14use strict;
15use encoding "utf8";
4942842f
E
16use POSIX qw(ceil);
17use Digest::SHA;
2c2dffe6
E
18use MusicBrainz::DiscID;
19use WebService::MusicBrainz::Release;
20use WebService::MusicBrainz::Artist;
21use WebService::MusicBrainz::Response::Track;
22use WebService::MusicBrainz::Response::TrackList;
23use Getopt::Long;
24
25my $FRAMES_PER_SEC = 75;
26
4942842f 27my ($device, $command, $discid, @discinfo, $workdir);
2c2dffe6
E
28Getopt::Long::Configure ('no_ignore_case');
29Getopt::Long::Configure ('no_auto_abbrev');
4942842f
E
30GetOptions ("device=s" => \$device,
31 "command=s" => \$command,
32 "discid=s" => \$discid,
33 "discinfo=i{5,}" => \@discinfo,
34 "workdir=s" => \$workdir);
2c2dffe6
E
35
36if (!defined($device)) {
37 $device = "/dev/cdrom";
38}
39if (!defined($command)) {
40 $command = "id";
41}
42if (!defined($workdir)) {
43 $workdir = "/tmp";
44}
45
efe23140
E
46sub 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
53if ($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) {
80 my $a_artist = $release->artist()->name();
81 my $va = 0;
82 if ($a_artist =~ /Various Artists/) {
83 $va = 1;
84 }
85 $releasenum++;
86 open (OUT, "> $workdir/cddbread.$releasenum");
87 binmode OUT, ":utf8";
88 print OUT "# xmcd style database file\n";
89 print OUT "#\n";
90 print OUT "# Track frame offsets:\n";
4942842f
E
91 # Assume standard pregap
92 my $total_len = 2000;
93 my @tracks = @{$release->track_list()->tracks()};
94 for (my $i = 0; $i < scalar(@tracks); $i++) {
95 printf OUT "# %d\n", ceil($total_len * $FRAMES_PER_SEC / 1000.0);
96 $total_len += $tracks[$i]->duration();
2c2dffe6
E
97 }
98 print OUT "#\n";
4942842f 99 printf OUT "# Disc length: %d seconds\n", $total_len / 1000.0;
2c2dffe6
E
100 print OUT "#\n";
101 print OUT "# Submitted via: XXXXXX\n";
102 print OUT "#\n";
103 print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n";
104 print OUT "#CATEGORY=none\n";
4942842f 105 print OUT "DISCID=" . $discid . "\n";
2c2dffe6
E
106 print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n";
107 print OUT "DYEAR=\n";
108 print OUT "DGENRE=\n";
109
110 my @tracks = @{$release->track_list()->tracks()};
111 for (my $i = 0; $i < scalar(@tracks); $i++) {
112 my $track = $tracks[$i];
113 my $t_name = $track->title;
114 if ($va) {
115 my $t_artist = $track->artist->name;
116 printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name;
117 } else {
118 printf OUT "TTITLE%d=%s\n", $i, $t_name;
119 }
120 }
121
122 print OUT "EXTD=\n";
123 for (my $i = 0; $i < scalar(@tracks); $i++) {
124 printf OUT "EXTT%d=\n", $i;
125 }
126 print OUT "PLAYORDER=\n";
127 print OUT ".\n";
128 close OUT;
efe23140
E
129
130 # Check to see that this entry is unique; generate a checksum
131 # and compare to any previous checksums
132 my $checksum = calc_sha1("$workdir/cddbread.$releasenum");
133 foreach my $sum (@sums) {
134 if ($checksum eq $sum) {
135 unlink("$workdir/cddbread.$releasenum");
136 $releasenum--;
137 last;
138 }
139 }
140 push (@sums, $checksum);
2c2dffe6 141 }
4942842f
E
142} elsif ($command =~ m/calcid/) {
143# Calculate MusicBrainz ID from disc offsets; see
144# http://musicbrainz.org/doc/DiscIDCalculation
145
146 my ($first, $last, $leadin, $leadout, @offsets) = @discinfo;
147
148 my $s = Digest::SHA->new(1);
149 $s->add(sprintf "%02X", int($first));
150 $s->add(sprintf "%02X", int($last));
151
152 my @a;
153 for (my $i = 0; $i < 100; $i++) {
154 $a[$i] = 0;
155 }
156 my $i = 0;
157 foreach my $o ($leadout, @offsets) {
158 $a[$i++] = int($o) + int($leadin);
159 }
160 for (my $i = 0; $i < 100; $i++) {
161 $s->add(sprintf "%08X", $a[$i]);
162 }
163
164 my $id = $s->b64digest;
165 # CPAN Digest modules do not pad their Base64 output, so we have to do it.
166 while (length($id) % 4) {
167 $id .= '=';
168 }
169
170 $id =~ tr#+#.#;
171 $id =~ tr#/#_#;
172 $id =~ tr#=#-#;
173
174 print $id;
2c2dffe6
E
175}
176