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