1 #! /usr/bin/env python2.4
6 B<fa-rip> - rip a CD for B<fa-flacd>
10 B<fa-rip> [B<-d> I<device>] [B<-p> I<post-processor> [B<-t> I<track-count>]
14 B<fa-rip> creates a temporary directory for storage of its
15 intermediate files, uses MusicBrainz to create the "cue" file and
16 candidate tags files, and runs C<cdparanoia(1)> to rip the CD to the
19 In order for this CD to be processed by B<fa-flacd>, you must create a
20 "tags" file. This is usually done by renaming one of the
21 candidate-tags files and deleting the others. Don't forget to fill in
22 the DATE tag in the selected candidate before renaming it. If
23 B<fa-rip> could not find any tag information from MusicBrainz, you'll
24 have to fill out the candidate-tags-0 template.
30 =item B<-d> [B<--device>] I<device>
32 Use I<device> as the CD-ROM device, instead of the default
33 "/dev/cdrom" or the environment variable CDDEV.
35 =item B<-p> [B<--post-processor>] I<post-processor>
37 Create a "post-processor" file in the temporary directory containing
38 the line 'I<post-processor> "$@"'. See B<fa-flacd>'s man page for
39 information about this hook.
41 =item B<-t> [B<--tracks>] I<track-count>
43 Archive only the first I<track-count> tracks. This is handy for
54 B<fa-rip> uses this to rip audio and save the cuesheet for a CD.
55 MusicBrainz::Client can usually figure this out automatically.
61 Written by Eric Gillespie <epg@pretzelnet.org>.
63 flac-archive is free software; you may redistribute it and/or modify
64 it under the same terms as Perl itself.
68 ''' #' # python-mode is sucks
70 import os, re, sys, tempfile, traceback
71 from optparse import OptionParser
74 import musicbrainz2.disc
75 import musicbrainz2.webservice
77 from org.diplodocus.util import catch_EnvironmentError as c
79 def mkcue(disc, trackcount=None):
80 fp = c(file, 'cue', 'w')
81 c(fp.write, 'FILE "dummy.wav" WAVE\n')
82 c(fp.write, ' TRACK 01 AUDIO\n')
83 c(fp.write, ' INDEX 01 00:00:00\n')
85 if trackcount == None:
86 trackcount = disc.lastTrackNum
88 trackcount = min(trackcount, disc.lastTrackNum)
90 pregap = disc.tracks[0][0]
91 for i in xrange(disc.firstTrackNum, trackcount):
92 offset = disc.tracks[i][0]
100 minutes = seconds / 60
101 seconds = seconds % 60
103 c(fp.write, ' TRACK %02d AUDIO\n' % (i + 1,))
105 ' INDEX 01 %02d:%02d:%02d\n' % (minutes, seconds, sectors))
111 def tags_file(fn, trackcount, various, artist=None, album=None,
112 release_dates={}, tracks=[]):
113 fp = c(file, fn, 'w')
114 c(fp.write, 'ARTIST=')
117 c(fp.write, '\nALBUM=')
123 for (country, date) in release_dates.items():
125 c(fp.write, 'DATE[%s]=%s\n' % (country, date))
126 have_date or c(fp.write, 'DATE=\n')
129 trackcount = min(trackcount, len(tracks))
130 for i in xrange(1, trackcount + 1):
132 track = tracks.pop(0)
134 artist = track.artist
138 various and c(fp.write, 'ARTIST[%d]=%s\n' % (i, artist))
139 c(fp.write, 'TITLE[%d]=%s\n' % (i, title))
143 def cover_art(i, asin):
144 url = 'http://images.amazon.com/images/P/%s.01.MZZZZZZZ.jpg' % (asin,)
145 fp = file('cover.front-' + i, 'w')
146 fp.write(urllib.urlopen(url).read())
149 def tags(q, releases, trackcount):
153 tags_file('candidate-tags-0', trackcount, False)
155 include = musicbrainz2.webservice.ReleaseIncludes(tracks=True)
158 for album in releases:
160 various = not album.release.isSingleArtistRelease()
162 if various and not seen_various:
164 tags_file('candidate-tags-0v', trackcount, True)
166 tags_file('candidate-tags-' + str(i), trackcount, various,
167 album.release.artist.name, album.release.title,
168 album.release.getReleaseEventsAsDict(),
169 q.getReleaseById(album.release.id, include).tracks)
171 cover_art(str(i), album.release.asin)
173 def rip(device, trackcount, single_file):
175 device = '/dev/cdrom'
177 argv = ['cdparanoia', '-d', device, '1-' + str(trackcount)]
184 c(os.execvp, argv[0], argv)
186 def make_post_processor(command):
190 fd = c(os.open, 'post-processor', os.O_CREAT | os.O_WRONLY, 0555)
191 fp = c(os.fdopen, fd, 'w')
192 c(fp.write, command +' "$@"\n')
195 def releases_by_disc(q, disc):
196 filter = musicbrainz2.webservice.ReleaseFilter(discId=disc.getId())
197 return q.getReleases(filter)
199 def releases_by(q, title, artist=None):
200 r = q.getReleases(musicbrainz2.webservice.ReleaseFilter(title=title))
204 artist = re.sub(r'\s+', r'\s+', artist.strip())
205 return [x for x in r if re.match(artist, x.release.artist.name,
206 re.IGNORECASE) != None]
209 # Control the exit code for any uncaught exceptions.
211 parser = OptionParser()
212 parser.disable_interspersed_args()
213 parser.add_option('--artist')
214 parser.add_option('--title')
215 parser.add_option('-d', '--device')
216 parser.add_option('-m', '--no-musicbrainz',
217 action='store_true', default=False)
218 parser.add_option('-p', '--post-processor')
219 parser.add_option('-s', '--single-file',
220 action='store_true', default=False)
221 parser.add_option('-t', '--tracks', type='int', default=99)
223 traceback.print_exc()
227 # Raises SystemExit on invalid options in argv.
228 (options, args) = parser.parse_args(argv[1:])
229 except Exception, error:
230 if isinstance(error, SystemExit):
232 traceback.print_exc()
236 device = options.device
237 trackcount = options.tracks
239 tempdir = c((lambda x: tempfile.mkdtemp(prefix=x, dir='.')),
243 make_post_processor(options.post_processor)
245 q = musicbrainz2.webservice.Query()
246 if options.title != None:
247 releases = releases_by(q, options.title, options.artist)
249 disc = musicbrainz2.disc.readDisc(device)
250 trackcount = mkcue(disc, trackcount)
251 if options.no_musicbrainz:
254 releases = releases_by_disc(q, disc)
256 tags(q, releases, trackcount)
258 if options.title == None:
259 rip(device, trackcount, options.single_file)
260 except Exception, error:
261 if isinstance(error, SystemExit):
263 # check all print_exc and format_exc in fa-flacd.py; i think
264 # for some i don't do this msg print check
265 sys.stderr.write(getattr(error, 'msg', ''))
266 traceback.print_exc()
271 if __name__ == '__main__':
272 sys.exit(main(sys.argv))