#!/usr/bin/python
#Audio Tools, a module and set of tools for manipulating audio data
#Copyright (C) 2007-2009 Brian Langenberger
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import audiotools
import audiotools.cdio as cdio
import gettext
gettext.install("audiotools",unicode=True)
if (__name__ == '__main__'):
parser = audiotools.OptionParser(
usage=_(u"%prog [-x filename] [--cdrom CDROM device]"),
version="Python Audio Tools %s" % (audiotools.VERSION))
parser.add_option('-V','--verbose',
action='store',
dest='verbosity',
choices=["quiet","normal","debug"],
default="normal",
help=_(u'the verbosity level to execute at'))
parser.add_option('-x','--xmcd',action='store',
type='string',dest='xmcd',default='-',
metavar='FILENAME',
help=_(u'FreeDB XMCD file or MusicBrainz XML output file'))
parser.add_option('-c','--cdrom',action='store',
type='string',dest='cdrom',
default=audiotools.DEFAULT_CDROM)
server = audiotools.OptionGroup(parser,_(u"Server Options"))
server.add_option('--musicbrainz-server',action='store',
type='string',dest='musicbrainz_server',
default=audiotools.MUSICBRAINZ_SERVER,
metavar='HOSTNAME')
server.add_option('--musicbrainz-port',action='store',
type='int',dest='musicbrainz_port',
default=audiotools.MUSICBRAINZ_PORT,
metavar='PORT')
server.add_option('--freedb-server',action='store',
type='string',dest='freedb_server',
default=audiotools.FREEDB_SERVER,
metavar='HOSTNAME')
server.add_option('--freedb-port',action='store',
type='int',dest='freedb_port',
default=audiotools.FREEDB_PORT,
metavar='PORT')
server.add_option('--no-musicbrainz',action='store_false',
dest='use_musicbrainz',default=True,
help='do not query MusicBrainz for metadata')
server.add_option('--no-freedb',action='store_false',
dest='use_freedb',default=True,
help='do not query FreeDB for metadata')
server.add_option('-D','--default',
action='store_const',const=1,default=None,
help=_(u'when multiple choices are available, select the first one automatically'))
parser.add_option_group(server)
(options,args) = parser.parse_args()
msg = audiotools.Messenger("cd2xmcd",options)
try:
if (options.xmcd == '-'):
outfile = sys.stdout
else:
outfile = open(options.xmcd,"w")
except IOError,err:
msg.error(_(u"Unable to write \"%s\"") % (msg.filename(options.xmcd)))
sys.exit(1)
cdda = audiotools.CDDA(options.cdrom)
if (len(cdda) == 255):
msg.error(_(u"No CD in drive"))
sys.exit(1)
tracks = list(cdda)
musicbrainz_discid = audiotools.MBDiscID.from_cdda(cdda)
freedb_discid = audiotools.DiscID.from_cdda(cdda)
msg.info(_(u"Found CD information"))
returnval = 0
musicbrainz_matches = 0
freedb_matches = 0
if (options.use_musicbrainz):
try:
musicbrainz_matches = audiotools.get_mbxml(
musicbrainz_discid,
outfile,
options.musicbrainz_server,
options.musicbrainz_port,
msg,
options.default)
returnval = 0
except IOError,e:
msg.error(unicode(e))
returnval = 1
if ((musicbrainz_matches == 0) and options.use_freedb):
try:
freedb_matches = audiotools.get_xmcd(
freedb_discid,
outfile,
options.freedb_server,
options.freedb_port,
msg,
options.default)
returnval = 0
except IOError,e:
msg.error(unicode(e))
returnval = 1
#if musicbrainz_matches and freedb_matches are both 0
#output a MusicBrainzReleaseXML file instead of a FreeDB one
#since XML is better for editing by end users
#(none of that " / "-handling nonsense)
if ((musicbrainz_matches == 0) and (freedb_matches == 0)):
if (options.use_musicbrainz):
musicbrainz_discid.toxml(outfile)
else:
freedb_discid.toxmcd(outfile)
msg.info(_(u"%s written") % (msg.filename(options.xmcd)))
outfile.close()
if (returnval != 0):
sys.exit(1)