-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShowImageCreator.py
300 lines (251 loc) · 9.87 KB
/
ShowImageCreator.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
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
import datetime
from random import randint
import sys
import textwrap
import argparse
import urllib.parse
from PIL import Image, ImageFont, ImageDraw
import requests
import os
# Defines location of different image files to create show image.
BACKGROUND_IMAGE_PATH = "GenericShowBackgrounds/"
COLOURED_BARS_PATH = "ColouredBars/"
DEFAULT_SHOW_IMAGE = "/images/default_show_profile.png"
def log(msg, showid="", stream=sys.stdout):
cur_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
stream.write("{} - ShowID({}), {}\n".format(cur_time, showid, msg))
def debug(msg, showid="", stream=sys.stdout):
if debugMode:
log(msg, showid, stream)
def error(msg, showid="", stream=sys.stderr):
log(msg, showid, stream)
sys.exit(1)
def getShows(apiKey):
"""Returns a dictionary of shows with show id mapping to the show title.
"""
debug("getShows()")
url = "https://ury.org.uk/api/v2/show/allshows?current_term_only=1&api_key=" + apiKey
try:
req = requests.get(url)
req.raise_for_status()
data = req.json()
except requests.exceptions.HTTPError as e:
error("Could not access API - {}".format(e))
shows = {}
for show_payload in data["payload"]:
showID = show_payload["show_id"]
showTitle = show_payload["title"]
showSubType = show_payload["subtype"]["class"]
shows[showID] = [showTitle, show_payload["photo"], showSubType]
debug(showTitle)
return shows
def getIfDefaultImage(image):
return image == DEFAULT_SHOW_IMAGE
def setImage(apiKey, showId):
# deprecated
image_location = apiDir + showId + '.jpg'
debug("API file location: " + image_location, showId)
if dryRun == False:
debug("Calling API to set image.", showId)
r = requests.put('https://ury.org.uk/api/v2/show/' + str(showId) +
'/showphoto?api_key=' + apiKey, json={'tmp_path': image_location})
return r.status_code == 200
else:
debug("Dry run, not setting image with API.", showId)
return True
def upload_to_myradio(showID):
return os.system(f"myradio-uploader/myradio-uploader {myr_user} {myr_pass} {showID} {outputDir + showID + '.jpg'}") == 0
def deleteImage(showId):
image_location = outputDir + showId + '.jpg'
if (os.path.exists(image_location)):
os.remove(image_location)
def applyBrand(showName, showID, branding):
"""Creates a show image for given show name, output file name and branding.
Args:
showName (str): Show name to add to image.
outputName (str): The name of the outputfile, standard form including the show id.
branding (str): The branding to be applied.
Return:
A JPG image to a sub folder called ShowImages.
"""
debug("applyBrand()")
showName = stripPrefix(showName)
# Determines which overlay to apply to the show image.
if branding == "speech":
brandingOverlay = "GreenSpeech.png"
elif branding == "news":
brandingOverlay = "News.png"
elif branding == "music":
brandingOverlay = "PurpleMusic.png"
elif branding == "event":
brandingOverlay = "RedEvent.png"
elif branding == "primetime":
brandingOverlay = "Flagship.png"
elif branding == "collab":
brandingOverlay = "Collab.png"
else: # Should be "regular"
brandingOverlay = "BlueGeneral.png"
debug("Branding {}".format(branding if branding != "" else "generic"), showID)
# maxNumberOfLines = 4
lines = normalize(showName, showID)
debug("Lines: " + str(lines))
debug("Line count: " + str(len(lines)))
if len(lines) > 5:
error("Show name is far too long, runs over 6 lines", showID)
if len(lines) > 3:
text_size = 40
else:
text_size = 70
debug(f"Text Size: {text_size}")
normalizedText = "\n".join(lines)
# Determines which background image to use for the show image.
img_path = BACKGROUND_IMAGE_PATH + str(randint(1, 25)) + ".png"
try:
img = Image.open(img_path)
except IOError as e:
error("Background image {} could not be opened - {}".format(img_path, str(e)))
# Opens overlay and pastes over the background image
overlay_path = COLOURED_BARS_PATH + brandingOverlay
try:
overlay = Image.open(overlay_path)
img.paste(overlay, (0, 0), overlay)
except IOError as e:
error("Overlay image {} could not be opened - {}".format(overlay_path, str(e)))
# ShowName formatting
debug("Formatting showname", showID)
textFont = ImageFont.truetype("Raleway-Bold.ttf", text_size)
debug("Normalised Text: " + normalizedText)
draw = ImageDraw.Draw(img)
w, h = draw.textsize(normalizedText, textFont)
# changes the start position, to centre text vertically
textLineHeight = max(200, 350 - (h/2))
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text(((800 - w) / 2, textLineHeight), normalizedText,
(255, 255, 255), textFont, align='center')
# website URY formatting
debug("Applying website branding", showID)
websiteURL = 'URY.ORG.UK \n @uniradioyork'
websiteTextSize = 50
websiteFont = ImageFont.truetype(
"Raleway-SemiBoldItalic.ttf", websiteTextSize)
draw = ImageDraw.Draw(img)
w, h = draw.textsize(websiteURL, websiteFont)
websiteURLHeight = 510
# draw.text((x, y),"Sample Text",(r,g,b))
draw.text(((800 - w) / 2, websiteURLHeight), websiteURL,
(255, 255, 255), websiteFont, align='center')
# Saves the image as the output name in a subfolder ShowImages
debug("Saving the final image", showID)
img.convert('RGB').save('{}{}.jpg'.format(outputDir, showID))
# Todo check if we actually created the file successfully!
return True
def stripPrefix(showName):
"""Strips any prefix from the show name.
Args:
showName (str): The show name.
Return:
The show name without the prefix.
"""
prefixes = [
"URY Brunch - ",
"URY:PM - ",
"URY Afternoon Tea: ",
"URY Brunch: ",
"URY Breakfast: ",
"URY Speech: ",
"URY Music: ",
"URY News & Sport: ",
"URY Sport: "
]
output = showName
for prefix in prefixes:
if showName.startswith(prefix):
output = showName[len(prefix):]
break
return output
def normalize(input_str, showID):
"""Splits the show name into separate lines of maximum lengths.
Args:
input (str): The Show name.
Return:
List of strings of limited length, depending on the size of the input text
"""
debug("normalize()")
lines = textwrap.wrap(input_str, width=17,
break_long_words=False, break_on_hyphens=False)
if len(lines) > 4 or len(max(lines, key=len)) > 17:
lines = textwrap.wrap(input_str, width=20,
break_long_words=False, break_on_hyphens=False)
if len(max(lines, key=len)) > 20:
error("Word too long for image", showID)
return lines
def main(env):
"""
Params:
env:
True if to read from environment variables
False if to read from argparse
"""
# Why these ones are and the apiKey isn't annoys me - MG
global debugMode
global dryRun
global apiDir
global outputDir
global myr_user
global myr_pass
if env:
# This is for Docker usage, so output dir is set, so the volume can link to it
apiKey = os.environ["API_KEY"]
dryRun = str(os.environ["DRY_RUN"]) == "1"
apiDir = os.environ["API_DIR"]
debugMode = str(os.environ["DEBUG_MODE"]) == "1"
outputDir = "/tmp/showimages/"
myr_user = os.environ["MYR_USER"]
myr_pass = os.environ["MYR_PASS"]
else:
parser = argparse.ArgumentParser(
description='Generate show images automagically.')
parser.add_argument('--apikey', required=True,
help='API key with valid permissions for viewing all shows and setting show images.')
parser.add_argument('--debug', action='store_true',
help='Print a lot of debug info.')
parser.add_argument('--dryrun', action='store_true',
help="Don't actually update the API with the images generated.")
parser.add_argument('--outputdir', required=True,
help='Location on the system to put the image files.')
parser.add_argument('--apidir', default=None,
help='Location on the API host where the image files will be. This is useful if you are using mounts instead. Defaults to --outputdir')
args = parser.parse_args()
apiKey = apiKey = urllib.parse.quote(args.apikey)
debugMode = args.debug
dryRun = args.dryrun
outputDir = args.outputdir
if args.apidir != None:
apiDir = args.apidir
else:
apiDir = outputDir
################################
################################
# Uses API To Get Shows #
################################
################################
debug("Program start")
ApiShowsDict = getShows(apiKey)
for showKey in ApiShowsDict:
show = ApiShowsDict[showKey]
if getIfDefaultImage(show[1]):
# The show uses the default image, let's make it one.
showName = show[0]
showID = str(showKey)
branding = show[2]
if applyBrand(showName, showID, branding):
if not env:
if setImage(apiKey, showID):
debug("Image set succeessfully, deleting.", showID)
deleteImage(showID)
if upload_to_myradio(showID):
debug("Image set succeessfully, deleting.", showID)
deleteImage(showID)
debug("Program complete")
if __name__ == "__main__":
main(False)