-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·85 lines (73 loc) · 2.25 KB
/
app.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
#!/usr/bin/env python
import os
import urllib.parse
from email.utils import formatdate
from hashlib import md5
from pathlib import Path
import bottle
BASE_URL = os.environ['BASE_URL']
PODCAST_DIR = os.environ['PODCAST_DIR']
app = bottle.Bottle()
FEED_TPL = """\
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
<channel>
<title>Symen autofeed</title>
<description>Automatically fenerated feed</description>
<link>{{link}}</link>
% for item in items:
<item>
<title>{{item['title']}}</title>
<description>{{item['desc']}}</description>
<pubDate>{{item['pub_date']}}</pubDate>
<enclosure url="{{item['file_url']}}"
% # type="audio/mpeg"
length="{{item['file_size']}}"
/>
% # <itunes:duration></itunes:duration>
<guid isPermaLink="false">{{item['guid']}}</guid>
</item>
% end
</channel>
</rss>
"""
@app.get('/feed', name="feed")
def view_feed():
items = []
files = (
file for file in
Path(PODCAST_DIR).glob('*.*')
if file.is_file() and not file.name.startswith('.')
)
sorted_files = sorted(
files, reverse=True, key=lambda it: it.stat().st_mtime
)
for file in sorted_files:
item = {}
stat = file.stat()
hash = md5(
(file.name + str(stat.st_size)).encode()
)
item['guid'] = hash.hexdigest()
item['title'] = file.name
item['desc'] = ''
item['pub_date'] = formatdate(stat.st_mtime)
quoted_name = urllib.parse.quote(file.name)
static_path = app.get_url("static", path=quoted_name)
item['file_url'] = BASE_URL + static_path
item['file_size'] = stat.st_size
items.append(item)
bottle.response.set_header(
"Content-Type", "text/xml; charset=utf-8"
)
static_url = BASE_URL + app.get_url("static", path="")
return bottle.template(
FEED_TPL, link=static_url, items=items
)
@app.get('/<path:path>', name="static")
def view_static(path):
return bottle.static_file(path, root=PODCAST_DIR)
application = app.wsgi
if __name__ == '__main__':
app.run(host='::', port=8006, debug=True, reloader=True)