Modified changelog
[clinton/abcde.git] / examples / musicbrainz-get-tracks
CommitLineData
22b54460 1#!/usr/bin/python
2
3# Copyright 2005 Decklin Foster, licensed under the same terms as abcde.
4
5import sys
6import getopt
7import musicbrainz
8import musicbrainz.queries as mq
9
10DEF_CD_DEV = '/dev/cdrom'
11
12# TODO:
13#
14# * --cdrom should take an argument, e.g. /dev/cdrom1. musicbrainz.queries
15# doesn't seem to provide for this.
16
17def get_toc_discid(mb):
18 mb.Query(mq.MBQ_GetCDTOC)
19 return mb.GetResultData(mq.MBE_TOCGetCDIndexId)
20
21def lookup_discid(mb, id):
22 mb.QueryWithArgs(mq.MBQ_GetCDInfoFromCDIndexId, [id])
23
24if __name__ == "__main__":
25 shortopts = 'cd:'
26 longopts = ['cdrom', 'discid=']
27
28 mb = musicbrainz.mb()
29 mb.SetDepth(2)
30
31 opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
32
33 id = None
34 if opts:
35 for opt, arg in opts:
36 if opt in ('--cdrom', '-c'):
37 id = get_toc_discid(mb)
38 lookup_discid(mb, id)
39 if opt in ('--discid', '-d'):
40 id = arg
41 lookup_discid(mb, id)
42 else:
43 id = get_toc_discid(mb)
44 lookup_discid(mb, id)
45
46 matches = mb.GetResultInt(mq.MBE_GetNumAlbums)
47 if matches == 0:
48 print >>sys.stderr, "could not find an entry for this CD"
49 sys.exit(1)
50 if matches > 1:
51 print >>sys.stderr, "warning: multiple matches, using first"
52
53 mb.Select1(mq.MBS_SelectAlbum, 1)
54 album = mb.GetResultData(mq.MBE_AlbumGetAlbumName)
55 n = mb.GetResultInt(mq.MBE_AlbumGetNumTracks)
56
57 artistid = mb.GetIDFromURL(mb.GetResultData(mq.MBE_AlbumGetAlbumArtistId))
58 if artistid == mq.MBI_VARIOUS_ARTIST_ID:
59 artist = 'Various Artists'
60 else:
61 try:
62 artist = mb.GetResultData1(mq.MBE_AlbumGetArtistName, 1)
63 except musicbrainz.MusicBrainzError:
64 artist = 'Unknown Artist'
65
66 if args:
67 tracks = map(int, args)
68 else:
69 tracks = range(1, n+1)
70
71 trackinfo = []
72 for i in tracks:
73 if artistid == mq.MBI_VARIOUS_ARTIST_ID:
74 try:
75 tartist = mb.GetResultData1(mq.MBE_AlbumGetArtistName, i)
76 except musicbrainz.MusicBrainzError:
77 tartist = 'Unknown Artist'
78 else:
79 tartist = None
80 try:
81 ttitle = mb.GetResultData1(mq.MBE_AlbumGetTrackName, i)
82 except musicbrainz.MusicBrainzError:
83 tartist = 'Unknown Track'
84 try:
85 tlength = mb.GetResultInt1(mq.MBE_AlbumGetTrackDuration, i)
86 except musicbrainz.MusicBrainzError:
87 tlength = 0
88 trackinfo.append((tartist, ttitle, tlength))
89
90 print "# fake CD database file generated by musicbrainz-get-tracks 0.1"
91 print "#"
92 print "# Track frame offsets:"
93
94 # Assume standard pregap
95 total_len = 2000
96 for t in trackinfo:
97 tartist, ttitle, tlength = t
98 print "# %d" % (total_len / (1000.0/75.0))
99 total_len += tlength
100
101 print "#"
102 print "# Disc length: %d seconds" % (total_len / 1000)
103 print "#"
104 print "# Revision: 0"
105 print "# Processed by: MusicBrainz"
106 print "# Submitted by: MusicBrainz"
107 print "DISCID=%s" % id
108 print "DTITLE=%s / %s" % (artist, album)
109
110 for i in range(0, len(trackinfo)):
111 tartist, ttitle, tlength = trackinfo[i]
112 if tartist:
113 print "TTITLE%d=%s / %s" % (i, tartist, ttitle)
114 else:
115 print "TTITLE%d=%s" % (i, ttitle)
116
117 print "EXTD="
118
119 for i in range(0, len(trackinfo)):
120 print "EXTD%d=" % i
121
122 print "PLAYORDER="
123 print "."