-
Notifications
You must be signed in to change notification settings - Fork 0
/
MakeOpenSongReport.py
executable file
·289 lines (247 loc) · 10.8 KB
/
MakeOpenSongReport.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
#!/usr/bin/env python3
import os
import sys
from xml.etree import ElementTree as ET
import time
import json
import re
import csv
# SETUP GLOBAL VARIABLES
SET_SEARCH = re.compile(r'(\d{4})-(\d{2})-(\d{2})[\s-]*(.*)')
REPORT_YEAR = 2021
if len(sys.argv) > 1:
REPORT_YEAR = int(sys.argv[1])
REPORT_START_DATE = "%d-01-01" % REPORT_YEAR
REPORT_END_DATE = "%d-01-01" % (REPORT_YEAR + 1)
IGNORE_WORDS = ['fusion', 'book']
basedir = '/Volumes/Data/SEAFILE/LCC/LCC Worship Team'
reportdir = os.path.join(basedir, 'Song Usage Reports')
opensong = os.path.join(basedir, 'OpenSong')
setsdir = os.path.join(opensong, 'Sets')
songsdir = os.path.join(opensong, 'Songs')
class OpenSongSet():
def __init__(self, setpath):
# print 'opening set:', setpath
if not os.path.exists(setpath):
return
xml = ET.parse(setpath)
root = xml.getroot()
self.name = root.attrib['name']
self.description = ''
self.songs = []
slide_groups = xml.find('slide_groups')
for slide_group in slide_groups:
if slide_group.attrib['type'] == 'song':
name = slide_group.attrib['name']
try:
subdir = slide_group.attrib['path']
except:
subdir = ''
if subdir != '':
self.songs.append(os.path.join(subdir, name))
else:
self.songs.append(name)
# print self.name
# print self.songs
class OpenSongSong():
data = {}
name = ''
lyrics = ''
author = ''
copyright = ''
last_used = ''
def __init__(self, songpath):
# print 'opening song:', songpath
if not os.path.exists(songpath):
return
xml = ET.parse(songpath)
song = xml.getroot()
self.data = {}
for element in song:
# print element.tag
# print element.text
# print element.attrib
self.data[element.tag] = element.text
self.name = self.data['title']
self.lyrics = self.data['lyrics']
self.author = self.data['author']
self.copyright = self.data['copyright']
if __name__ == "__main__":
# first we get the sets from the sets directory
# then we sort by date and take the ones in the past year
# then we grab each song in each set and create data objects like this
# song object == { songname : {setdate, uses} }
# set object == { setdate : [song objects] }
#
# then we create two lists: one list is all songs in descending order of uses
# second list is most recent 16 sets
sets = {}
allsongs = {}
recent_sets = []
for root, dirs, files in os.walk(setsdir):
for basename in files:
pathname = os.path.join(root, basename)
try:
set = OpenSongSet(pathname)
except:
print("could not parse", basename)
continue
name = set.name
lower = name.lower()
should_ignore = False
for word in IGNORE_WORDS:
if word in lower:
should_ignore = True
if should_ignore:
continue
namedata = SET_SEARCH.findall(name)
if len(namedata) == 0:
continue
else:
print(namedata)
try:
datestamp = time.strptime("%s-%s-%s" % namedata[0][:3], "%Y-%m-%d")
except ValueError:
continue
set.description = namedata[0][-1]
if datestamp < time.strptime(REPORT_START_DATE, "%Y-%m-%d"):
continue
if datestamp > time.strptime(REPORT_END_DATE, "%Y-%m-%d"):
continue
print("\nparsing songs from set:")
print(name)
print(time.strftime('%B %d, %Y', datestamp))
print("===================================")
ignore_remaining_songs_in_set = False
songs_seen = []
for song_relative_path in set.songs[:]:
song_real_path = os.path.join(songsdir, song_relative_path)
name = os.path.basename(song_real_path)
# if we have already seen an "alternates" divider, we ignore the rest of the songs in this set.
if ignore_remaining_songs_in_set:
set.songs.remove(song_relative_path)
continue
# have we already seen this song in this set?
if name in songs_seen:
set.songs.remove(song_relative_path)
continue
songs_seen.append(name)
# is this song an admin divider for unused "alternates"
if 'ALTERNATES' in name:
print("-- ALTERNATES found. Ignoring remaining songs in this set. ---")
ignore_remaining_songs_in_set = True
set.songs.remove(song_relative_path)
continue
# is this song an admin divider
if '===' in name:
# print "IGNORED:", name
set.songs.remove(song_relative_path)
continue
# if we got here, add this song to the report array
print("ADDED: ", name)
song = OpenSongSong(song_real_path)
if name not in allsongs:
allsongs[name] = {"uses": 1, 'author': song.author, 'copyright': song.copyright, 'last_used': datestamp}
allsongs[name]['last_used_string'] = f"{datestamp.tm_year}-{datestamp.tm_mon:02}-{datestamp.tm_mday:02}"
else:
allsongs[name]['uses'] += 1
if (datestamp > allsongs[name]['last_used']):
allsongs[name]['last_used'] = datestamp
allsongs[name]['last_used_string'] = f"{datestamp.tm_year}-{datestamp.tm_mon:02}-{datestamp.tm_mday:02}"
# print set.songs
sets[datestamp] = set
# WE SAVE THE RAW SONG REPORT DATA
towrite = {'allsongs': allsongs, 'recent_sets': recent_sets}
reportpath = os.path.join(reportdir, f'LCC Song Usage ({REPORT_YEAR}).json')
open(reportpath, 'w').write(json.dumps(towrite, indent=2))
# NOW, WE PREPARE THE HTML REPORT
html = "<html><head><style>body{font-family:'Source Sans Pro';}</style><title>OpenSongReport %s</title></head><body>" % time.strftime('%c')
# now we need to sort the sets into ascending order by date
print("SETLISTS FROM %d" % REPORT_YEAR)
html += '<div id="menu"><a href="#setlists">Setlists for %s</a> || <a href="#frequency">Song Frequency</a></div>' % REPORT_YEAR
html += '<a name="setlists" />'
html += '<div id="setlists">'
html += '<h1>Setlists For %s</h1>\n' % REPORT_YEAR
for datestamp in sorted(sets.keys()):
set = sets[datestamp]
print(time.strftime('%B %d, %Y', datestamp))
html += '<h2>%s <small>%s</small></h2><ul>' % (time.strftime('%B %d, %Y', datestamp), set.description)
print('===================================')
print(set.songs)
for song in set.songs:
if '===' in song:
continue
name = os.path.basename(song)
songdata = allsongs[name]
# print "%s (%s / (c) %s)" % (name, songdata['author'] or 'Unknown', songdata['copyright'] or 'Unknown')
html += '<li>%s</li>' % "%s (%s / (c) %s)" % (name, songdata['author'] or 'Unknown', songdata['copyright'] or 'Unknown')
print()
print()
html += '</ul>\n'
html += '</div> <!-- end setlists -->'
html += '<a name="frequency" />'
html += '<div id="frequency">'
# now we need to sort the songs into descending order by uses
print("")
print("SONGS BY FREQUENCY")
html += '<h1>Songs By Frequency</h1>\n<ul>'
print('===================================')
sorted_songs = sorted(allsongs.keys(), key=lambda y: (allsongs[y]['uses']), reverse=True)
for songname in sorted_songs:
if '===' in songname:
continue
songdata = allsongs[songname]
statline = f'{songdata["uses"]} times {songname} (last time {songdata["last_used_string"]}) ({songdata["author"] or "Unknown"} / (c) {songdata["copyright"] or "Unknown"})'
print(statline)
html += f'<li>{statline}</li>'
html += '</ul></div></body></html>'
reportname = "LCC Song Usage (%s).html" % REPORT_YEAR
reportpath = os.path.join(reportdir, reportname)
open(reportpath, 'w').write(html)
# NOW WE PREPARE THE CSV USAGE REPORT
reportname = "LCC Song Usage (%s).csv" % REPORT_YEAR
reportpath = os.path.join(reportdir, reportname)
with open(reportpath, 'w') as csvfile:
report_writer = csv.writer(csvfile, delimiter=",", quotechar='"')
report_writer.writerow(['Uses', 'Last Use', 'Song Name'])
for songname in sorted_songs:
if '===' in songname:
continue
uses = allsongs[songname]['uses']
last_used = allsongs[songname]['last_used_string']
report_writer.writerow([uses, last_used, songname])
# exit()
# AT YEAR END RUN THIS TOO
# now add this report to the all songs report
reportname = "LCC Song Usage (all).csv"
reportpath = os.path.join(reportdir, reportname)
with open(reportpath, 'r') as csvfile:
report_reader = csv.reader(csvfile, delimiter=",", quotechar='"')
first_line = True
for row in report_reader:
if first_line:
first_line = False
continue
count = int(row[0].replace('"', '').strip())
last_used = row[1].strip().replace('"', '')
name = row[2].strip().replace('"', '')
if name in allsongs:
allsongs[name]['uses'] += count
if last_used > allsongs[name]['last_used_string']:
allsongs[name]['last_used_string'] = last_used
else:
allsongs[name] = {"uses": 1, 'last_used_string': last_used}
# NOW WE PREPARE THE FINAL CSV USAGE REPORT
towrite = 'Uses,Last Use,Song Name\n'
sorted_songs = sorted(allsongs.keys(), key=lambda y: (allsongs[y]['uses']), reverse=True)
reportname = "LCC Song Usage (all).csv"
reportpath = os.path.join(reportdir, reportname)
with open(reportpath, 'w') as csvfile:
report_writer = csv.writer(csvfile, delimiter=",", quotechar='"')
report_writer.writerow(['Uses', 'Last Use', 'Song Name'])
for songname in sorted_songs:
if '===' in songname:
continue
uses = allsongs[songname]['uses']
last_used = allsongs[songname]['last_used_string']
report_writer.writerow([uses, last_used, songname])