| 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"; |
| 16 | use MusicBrainz::DiscID; |
| 17 | use WebService::MusicBrainz::Release; |
| 18 | use WebService::MusicBrainz::Artist; |
| 19 | use WebService::MusicBrainz::Response::Track; |
| 20 | use WebService::MusicBrainz::Response::TrackList; |
| 21 | use Getopt::Long; |
| 22 | |
| 23 | my $FRAMES_PER_SEC = 75; |
| 24 | |
| 25 | my ($device, $command, $workdir); |
| 26 | Getopt::Long::Configure ('no_ignore_case'); |
| 27 | Getopt::Long::Configure ('no_auto_abbrev'); |
| 28 | GetOptions ("device=s" => \$device, |
| 29 | "command=s" => \$command, |
| 30 | "workdir=s" => \$workdir); |
| 31 | |
| 32 | if (!defined($device)) { |
| 33 | $device = "/dev/cdrom"; |
| 34 | } |
| 35 | if (!defined($command)) { |
| 36 | $command = "id"; |
| 37 | } |
| 38 | if (!defined($workdir)) { |
| 39 | $workdir = "/tmp"; |
| 40 | } |
| 41 | |
| 42 | my $disc = new MusicBrainz::DiscID($device); |
| 43 | |
| 44 | # read the disc in the default disc drive */ |
| 45 | if ( $disc->read() == 0 ) { |
| 46 | printf STDERR "Error: %s\n", $disc->error_msg(); |
| 47 | exit(1); |
| 48 | } |
| 49 | |
| 50 | if ($command =~ m/id/) { |
| 51 | |
| 52 | printf("%s ", $disc->id()); |
| 53 | printf("%d ", $disc->last_track_num() + 1 - $disc->first_track_num()); |
| 54 | |
| 55 | for ( my $i = $disc->first_track_num; |
| 56 | $i <= $disc->last_track_num; $i++ ) { |
| 57 | printf("%d ", $disc->track_offset($i)); |
| 58 | } |
| 59 | printf("%d\n", $disc->sectors() / $FRAMES_PER_SEC); |
| 60 | |
| 61 | } elsif ($command =~ m/data/) { |
| 62 | my $ws = WebService::MusicBrainz::Release->new(); |
| 63 | my $response = $ws->search({ DISCID => $disc->id()}); |
| 64 | my @releases = $response->release_list(); |
| 65 | my $releasenum = 0; |
| 66 | |
| 67 | foreach my $release (@releases) { |
| 68 | my $a_artist = $release->artist()->name(); |
| 69 | my $va = 0; |
| 70 | if ($a_artist =~ /Various Artists/) { |
| 71 | $va = 1; |
| 72 | } |
| 73 | $releasenum++; |
| 74 | open (OUT, "> $workdir/cddbread.$releasenum"); |
| 75 | binmode OUT, ":utf8"; |
| 76 | print OUT "# xmcd style database file\n"; |
| 77 | print OUT "#\n"; |
| 78 | print OUT "# Track frame offsets:\n"; |
| 79 | for ( my $i = $disc->first_track_num; |
| 80 | $i <= $disc->last_track_num; $i++ ) { |
| 81 | print OUT "# " . $disc->track_offset($i) . "\n"; |
| 82 | } |
| 83 | print OUT "#\n"; |
| 84 | printf OUT "# Disc length: %d seconds\n", $disc->sectors() / $FRAMES_PER_SEC; |
| 85 | print OUT "#\n"; |
| 86 | print OUT "# Submitted via: XXXXXX\n"; |
| 87 | print OUT "#\n"; |
| 88 | print OUT "#blues,classical,country,data,folk,jazz,newage,reggae,rock,soundtrack,misc\n"; |
| 89 | print OUT "#CATEGORY=none\n"; |
| 90 | print OUT "DISCID=" . $disc->id() . "\n"; |
| 91 | print OUT "DTITLE=" . $a_artist. " / " . $release->title() . "\n"; |
| 92 | print OUT "DYEAR=\n"; |
| 93 | print OUT "DGENRE=\n"; |
| 94 | |
| 95 | my @tracks = @{$release->track_list()->tracks()}; |
| 96 | for (my $i = 0; $i < scalar(@tracks); $i++) { |
| 97 | my $track = $tracks[$i]; |
| 98 | my $t_name = $track->title; |
| 99 | if ($va) { |
| 100 | my $t_artist = $track->artist->name; |
| 101 | printf OUT "TTITLE%d=%s / %s\n", $i, $t_artist, $t_name; |
| 102 | } else { |
| 103 | printf OUT "TTITLE%d=%s\n", $i, $t_name; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | print OUT "EXTD=\n"; |
| 108 | for (my $i = 0; $i < scalar(@tracks); $i++) { |
| 109 | printf OUT "EXTT%d=\n", $i; |
| 110 | } |
| 111 | print OUT "PLAYORDER=\n"; |
| 112 | print OUT ".\n"; |
| 113 | close OUT; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | undef $disc; |