-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpImage.py
254 lines (219 loc) · 9.38 KB
/
npImage.py
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
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import time
import json
import os
import sys
config = {}
defaultConfig = """{
"imgPath" : "./image.png",
"textPath" : "./np.txt",
"font" : "arial.ttf",
"fontSize" : 15,
"colourFormat" : "RGBA",
"fontColour" : [0, 0, 0, 255],
"backgroundColour" : [0, 0, 0, 0],
"borderWidth" : 0,
"scanInterval" : 10,
"blankImageSize" : [1, 1],
"blankImageColourFormat" : "RGBA",
"blankImageBackground" : [0, 0, 0, 0],
"blankImageText" : [""],
"blankImageFont" : "arial.ttf",
"blankImageFontSize" : 15,
"blankImageFontColour" : [0, 0, 0, 255],
"blankImageBorderWidth": 0,
"hostname" : "example.org",
"port" : 22,
"username" : "username",
"remoteDir" : ".",
"SFTPscanInterval" : 10
}"""
def getConfig():
global defaultConfig, config
confFile = "./npImage.json"
if not os.path.isfile(confFile):
cf = open(confFile, "w", encoding = "utf-8")
cf.write(defaultConfig)
cf.close()
print("Generated new config file (npImage.json)")
sys.exit(0)
cf = open(confFile, "r", encoding = "utf-8")
config = json.loads(cf.read())
checkConfig()
cf.close()
config["fontColour"] = tuple(config["fontColour"])
config["backgroundColour"] = tuple(config["backgroundColour"])
config["blankImageSize"] = tuple(config["blankImageSize"])
config["blankImageBackground"] = tuple(config["blankImageBackground"])
config["blankImageFontColour"] = tuple(config["blankImageFontColour"])
def checkConfig():
global config
expectedTypes = {
"imgPath" : str,
"textPath" : str,
"font" : str,
"fontSize" : int,
"colourFormat" : str,
"fontColour" : list,
"backgroundColour" : list,
"borderWidth" : int,
"scanInterval" : int,
"blankImageSize" : list,
"blankImageColourFormat" : str,
"blankImageBackground" : list,
"blankImageText" : list,
"blankImageFont" : str,
"blankImageFontSize" : int,
"blankImageFontColour" : list,
"blankImageBorderWidth": int,
"hostname" : str,
"port" : int,
"username" : str,
"remoteDir" : str,
"SFTPscanInterval" : int
}
for option, expectedType in expectedTypes.items():
try:
if (type(config[option]) != expectedType):
print("Invalid %s. Expected type %s, got %s." % (option, str(expectedType), str(type(config[option]))))
sys.exit(1)
except KeyError:
print("Missing option: %s" % option)
sys.exit(1)
for channel in config["fontColour"]:
if (type(channel) != int):
print("Invalid fontColour value: %s. Expected type %s, got %s." % (str(channel), str(int), str(type(channel))))
sys.exit(1)
for channel in config["backgroundColour"]:
if (type(channel) != int):
print("Invalid backgroundColour value: %s. Expected type %s, got %s." % (str(channel), str(int), str(type(channel))))
sys.exit(1)
for channel in config["blankImageSize"]:
if (type(channel) != int):
print("Invalid blankImageSize value: %s. Expected type %s, got %s." % (str(channel), str(int), str(type(channel))))
sys.exit(1)
for channel in config["blankImageBackground"]:
if (type(channel) != int):
print("Invalid blankImageBackground value: %s. Expected type %s, got %s." % (str(channel), str(int), str(type(channel))))
sys.exit(1)
for channel in config["blankImageText"]:
if (type(channel) != str):
print("Invalid blankImageText value: %s. Expected type %s, got %s." % (str(channel), str(str), str(type(channel))))
sys.exit(1)
for channel in config["blankImageFontColour"]:
if (type(channel) != int):
print("Invalid blankImageFontColour value: %s. Expected type %s, got %s." % (str(channel), str(int), str(type(channel))))
sys.exit(1)
def main():
getConfig()
mainLoop("")
def createImage(text):
global config
lengths = []
for line in text:
lengths.append(len(line))
maxLengthIndex = lengths.index(max(lengths))
font = ImageFont.truetype(config["font"], config["fontSize"])
imageSize = font.getsize(text[maxLengthIndex])
lineHeight = imageSize[1]
imageSize = (imageSize[0] + (2 * config["borderWidth"]), (lineHeight * len(text)) + (2 * config["borderWidth"]))
try:
img = Image.new(config["colourFormat"], imageSize, config["backgroundColour"])
except ValueError:
return 1
draw = ImageDraw.Draw(img)
lineNumber = 0
for line in text:
draw.text((config["borderWidth"], (lineHeight * lineNumber) + config["borderWidth"]), line, config["fontColour"], font = font)
lineNumber += 1
img.save(config["imgPath"])
def createBlankImage(text):
global config
lengths = []
for line in text:
lengths.append(len(line))
maxLengthIndex = lengths.index(max(lengths))
font = ImageFont.truetype(config["blankImageFont"], config["blankImageFontSize"])
imageSize = font.getsize(text[maxLengthIndex])
lineHeight = imageSize[1]
imageSize = (imageSize[0] + (2 * config["blankImageBorderWidth"]), (lineHeight * len(text)) + (2 * config["blankImageBorderWidth"]))
try:
img = Image.new(config["blankImageColourFormat"], imageSize, config["blankImageBackground"])
except ValueError:
return 1
draw = ImageDraw.Draw(img)
lineNumber = 0
for line in text:
draw.text((config["blankImageBorderWidth"], (lineHeight * lineNumber) + config["blankImageBorderWidth"]), line, config["blankImageFontColour"], font = font)
lineNumber += 1
img.save(config["imgPath"])
def clearImage():
global config
if config["blankImageText"] != [""]:
createBlankImage(config["blankImageText"])
else:
img = Image.new(config["blankImageColourFormat"], config["blankImageSize"], config["blankImageBackground"])
img.save(config["imgPath"])
def mainLoop(oldSong):
global config
blankImage = False
paused = False
while (1):
songInfo = getSongInfo()
if (songInfo["isplaying"] != "1"):
if not blankImage:
clearImage()
blankImage = True
elif ((songInfo["path"] != oldSong) or ((songInfo["ispaused"] == "1") and (paused == False)) or ((songInfo["ispaused"] != "1") and (paused == True))):
paused = False
# print("New song: %s - %s" % (songInfo["artist"], songInfo["title"]))
imageText = []
if songInfo["ispaused"] == "1":
paused = True
imageText.append("Now playing (Paused)")
else:
imageText.append("Now playing")
imageText.append("%s - %s" % (songInfo["artist"], songInfo["title"]))
if songInfo["album"]:
imageText.append(songInfo["album"])
imageText.append("%s / %skb/s / %sbit / %sHz / %s / %s" % (songInfo["codec"], songInfo["bitrate"], songInfo["bitdepth"], songInfo["samplerate"], songInfo["channels"], songInfo["filesize"]))
imageText.append(songInfo["playcount"])
createImage(imageText)
oldSong = songInfo["path"]
time.sleep(config["scanInterval"])
def getSongInfo():
global config
songinfo = {}
output = []
with open(config["textPath"], "r", encoding="utf-8") as file:
for line in file.readlines():
line = line.replace("\r", "")
if len(line.split("=")) > 2:
songinfo[line.split("=")[0]] = "=".join(line.split("=").pop(0))[:-1]
elif len(line.split("=")) < 2:
songinfo[line.split("=")[0]] = ""
elif len(line.split("=")) == 2:
songinfo[line.split("=")[0]] = line.split("=")[1][:-1]
if songinfo["isplaying"] == "1":
songinfo["title"] = songinfo["title"].rstrip() # Fix for Heartfire's broken title metadata
if songinfo["album"] == "?":
if songinfo["path"][:4] == "http":
songinfo["album"] = songinfo["path"]
else:
songinfo["album"] = ""
if songinfo["length"] == "?":
songinfo["length"] = ""
if songinfo["filesize"] == "?":
songinfo["filesize"] = ""
if songinfo["playcount"] == "1":
songinfo["playcount"] = "%s total play" % songinfo["playcount"]
else:
songinfo["playcount"] = "%s total plays" % songinfo["playcount"]
if songinfo["channels"] == "1":
songinfo["channels"] = "%s channel" % songinfo["channels"]
else:
songinfo["channels"] = "%s channels" % songinfo["channels"]
return songinfo
main()