~ubuntu-branches/ubuntu/lucid/abcde/lucid

« back to all changes in this revision

Viewing changes to examples/musicbrainz-get-tracks

  • Committer: Bazaar Package Importer
  • Author(s): Jesus Climent
  • Date: 2006-02-07 23:48:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060207234800-vwmq94rs80xll3es
Tags: 2.3.99.5-1
* Repaired multiple CDDB entries when recursive search is active.
* Patch from A. Costa to clean some typos (Closes: #351774).

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 
10
10
DEF_CD_DEV = '/dev/cdrom'
11
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
 
 
17
 
def get_toc_discid(mb):
18
 
    mb.Query(mq.MBQ_GetCDTOC)
19
 
    return mb.GetResultData(mq.MBE_TOCGetCDIndexId)
 
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
20
 
21
21
def lookup_discid(mb, id):
22
22
    mb.QueryWithArgs(mq.MBQ_GetCDInfoFromCDIndexId, [id])
23
23
 
24
 
if __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
 
 
 
24
def fake_cddb(mb):
46
25
    matches = mb.GetResultInt(mq.MBE_GetNumAlbums)
47
26
    if matches == 0:
48
 
        print >>sys.stderr, "could not find an entry for this CD"
 
27
        print >>sys.stderr, "error: could not find an entry for this disc"
49
28
        sys.exit(1)
50
29
    if matches > 1:
51
30
        print >>sys.stderr, "warning: multiple matches, using first"
121
100
 
122
101
    print "PLAYORDER="
123
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