+"""
+
+import os, re, sys, tempfile, time, traceback
+from optparse import OptionParser
+import urllib
+
+import discid.disc
+import musicbrainzngs.musicbrainz
+
+from org.diplodocus.util import catch_EnvironmentError as c
+
+musicbrainzngs.musicbrainz.set_useragent(
+ 'flac-archive', '0.1', 'https://diplodocus.org/git/flac-archive')
+
+#import logging
+#logging.basicConfig(level=logging.DEBUG)
+
+# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=439790
+MSF_OFFSET = 150
+
+def mkcue(fp, disc, trackcount=None):
+ c(fp.write, 'FILE "dummy.wav" WAVE\n')
+
+ if trackcount == None:
+ trackcount = len(disc.tracks)
+ else:
+ trackcount = min(trackcount, len(disc.tracks))
+
+ for i in xrange(1, trackcount+1):
+ track = disc.tracks[i-1]
+ offset = track.offset - MSF_OFFSET
+
+ minutes = seconds = 0
+ sectors = offset % 75
+ if offset >= 75:
+ seconds = offset / 75
+ if seconds >= 60:
+ minutes = seconds / 60
+ seconds = seconds % 60
+
+ c(fp.write, ' TRACK %02d AUDIO\n' % (track.number,))
+ if i == 1 and offset > 0:
+ c(fp.write, ' INDEX 00 00:00:00\n')
+ c(fp.write,
+ ' INDEX 01 %02d:%02d:%02d\n' % (minutes, seconds, sectors))
+
+ return trackcount
+
+def tags_file(fn, trackcount, various, artist=None, album=None,
+ release_dates={}, tracks=[]):
+ fp = c(file, fn, 'w')
+ if various:
+ c(fp.write, 'ALBUMARTIST=')
+ else:
+ c(fp.write, 'ARTIST=')
+ if artist != None:
+ c(fp.write, artist.encode('utf-8'))
+ c(fp.write, '\nALBUM=')
+ if album != None:
+ c(fp.write, album.encode('utf-8'))
+ c(fp.write, '\n')
+
+ have_date = False
+ for (country, date) in release_dates.items():
+ have_date = True
+ c(fp.write, 'DATE[%s]=%s\n' % (country, date))
+ have_date or c(fp.write, 'DATE=\n')
+
+ if len(tracks) > 0:
+ trackcount = min(trackcount, len(tracks))
+ for i in xrange(1, trackcount + 1):
+ artist = title = ''
+ if len(tracks) > 0:
+ track = tracks.pop(0)
+ title = track.title
+ if track.artist:
+ artist = track.artist.name
+ various and c(fp.write, 'ARTIST[%d]=%s\n' % (i,
+ artist.encode('utf-8')))
+ c(fp.write, 'TITLE[%d]=%s\n' % (i, title.encode('utf-8')))
+
+ c(fp.close)
+
+def cover_art(i, asin):
+ url = 'http://images.amazon.com/images/P/%s.01.MZZZZZZZ.jpg' % (asin,)
+ fp = file('cover.front-' + i, 'w')
+ fp.write(urllib.urlopen(url).read())
+ fp.close()
+
+def tags(releases, trackcount):
+ results = []
+ seen_asins = set()
+ seen_various = False
+
+ tags_file('candidate-tags-0', trackcount, False)
+
+ i = 0
+ for release in releases:
+ i += 1
+ various = not release.isSingleArtistRelease()
+
+ if various and not seen_various:
+ seen_various = True
+ tags_file('candidate-tags-0v', trackcount, various)
+
+ tags_file('candidate-tags-' + str(i), trackcount, various,
+ release.artist.name, release.title,
+ release.getReleaseEventsAsDict(),
+ release.tracks)
+ if release.asin:
+ # See also:
+ # for i in release.getRelations(): print i.type
+ # http://musicbrainz.org/ns/rel-1.0#Wikipedia
+ # ...
+ # http://musicbrainz.org/ns/rel-1.0#AmazonAsin
+ asin = release.asin
+ if asin not in seen_asins:
+ seen_asins.add(asin)
+ cover_art(str(i), asin)
+
+def rip(device, trackcount, single_file):
+ if device == None:
+ device = '/dev/cdrom'
+
+ argv = ['cdparanoia', '-d', device, '1-' + str(trackcount)]
+
+ if single_file:
+ argv.append('wav')
+ else:
+ argv.append('-B')
+
+ c(os.execvp, argv[0], argv)
+
+def make_post_processor(command):
+ if command == None:
+ return
+
+ fd = c(os.open, 'post-processor', os.O_CREAT | os.O_WRONLY, 0555)
+ fp = c(os.fdopen, fd, 'w')
+ c(fp.write, command +' "$@"\n')
+ c(fp.close)
+
+def get_releases(filter_, tries=5):
+ sleep = 1
+ query = musicbrainz2.webservice.Query()
+ while True:
+ try:
+ return query.getReleases(filter_)
+ except musicbrainz2.webservice.WebServiceError, e:
+ if '503' not in e.msg:
+ raise
+ tries -= 1
+ sys.stderr.write('getReleases: %s: ' % e)
+ if tries == 0:
+ sys.stderr.write('giving up\n')
+ raise
+ sleep *= 2
+ sys.stderr.write('sleeping %ds before retry...\n' % sleep)
+ time.sleep(sleep)
+
+def releases_by_disc(disc_id):
+ try:
+ musicbrainzngs.musicbrainz.get_releases_by_discid(disc_id)
+ except musicbrainzngs.musicbrainz.ResponseError:
+ return []
+ raise 'what now'
+
+def releases_by(q, title, artist=None):
+ filter_ = musicbrainz2.webservice.ReleaseFilter(title=title)
+ results = get_releases(filter_)
+ releases = (result.release for result in results)
+ if artist:
+ pattern = re.sub(r'\s+', r'\s+', artist.strip())
+ releases = (x for x in releases
+ if re.match(pattern, x.artist.name, re.IGNORECASE))
+ return releases
+
+def main(argv):
+ # Control the exit code for any uncaught exceptions.
+ try:
+ parser = OptionParser()
+ parser.disable_interspersed_args()
+ parser.add_option('--artist')
+ parser.add_option('--discid')
+ parser.add_option('--title')
+ parser.add_option('--print-discid', action='store_true', default=False)
+ parser.add_option('-d', '--device')
+ parser.add_option('-m', '--no-musicbrainz',
+ action='store_true', default=False)
+ parser.add_option('-p', '--post-processor')
+ parser.add_option('-s', '--single-file',
+ action='store_true', default=False)
+ parser.add_option('-t', '--tracks', type='int', default=99)
+ except:
+ traceback.print_exc()
+ return 2
+
+ try:
+ # Raises SystemExit on invalid options in argv.
+ (options, args) = parser.parse_args(argv[1:])
+ except Exception, error:
+ if isinstance(error, SystemExit):
+ return 1
+ traceback.print_exc()
+ return 2
+
+ try:
+ device = options.device
+ trackcount = options.tracks
+
+ tempdir = c((lambda x: tempfile.mkdtemp(prefix=x, dir='.')),
+ 'flac-archive.')
+ sys.stderr.write('ripping to %s\n\n' % (tempdir,))
+ c(os.chdir, tempdir)
+
+ make_post_processor(options.post_processor)
+
+ if options.title != None:
+ tags(releases_by(q, options.title, options.artist), trackcount)
+ elif options.discid != None:
+ tags(releases_by_disc(options.discid), trackcount)
+ else:
+ disc = discid.disc.read(device)
+ if options.print_discid:
+ print disc.id
+ return 0
+ fp = c(file, 'cue', 'w')
+ trackcount = mkcue(fp, disc, trackcount)
+ c(fp.close)
+ if options.no_musicbrainz:
+ releases = []
+ else:
+ releases = releases_by_disc(disc.id)
+ tags(releases, trackcount)
+ rip(device, trackcount, options.single_file)
+ except Exception, error:
+ if isinstance(error, SystemExit):
+ raise
+ # check all print_exc and format_exc in fa-flacd.py; i think
+ # for some i don't do this msg print check
+ sys.stderr.write(getattr(error, 'msg', ''))
+ traceback.print_exc()
+ return 2
+
+ return 0
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))