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
|
#!/usr/bin/env python3
from __future__ import print_function
import sys
import os
import notify2
import argparse
import types
from mpd import MPDClient
import time
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')):
quit()
else:
notify2.init('Clerk')
n = notify2.Notification("Clerk", "Updating Cache Files")
n.show()
albumCache(args)
trackCache(args)
lastCache(args)
else:
notify2.init('Clerk')
n = notify2.Notification("Clerk", "Updating Cache Files")
n.show()
albumCache(args)
trackCache(args)
lastCache(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)
content = set()
for artist in artistlist:
for element in client.find(key, artist):
elementvalues = []
for field in fields:
if field in element:
fieldvalue = element[field]
else:
fieldvalue = ''
if isinstance(fieldvalue, list):
elementvalues.append(fieldvalue[0])
else:
elementvalues.append(fieldvalue)
content.add(tuple(elementvalues))
return content
def trackCache(args):
key = 'artist'
fields = ('artist', 'track', 'title', 'album')
content = lookup(key, fields)
fh = open(os.getenv('HOME')+'/.config/clerk/tracks.cache', "w")
entries = [os.getenv('separator').join(entry) for entry in content]
for entry in sorted(entries, key=str.lower):
fh.write(entry+"\n")
fh.close()
def albumCache(args):
key = 'albumartist'
fields = ('albumartist', 'date', 'album')
content = lookup(key, fields)
fh = open(os.getenv('HOME')+'/.config/clerk/album.cache', "w")
entries = [os.getenv('separator').join(entry) for entry in content]
for entry in sorted(entries, key=str.lower):
fh.write(entry+"\n")
fh.close()
def lastCache(args):
key = 'albumartist'
fields = ('last-modified', 'date', 'albumartist', 'album')
content = lookup(key, fields)
seen = set()
fh = open(os.getenv('HOME')+'/.config/clerk/last.cache', "w")
for element in sorted(content, key=lambda elem: elem[0], reverse=True):
if element[1:] not in seen:
line = element[1:]
fh.write(os.getenv('separator').join(line)+"\n")
seen.add(line)
fh.close()
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)
#create commandline arguments
parser = argparse.ArgumentParser(prog='mppc', description='A mpc clone in python')
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_albumcache = subparsers.add_parser('albumcache', help="create album cache for clerk")
parser_albumcache.set_defaults(call=albumCache)
parser_trackcache = subparsers.add_parser('trackcache', help="create track cache for clerk")
parser_trackcache.set_defaults(call=trackCache)
parser_lastcache = subparsers.add_parser('lastcache', help="create album cache sorted by addition for clerk")
parser_lastcache.set_defaults(call=lastCache)
#parse arguments (thanks jat)
args = parser.parse_args()
try:
args.call(args)
except AttributeError:
print("No arguments given. Try mppc -h")
|