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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
#!/usr/bin/env python3
from __future__ import print_function
import sys
import os
import notify2
import json
import argparse
import fnmatch
from pprint import pprint
from mpd import MPDClient
import configparser
client = MPDClient()
config = configparser.ConfigParser()
config.sections()
config.read(os.getenv('HOME')+'/.config/clerk/helper_config')
change_db = (config['updater']['change_db'])
separator = (config['global']['separator'])
os.environ['separator'] = str(" "+separator+" ")
mpd_host = 'localhost'
mpd_port = '6600'
mpd_pass = ''
if 'MPD_HOST' in os.environ:
mpd_connection = os.environ['MPD_HOST'].split('@')
if len(mpd_connection) == 1:
mpd_host = mpd_connection[0]
elif len(mpd_connection) == 2:
mpd_host = mpd_connection[1]
mpd_pass = mpd_connection[0]
else:
print('Unable to parse MPD_HOST, using defaults')
if 'MPD_PORT' in os.environ:
mpd_port = os.environ['MPD_PORT']
client.connect(mpd_host, mpd_port)
if mpd_pass:
client.password(mpd_pass)
def update(args):
db = (client.stats())
new_db = db.get('db_update')
if change_db == new_db:
if (os.path.isfile(os.getenv('HOME')+'/.config/clerk/tracks.cache.json')):
quit()
else:
notify2.init('Clerk')
n = notify2.Notification("Clerk", "Updating Cache Files")
n.show()
createCache(args)
else:
notify2.init('Clerk')
n = notify2.Notification("Clerk", "Updating Cache Files")
n.show()
createCache(args)
config['updater']['change_db'] = str(new_db)
with open(os.getenv('HOME')+'/.config/clerk/helper_config', 'w') as configfile:
config.write(configfile)
def lookup(key, fields):
artistlist = client.list(key)
for artist in artistlist:
for element in client.find(key, artist):
entry = {}
for field in fields:
if field in element:
if isinstance(element[field], list):
entry[field] = element[field][0]
else:
entry[field] = element[field]
else:
entry[field] = ''
yield entry
def createCache(args):
# track cache creation
key = 'artist'
fields = ('artist', 'track', 'title', 'album', 'disc')
all_tracks = list({repr(t): t for t in lookup(key, fields)}.values())
with open(os.getenv('HOME')+'/.config/clerk/tracks.cache.json', "w") as cache_file:
json.dump(all_tracks, cache_file)
# album cache creation with only the youngest track modified
key = 'albumartist'
fields = ('albumartist', 'date', 'album', 'last-modified')
album_cache = {}
for entry in lookup(key, fields):
# key = repr(entry)
# cant use repr in this case because last-modified could differ for
# songs from the same album and we want these to clash.
key = entry['albumartist'] + entry['album']
if key not in album_cache:
album_cache[key] = entry
else:
if entry['last-modified'] > album_cache[key]['last-modified']:
album_cache[key] = entry
with open(os.getenv('HOME')+'/.config/clerk/albums.cache.json', "w") as cache_file:
json.dump([entry for entry in album_cache.values()], cache_file)
def track_sort(track_num):
if track_num == '':
return 0
else:
return int(track_num.split('/')[0])
def getTracks(args):
with open(os.getenv('HOME')+'/.config/clerk/tracks.cache.json') as cache_file:
content = json.load(cache_file)
sorted_content = sorted(content, key=lambda x: (x['artist'],
x['album'],
x['disc'],
track_sort(x['track'])))
print('\n'.join([os.getenv('separator').join([entry['artist'],
entry['track'],
entry['title'],
entry['album']])
for entry in sorted_content]))
def getAlbums(args):
with open(os.getenv('HOME')+'/.config/clerk/albums.cache.json') as cache_file:
content = json.load(cache_file)
sorted_content = sorted(content, key=lambda x: (x['albumartist'], x['date'], x['album']))
print('\n'.join([os.getenv('separator').join([entry['albumartist'],
entry['date'],
entry['album']])
for entry in sorted_content]))
def getLatest(args):
with open(os.getenv('HOME')+'/.config/clerk/albums.cache.json') as cache_file:
content = json.load(cache_file)
sorted_content = sorted(content, key=lambda x: (x['last-modified']), reverse=True)
print('\n'.join([os.getenv('separator').join([entry['date'],
entry['albumartist'],
entry['album'],
entry['last-modified']])
for entry in sorted_content]))
def loadjson(fname):
with open(fname) as data_file:
data = json.load(data_file)
return data
def extract_key(entry):
return entry["albumartist"] + entry["album"] + entry["date"]
def hashdata(jdata):
hashmap = {}
for i in range(len(jdata)):
key = extract_key(jdata[i])
hashmap[key] = i
return hashmap
def has_entry(fastlist, albumartist, album, date):
key = albumartist+album+date
if key in fastlist[1]:
return True
else:
return False
def get_entry(fastlist, albumartist, album, date):
return fastlist[0][fastlist[1][albumartist+album+date]]
def load_fastlist(fname):
jdata = loadjson(fname)
hdata = hashdata(jdata)
return (jdata, hdata)
def save_fastlist(fname, fastlist):
with open(fname, "w") as data_file:
json.dump(fastlist[0], data_file, ensure_ascii=False)
def append_entry(fastlist, entry):
index = len(fastlist[0])
fastlist[0].append(entry)
fastlist[1][extract_key(entry)] = index
def prune_fastlist(fastlist, mpdcachefile):
mpdlist = loadjson(mpdcachefile)
data = fastlist[0]
new = []
for e in mpdlist:
key = (e["albumartist"], e["album"], e["date"])
if has_entry(fastlist, *key):
entry = get_entry(fastlist, *key)
new.append(entry)
newhdata = hashdata(new)
return (new, newhdata)
def rateAlbum(args):
fastlist = load_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json')
try:
entry = get_entry(fastlist, client.currentsong()['albumartist'], client.currentsong()['album'], client.currentsong()['date'], client.currentsong()['disc'])
entry["rating"] = os.getenv('rating')
save_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json', fastlist)
client.sticker_set("song", client.currentsong()['file'], "albumrating", os.getenv('rating'))
except KeyError:
entry = {'albumartist': client.currentsong()['albumartist'], 'album': client.currentsong()['album'], 'date': client.currentsong()['date'], 'disc': client.currentsong()['disc'], 'albumrating': os.getenv('rating')}
append_entry(fastlist, entry)
save_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json', fastlist)
client.sticker_set("song", client.currentsong()['file'], "albumrating", os.getenv('rating'))
def loadjsonTrack(fname):
with open(fname) as data_file:
data = json.load(data_file)
return data
def extract_keyTrack(entry):
return entry["artist"] + entry["album"] + entry["track"] + entry["title"]
def hashdataTrack(jdata):
hashmap = {}
for i in range(len(jdata)):
key = extract_keyTrack(jdata[i])
hashmap[key] = i
return hashmap
def has_entryTrack(fastlist, artist, album, track, title):
key = artist+album+track+title
if key in fastlist[1]:
return True
else:
return False
def get_entryTrack(fastlist, artist, album, track, title):
return fastlist[0][fastlist[1][artist+album+track+title]]
def load_fastlistTrack(fname):
jdata = loadjsonTrack(fname)
hdata = hashdataTrack(jdata)
return (jdata, hdata)
def save_fastlistTrack(fname, fastlist):
with open(fname, "w") as data_file:
json.dump(fastlist[0], data_file, ensure_ascii=False)
def append_entryTrack(fastlist, entry):
index = len(fastlist[0])
fastlist[0].append(entry)
fastlist[1][extract_keyTrack(entry)] = index
def prune_fastlistTrack(fastlist, mpdcachefile):
mpdlist = loadjsonTrack(mpdcachefile)
data = fastlist[0]
new = []
for e in mpdlist:
key = (e["artist"], e["album"], e["track"], e["title"])
if has_entryTrack(fastlist, *key):
entry = get_entryTrack(fastlist, *key)
new.append(entry)
newhdata = hashdataTrack(new)
return (new, newhdata)
def rateTrack(args):
fastlist = load_fastlistTrack(os.getenv('HOME')+'/.config/clerk/trackratings.json')
try:
entry = get_entryTrack(fastlist, client.currentsong()['artist'], client.currentsong()['album'], client.currentsong()['track'], client.currentsong()['title'])
entry["rating"] = os.getenv('rating')
save_fastlistTrack(os.getenv('HOME')+'/.config/clerk/trackratings.json', fastlist)
client.sticker_set("song", client.currentsong()['file'], "rating", os.getenv('rating'))
except KeyError:
entry = {'artist': client.currentsong()['artist'], 'album': client.currentsong()['album'], 'track': client.currentsong()['track'], 'title': client.currentsong()['title'], 'rating': os.getenv('rating')}
append_entryTrack(fastlist, entry)
save_fastlistTrack(os.getenv('HOME')+'/.config/clerk/trackratings.json', fastlist)
client.sticker_set("song", client.currentsong()['file'], "rating", os.getenv('rating'))
def cleanRatings(args):
fastlist = load_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json')
prunedlist = prune_fastlist(fastlist, os.getenv('HOME')+'/.config/clerk/albums.cache.json')
save_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json', prunedlist)
fastlist = load_fastlistTrack(os.getenv('HOME')+'/.config/clerk/trackratings.json')
prunedlist = prune_fastlistTrack(fastlist, os.getenv('HOME')+'/.config/clerk/tracks.cache.json')
save_fastlistTrack(os.getenv('HOME')+'/.config/clerk/trackratings.json', prunedlist)
def importTrackRatings(args):
client.clear()
list = client.sticker_find('song', "", 'rating')
rated_tracks = []
for i in list:
rating_temp = i['sticker']
rating = rating_temp.split('=')[1]
for x in client.find('file', i['file']):
artist = x['artist']
album = x['album']
track = x['track']
title = x['title']
entry = {'artist': artist, 'album': album, 'track': track, 'title': title, 'rating': rating}
rated_tracks.append(entry)
with open(os.getenv('HOME')+'/.config/clerk/trackratings.json', 'w') as ratingfile:
json.dump(rated_tracks, ratingfile)
def importAlbumRatings(args):
client.clear()
list = client.sticker_find('song', "", 'albumrating')
rated_tracks = []
for i in list:
rating_temp = i['sticker']
rating = rating_temp.split('=')[1]
for x in client.find('file', i['file']):
artist = x['albumartist']
album = x['album']
date = x['date']
if 'disc' in x:
disc = x['disc']
else:
disc = ""
entry = {'albumartist': artist, 'album': album, 'date': date, 'disc': disc, 'rating': rating}
rated_tracks.append(entry)
with open(os.getenv('HOME')+'/.config/clerk/albumratings.json', 'w') as ratingfile:
json.dump(rated_tracks, ratingfile)
def sendStickers(args):
with open(os.getenv('HOME')+'/.config/clerk/trackratings.json') as cache_file:
content = json.load(cache_file)
for track in content:
uri = client.find('artist', track['artist'], 'album', track['album'], 'title', track['title'], 'track', track['track'])
rating = track['rating']
for x in uri:
client.sticker_set('song', x['file'], 'rating', rating)
print("Imported Rating of "+rating+" for file "+x['file'])
with open(os.getenv('HOME')+'/.config/clerk/albumratings.json') as cache_file:
content = json.load(cache_file)
for album in content:
uri = client.find('albumartist', album['albumartist'], 'album', album['album'], 'disc', album['disc'], 'date', album['date'], 'track', '1')
rating = album['rating']
for x in uri:
client.sticker_set('song', x['file'], 'albumrating', rating)
print("Imported Rating of "+rating+" for album "+album['albumartist']+" - "+album['album'])
def readComments(args):
args = vars(args)
comments = (client.readcomments(sys.stdin.read()[:-1]))
for key, value in sorted(comments.items()):
print('%s : %s' % (key, value))
def prioSong(args):
for line in sys.stdin.read().splitlines():
client.prio(255, line)
def savetoPlaylist(args):
for line in sys.stdin:
if line.strip():
line = line.strip()
client.playlistadd("clerk", line)
# print(line, end="")
# create commandline arguments
parser = argparse.ArgumentParser(prog='clerk_helper', description='Companion script for clerk')
subparsers = parser.add_subparsers()
parser_prio = subparsers.add_parser('prio', help="prioritize song")
parser_prio.set_defaults(call=prioSong)
parser_update = subparsers.add_parser('update', help="update cache files")
parser_update.set_defaults(call=update)
parser_readcomments = subparsers.add_parser('readcomments', help="show all tags of current song")
parser_readcomments.set_defaults(call=readComments)
parser_saveto = subparsers.add_parser('saveto', help="save stdin to playlist \"clerk\"")
parser_saveto.set_defaults(call=savetoPlaylist)
parser_createcache = subparsers.add_parser('createcache', help="create track cache for clerk")
parser_createcache.set_defaults(call=createCache)
parser_gettracks = subparsers.add_parser('getTracks', help="get all tracks from track cache for clerk")
parser_gettracks.set_defaults(call=getTracks)
parser_getalbums = subparsers.add_parser('getAlbums', help="get all albums from album cache for clerk")
parser_getalbums.set_defaults(call=getAlbums)
parser_getlatest = subparsers.add_parser('getLatest', help="get all albums from album cache for clerk, sorted by mtime")
parser_getlatest.set_defaults(call=getLatest)
parser_ratealbum = subparsers.add_parser('ratealbum', help="rate current Album")
parser_ratealbum.set_defaults(call=rateAlbum)
parser_ratetrack = subparsers.add_parser('ratetrack', help="rate current Track")
parser_ratetrack.set_defaults(call=rateTrack)
parser_cleanratings = subparsers.add_parser('cleanratings', help="Clean up Ratings file.")
parser_cleanratings.set_defaults(call=cleanRatings)
parser_importtrackratings = subparsers.add_parser('importtrackratings', help="Import track ratings from mpd stickers")
parser_importtrackratings.set_defaults(call=importTrackRatings)
parser_importalbumratings = subparsers.add_parser('importalbumratings', help="Import album ratings from mpd stickers")
parser_importalbumratings.set_defaults(call=importAlbumRatings)
parser_sendstickers = subparsers.add_parser('sendstickers', help="Send clerk ratings to mpd")
parser_sendstickers.set_defaults(call=sendStickers)
# parse arguments (thanks jat)
args = parser.parse_args()
try:
args.call(args)
except AttributeError:
print("No arguments given. Try clerk_helper -h")
|