-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_rss.py
More file actions
executable file
·359 lines (303 loc) · 12.5 KB
/
Copy pathcreate_rss.py
File metadata and controls
executable file
·359 lines (303 loc) · 12.5 KB
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
#!/usr/bin/python3
# update environment to handle Unicode
PYTHONIOENCODING="utf-8"
# Ideas:
# Function to check if file should be ignored. Only accept recognised files
# Check for existence of nfo file
# May be read details from nfo file?
# Move reading of tags into a function so can have different versions
# depending if reading nfo or audio file
# Name output rss by input file directory.rss if output file not provided
# or just provided as a directory.
# Overwrite existing rss file.
# import libraries
import os
import sys
import datetime
import mutagen
from urllib.parse import urlsplit, quote, urlunsplit
import xml.etree.ElementTree as ET
from textwrap import TextWrapper
def print_diag(level, value, linefeed = True):
if level < config.verbosity:
if level == CRITICAL:
print("***", end="")
try:
# If unicode, convert to UTF-8 string
value = value.encode("utf-8", "replace")
except:
pass
try:
print(str(value), end="")
except UnicodeEncodeError:
# OK, failed to output a UTF-8 string so try plain ASCII
value = value.encode("ascii", "replace")
print(str(value), end="")
if linefeed:
print()
# format date method
def formatDate(dt):
return dt.strftime("%a, %d %b %Y %H:%M:%S +0000")
# A quick and dirty function to produce a 'safe' version the
# the URL for embedding in the RSS feed
def urlquote(url, *bits, **kwds):
for bit in bits:
if url[-1] != '/' and bit[0] != '/':
url += '/'
url += bit
parts = urlsplit(url)
parts = parts._replace(path=quote(parts.path))
url = urlunsplit(parts)
return url
# A function to produce the RSS XML skeleton
# Returns a tuple of the whole XML and the channel element for population
def create_rss_channel(config):
#record datetime started
now = datetime.datetime.now()
# Construct the XML
xml = ET.ElementTree(ET.Element("rss", {"version": "2.0"}))
# Now populate the 'rss' element */
chan = ET.SubElement(xml.getroot(), "channel")
ET.SubElement(chan, "title").text = config.rssTitle
ET.SubElement(chan, "description").text = config.rssDescription
ET.SubElement(chan, "link").text = config.rssLink
ET.SubElement(chan, "ttl").text = str(config.rssTtl)
ET.SubElement(chan, "lastBuildDate").text = formatDate(now)
ET.SubElement(chan, "pubDate").text = formatDate(now)
return xml, chan
# Ignore files unless they have one of these extensions and include the item type for each
fileTypes = {
".aac": "audio/mp4",
".m4a": "audio/mp4",
".mp4": "audio/mp4",
".mp3": "audio/mpeg",
".wma": "audio/x-ms-wma",
}
def WMA_process_tags(wmaTags, mTime):
tags = {
"title" : "Unknown title",
"album" : "Unknown album",
"date" : datetime.datetime.fromtimestamp(mTime),
"comment" : "No comment",
}
copyright = "\xa9"
try:
tags["title"] = wmaTags["Title"][0]
except KeyError:
print_diag(IMPORTANT, "Unable to determine title from metadata")
try:
tags["album"] = wmaTags["Album"][0]
except KeyError:
print_diag(IMPORTANT, "Unable to determine album from metadata")
try:
fileDate = wmaTags["Date"][0]
try:
# If unicode, convert to regular string
fileDate = fileDate.encode('utf-8')
except:
pass
fileTimeStamp = datetime.datetime.strptime(fileDate, "%Y-%m-%dT%H:%M:%SZ")
tags["date"] = fileTimeStamp
except (KeyError, ValueError):
print_diag(IMPORTANT, "Unable to parse date from meta-data; falling back to %s" % tags["date"])
try:
tags["comment"] = unicode(wmaTags["Comment"][0])
except KeyError:
print_diag(INFOMATION, "Unable to determine description from metadata")
return tags
def MP4_process_tags(mp4tags, mTime):
tags = {
"title" : "Unknown title",
"album" : "Unknown album",
"date" : datetime.datetime.fromtimestamp(mTime),
"comment" : "No comment",
}
copyright = "\xa9"
try:
tags["title"] = mp4tags[copyright + "nam"][0]
except KeyError:
print_diag(IMPORTANT, "Unable to determine title from metadata")
try:
tags["album"] = mp4tags[copyright + "alb"][0]
except KeyError:
print_diag(IMPORTANT, "Unable to determine album from metadata")
try:
fileDate = mp4tags[copyright + "day"][0]
try:
# If bytes, convert to regular string
fileDate = fileDate.decode('utf-8')
except:
pass
fileTimeStamp = datetime.datetime.strptime(fileDate, "%Y-%m-%dT%H:%M:%SZ")
tags["date"] = fileTimeStamp
except (KeyError, ValueError):
print_diag(IMPORTANT, "Unable to parse date from meta-data; falling back to %s" % tags["date"])
try:
tags["comment"] = mp4tags[copyright + "cmt"][0]
except KeyError:
print_diag(INFOMATION, "Unable to determine description from metadata")
return tags
def MP3_process_tags(mp3tags, mTime):
tags = {
"title" : "Unknown title",
"album" : "Unknown album",
"date" : datetime.datetime.fromtimestamp(mTime),
"comment" : "No comment",
}
try:
tags["title"] = mp3tags["TIT2"].text[0]
except KeyError:
print_diag(IMPORTANT, "Unable to determine title from metadata")
try:
tags["album"] = mp3tags["TALB"].text[0]
except KeyError:
print_diag(IMPORTANT, "Unable to determine album from metadata")
try:
fileDate = mp3tags["TDRC"].text[0]
try:
# If bytes, convert to regular string
fileDate = fileDate.decode('utf-8')
except:
pass
fileTimeStamp = datetime.datetime.strptime(fileDate, "%Y-%m-%dT%H:%M:%SZ")
tags["date"] = fileTimeStamp
except (KeyError, ValueError):
print_diag(IMPORTANT, "Unable to parse date from meta-data; falling back to %s" % tags["date"])
for k in mp3tags.keys():
if k.startswith("COMM"):
tags["comment"] = mp3tags[k].text[0]
break
else:
print_diag(INFOMATION, "Unable to determine description from metadata")
return tags
# The mutagen "easy mode" doesn't work with some tag formats so this, with the functions above,
# replicates it but gives us the ability to work with the tags that we may get...
tagTypes = {
"MP4": MP4_process_tags,
"MP3": MP3_process_tags,
"ASF": WMA_process_tags,
}
CRITICAL, IMPORTANT, INFOMATION, DEBUG, EXTRA_DEBUG = range(5)
defaults = {
"source": (None, "String (required) - The directory containing content for this feed"),
"sourceUrl": (None, "String (required) - The URL for the 'source' directory on the internet"),
"rssFile": (None, "String (required) - The RSS (i.e. XML) file to produce"),
"rssTitle": ("Random podcast title", "String - The RSS feed title"),
"rssLink": ("http://www.example.com/", "String - The website corresponding to the RSS feed"),
"rssDescription": ("Random podcast description", "String - The RSS feed description"),
"rssTtl": (60, "Integer - How long (in minutes) a feed can be cached before being refreshed"),
"maxAge": (None, "Integer - The max age, indays, for items to be included in the RSS feed"),
"deleteOld": (False, "Boolean - Whether files older than 'maxAge' should be deleted"),
"deleteAllOld": (False, "Boolean - If 'maxAge' is set and 'deleteOld' is True, deletes all old files not just audio files"),
"episodeTitle": ("%(title)s", "String - The string format to use for the title of each episode. Available attributes are 'title', 'album' and 'comment'"),
"verbosity": (2, "Integer (0-5) - Amount of information to output. 0 results in no output"),
}
class ConfigOptionException(Exception): pass
class BadConfigOptionException(ConfigOptionException): pass
class ConfigOptionNotSetException(ConfigOptionException): pass
class Config(object):
def __init__(self, defaults):
self._defaults = defaults
self._config = {}
def __call__(self, filename):
for k, v in self._defaults.items():
self._config[k] = v[0]
with open(filename) as codeFile:
code = compile(codeFile.read(), filename, "exec")
exec(code, {}, self._config)
for k in self._config.keys():
assert k in self._defaults.keys(), "'%s' is not a valid configuration option" % k
if self._config[k] is None:
del self._config[k]
def __getattr__(self, attr):
try:
return self._config[attr]
except KeyError:
if attr in self._defaults.keys():
raise ConfigOptionNotSetException("'%s' has not be set" % attr)
else:
raise BadConfigOptionException("'%s' is not a valid configuration option" % attr)
def __str__(self):
return str(self._config)
config = Config(defaults)
if len(sys.argv) != 2:
textWrapper = TextWrapper(initial_indent = " ", width = 78)
print("""Usage: %s <config file>
The config file is a python script setting some or all of the following variables:
""" % sys.argv[0], file=sys.stderr)
for k, v in sorted(defaults.items()):
print(" " + k + ":", file=sys.stderr)
o = v[1].find("-")
if o < 0:
textWrapper.subsequent_indent = " "
else:
textWrapper.subsequent_indent = " " * (6 + o)
print("\n".join(textWrapper.wrap(v[1])), file=sys.stderr)
if v[0] is not None:
print(" Default:", repr(v[0]), file=sys.stderr)
sys.exit()
else:
config(sys.argv[1])
xml, chan = create_rss_channel(config)
# walk through all files and subfolders
for path, subFolders, files in os.walk(config.source):
for f in files:
# We use the extension to work out the media type
ext = os.path.splitext(f)[1]
# Get the full path of the file
fullPath = os.path.join(path, f)
# find the path relative to the starting folder, e.g. /subFolder/file
relativePath = os.path.relpath(fullPath, config.source)
print_diag(INFOMATION, relativePath)
try:
config.maxAge # Test for this option having been set
mTime = datetime.datetime.fromtimestamp(os.path.getmtime(fullPath))
timediff = datetime.datetime.now() - mTime
if timediff.days > config.maxAge:
print_diag(DEBUG, "%s is older than maxAge" % f)
if config.deleteOld and (ext in fileTypes.keys() or config.deleteAllOld):
# This is an old audio file and we've been told to delete it
print_diag(IMPORTANT, "%s is older than maxAge - deleting" % f)
os.remove(fullPath)
continue
except ConfigOptionNotSetException:
pass
if not ext in fileTypes.keys():
continue
try:
audioTags = mutagen.File(fullPath)
except Exception as e:
print_diag(CRITICAL, "Got Mutagen exception for '%s': " % fullPath + str(e))
continue
print_diag(INFOMATION, "Tag class: " + audioTags.__class__.__name__)
try:
print_diag(INFOMATION, audioTags.pprint())
except AttributeError:
print_diag(CRITICAL, "Unable to pprint() audio tags for '%s': " % fullPath)
try:
tags = tagTypes[audioTags.__class__.__name__](audioTags, os.path.getmtime(fullPath))
except KeyError:
print_diag(CRITICAL, "Unable to process tag class " + audioTags.__class__.__name__)
continue
print_diag(INFOMATION, tags["title"])
print_diag(INFOMATION, tags["album"])
print_diag(INFOMATION, tags["date"])
print_diag(INFOMATION, tags["comment"])
# Add the item to the RSS XML
url = urlquote(config.sourceUrl, relativePath)
item = ET.SubElement(chan, "item")
ET.SubElement(item, "title").text = config.episodeTitle % tags
ET.SubElement(item, "description").text = tags["comment"]
ET.SubElement(item, "link").text = url
ET.SubElement(item, "guid").text = url
ET.SubElement(item, "pubDate").text = formatDate(tags["date"])
ET.SubElement(item, "enclosure", {
"url": url,
"length": str(os.path.getsize(fullPath)),
"type": fileTypes[ext],
})
#end for loop
#end for loop
# Write the XML
xml.write(config.rssFile, "UTF-8")
print_diag(EXTRA_DEBUG, "complete")