~ubuntu-branches/debian/stretch/ears/stretch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python

import os
import sys
import time
import getopt
import select
import fcntl

import lastfm.client
import lastfm.marshaller
from lastfm.config import SaneConfParser

BUF_CHUNK = 4096
BUF_MAX = 2048
BUF_INIT = 512

def popen(argv, mode='r'):
    command = ' '.join(argv)
    return os.popen(command.encode(sys.getfilesystemencoding()), mode)

def quotemeta(s):
    for meta in ('\\', '$', '`', '"', '\n'):
        s = s.replace(meta, '\\' + meta)
    return '"%s"' % s

class Command:
    def __init__(self, user_opts):
        self.user_opts = user_opts.split()

class CdParanoia(Command):
    def open(self, device, number):
        return popen(['cdparanoia', '-r'] + self.user_opts + ['-d', device,
            '%d' % number, '-'])

class APlay(Command):
    def open(self):
        return popen(['aplay', '-q', '-t', 'raw', '-c', '2', '-r', '44100',
            '-f', 'S16_LE'] + self.user_opts, 'w')

class Bfp(Command):
    def open(self):
        return popen(['bfp'] + self.user_opts, 'w')

class OggEnc(Command):
    def open(self, song, path):
        argv = ['oggenc', '-Q', '-r'] + self.user_opts
        try: argv += ['-a', quotemeta(song['artist'])]
        except KeyError: pass
        try: argv += ['-t', quotemeta(song['title'])]
        except KeyError: pass
        try: argv += ['-l', quotemeta(song['album'])]
        except KeyError: pass
        try: argv += ['-N', '%d' % song['number']]
        except KeyError: pass
        try: argv += ['-c', quotemeta('musicbrainz_trackid=%s' % song['mbid'])]
        except KeyError: pass
        argv += ['-o', quotemeta('%s.ogg' % path), '-']
        return popen(argv, 'w')

class Lame(Command):
    def open(self, song, path):
        argv = ['lame', '--quiet', '-rx'] + self.user_opts
        try: argv += ['--ta', quotemeta(song['artist'])]
        except KeyError: pass
        try: argv += ['--tt', quotemeta(song['title'])]
        except KeyError: pass
        try: argv += ['--tl', quotemeta(song['album'])]
        except KeyError: pass
        try: argv += ['--tn', '%d' % song['number']] # XXX
        except KeyError: pass
        argv += ['-', quotemeta('%s.mp3' % path)]
        return popen(argv, 'w')

def sox_open(path):
    return popen(['sox', path, '-t', 'raw', '-r', '44100', '-c', '2', '-s',
        '-'])

rippers = {'cdparanoia': CdParanoia}
players = {'aplay': APlay, 'bfp': Bfp}
encoders = {'oggenc': OggEnc, 'lame': Lame}

def_path = '%(artist)s/%(album)s/%(number)02d - %(title)s'

if __name__ == '__main__':
    shortopts = 'd:e:qc'
    longopts = ['device=', 'encoder', 'quiet', 'continue']

    try:
        opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
    except getopt.GetoptError, e:
        print >>sys.stderr, 'peel: %s' % e
        sys.exit(1)

    device = '/dev/cdrom'
    quiet = False

    cp = SaneConfParser()
    cp.read([os.path.expanduser('~/.peelrc')])

    rip_cmd = cp.get('commands', 'rip', 'cdparanoia')
    play_cmd = cp.get('commands', 'play', 'aplay')
    enc_cmd = cp.get('commands', 'encode', 'oggenc')

    rip_opts = cp.get('options', rip_cmd, '')
    play_opts = cp.get('options', play_cmd, '')
    enc_opts = cp.get('options', enc_cmd, '')

    path_tmpl = cp.get('output', 'path', def_path)

    for opt, arg in opts:
        if opt in ('--device', '-d'):
            device = arg
        elif opt in ('--quiet', '-q'):
            quiet = True
        elif opt in ('--continue', '-c'):
            device = None
        elif opt in ('--encoder', '-e'):
            enc_cmd = arg

    try:
        ripper = rippers[rip_cmd](rip_opts)
        player = players[play_cmd](play_opts)
        encoder = encoders[enc_cmd](enc_opts)
    except KeyError, e:
        print >>sys.stderr, 'unknown command: %s' % e.args[0]

    if not quiet:
        play = player.open()
        cli = lastfm.client.Client('peel')
        cli.open_log()

    for song in lastfm.marshaller.load_documents(sys.stdin):
        print "Track %(number)s: %(title)s..." % song

        safe_song = {}
        for k, v in song.iteritems():
            try:
                safe_song[k] = v.replace('/', '_')
            except AttributeError:
                safe_song[k] = v

        path = path_tmpl % safe_song
        dir = os.path.dirname(path)
        if dir and not os.path.isdir(dir):
            os.makedirs(dir)

        enc = encoder.open(song, path)

        if device:
            rip = ripper.open(device, song['number'])
        else:
            rip = sox_open('track%02d.cdda.wav' % song['number'])

        buf = [rip.read(BUF_CHUNK) for i in range(0, BUF_INIT)]

        flags = fcntl.fcntl(rip, fcntl.F_GETFL)
        fcntl.fcntl(rip, fcntl.F_SETFL, flags|os.O_NONBLOCK)

        while len(buf) > 0 or not rip.closed:
            while len(buf) < BUF_MAX and not rip.closed:
                rd, wr, ex = select.select([rip], [], [], 0)
                if rip in rd:
                    raw = rip.read(BUF_CHUNK)
                    if len(raw) > 0:
                        buf.append(raw)
                    else:
                        rip.close()
                else:
                    break
            if len(buf) > 0:
                raw = buf.pop(0)
                enc.write(raw)
                if not quiet:
                    play.write(raw)

        if not quiet:
            if song['length'] >= 30:
                song['time'] = time.gmtime()
                cli.submit(song)
                cli.log.info('Sent %s to daemon' % lastfm.repr(song))