-
Notifications
You must be signed in to change notification settings - Fork 5
/
lastfmsubmit
executable file
·105 lines (93 loc) · 3.06 KB
/
lastfmsubmit
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
#!/usr/bin/python
import sys
import os
import time
import getopt
import locale
import lastfm
import lastfm.client
import lastfm.marshaller
USAGE = """\
usage: lastfmsubmitd [--encoding ENC] [--artist ARTIST] [--title TITLE]
[--length LEN] [--time STAMP] [--album ALBUM] [--mbid TRACKID]
[--encoding ENC] [--stdout] [--debug] [--quiet] [--help]"""
if __name__ == '__main__':
shortopts = 'e:a:s:l:d:b:m:sdqh'
longopts = [
'encoding=',
'artist=',
'title=',
'length=',
'time=',
'album=',
'mbid=',
'stdout',
'debug',
'quiet',
'help',
]
try:
opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
except getopt.GetoptError, e:
print >>sys.stderr, 'lastfmsubmit: %s' % e
print >>sys.stderr, USAGE
sys.exit(1)
sub = {}
encoding = locale.getpreferredencoding()
stdout = False
debug = False
stderr = True
for opt, arg in opts:
if opt in ('--encoding', '-e'):
encoding = arg
if opt in ('--debug', '-d'):
debug = True
if opt in ('--quiet', '-q'):
stderr = False
cli = lastfm.client.Client('lastfmsubmit')
cli.open_log(debug, stderr)
try:
for opt, arg in opts:
try:
if opt in ('--artist', '-a'):
sub['artist'] = lastfm.marshaller.guess_enc(arg, encoding)
elif opt in ('--title', '-t'):
sub['title'] = lastfm.marshaller.guess_enc(arg, encoding)
elif opt in ('--length', '-l'):
sub['length'] = lastfm.marshaller.parse_length(arg)
elif opt in ('--time', '-i'):
sub['time'] = time.strptime(arg, lastfm.TIME_FMT)
elif opt in ('--album', '-b'):
sub['album'] = lastfm.marshaller.guess_enc(arg, encoding)
elif opt in ('--mbid', '-m'):
sub['mbid'] = arg
elif opt in ('--stdout', '-s'):
stdout = True
elif opt in ('--help', '-h'):
print USAGE
sys.exit(0)
except ValueError:
print >>sys.stderr, "lastfmsubmit: can't parse %s" % opt
sys.exit(1)
if len(sub) == 0:
print >>sys.stderr, 'lastfmsubmit: nothing specified'
print >>sys.stderr, USAGE
sys.exit(1)
if not sub.has_key('time'):
sub['time'] = time.gmtime()
if stdout:
print lastfm.marshaller.dump(sub)
else:
try:
cli.log.debug('Sending %s to daemon' % lastfm.repr(sub))
cli.submit(sub)
except IOError:
cli.log.error("Can't write sub: %s" % e)
sys.exit(1)
except SystemExit, e:
sys.exit(e.args[0])
except:
import traceback
einfo = traceback.format_exception(*sys.exc_info())
cli.log.error('Aborting: %s' % ''.join(einfo))
sys.exit(1)