-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyaspp.py
executable file
·157 lines (116 loc) · 4.11 KB
/
yaspp.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
#!/usr/bin/env python
import json
import os
import string
import podgen
import yaml
import config
import templates
#-------------------------------------------------------------------------------
import html.parser
class HTMLStripParser(html.parser.HTMLParser):
def __init__(self):
super(HTMLStripParser, self).__init__()
self.result = []
def handle_data(self, data):
self.result.append(data)
def get_text(self):
return "".join(self.result)
def strip_html_tags(html):
p = HTMLStripParser()
p.feed(html)
return p.get_text()
#-------------------------------------------------------------------------------
def read_content_yaml(filename):
for entry in yaml.load_all(open(filename), Loader=yaml.Loader):
for audio in entry["audio"]:
audio["url"] = string.Template(audio["url"]).substitute(
media_base_url=config.media_base_url)
yield entry
def generate_subscribe_button(content):
return templates.subscribe_button.substitute(
title=config.podcast_title,
description=repr(config.hello_text),
cover=config.small_cover_image,
datacolor=config.podlove_player_theme.get("main", "blue"),
feed_url=config.feed_url
)
def generate_html_entry(entryid: int, entry: dict):
entrydivid = "yassp_entry_%d" % entryid
clean_entry = entry.copy()
clean_entry.pop("uuid", None)
clean_entry.pop("long_summary", None)
clean_entry.pop("long_summary_md", None)
clean_entry["theme"] = config.podlove_player_theme
podlove_player = "<script>podlovePlayer('#player_%s', %s);</script>" % \
(entrydivid, json.dumps(clean_entry))
if long_summary_md := entry.get("long_summary_md"):
import markdown
long_summary = markdown.markdown(long_summary_md)
else:
long_summary = entry.get("long_summary", "")
return templates.entry.substitute(
uuid=entry["uuid"],
entrydivid=entrydivid,
title=entry["title"],
summary=entry["summary"],
long_summary=long_summary,
) + podlove_player
def generate_html(content, rev=True):
op = reversed if rev else lambda x: x
content_list = "\n".join(generate_html_entry(i, e)
for i, e in enumerate(op(content)))
return templates.index_html.substitute(
podcast_title=config.podcast_title,
hello_text=config.hello_text,
footer_text=config.footer_text,
subscribe_button=generate_subscribe_button(content),
content=content_list
)
#-------------------------------------------------------------------------------
def generate_feed(content, audio_idx=0):
def create_long_summary(entry):
if long_summary_md := entry.get("long_summary_md"):
import markdown
long_summary = markdown.markdown(long_summary_md)
else:
long_summary = entry.get("long_summary", "")
return entry.get("summary"), long_summary
p = podgen.Podcast(
name=config.podcast_title,
description=config.hello_text,
website=config.website,
image=config.cover_image,
language=config.language,
copyright=config.copyright,
authors=[podgen.Person(config.author_name)],
category=podgen.Category(config.category),
explicit=False
)
p.episodes = [podgen.Episode(
id=entry["uuid"],
title=entry["title"],
summary=entry["summary"],
long_summary=("<p>%s</p>\n%s" % create_long_summary(entry)),
publication_date=entry["publicationDate"],
media=podgen.Media.create_from_server_response(entry["audio"][audio_idx]["url"]),
) for entry in content]
return str(p)
#-------------------------------------------------------------------------------
def parseArgs():
import argparse
parser = argparse.ArgumentParser(
description='Generate a static feed and podlove webplayer list.')
parser.add_argument("yaml_file", type=str, help="The content file.")
parser.add_argument("-o", "--output-dir", type=str, default=".",
help="Output directory (default: .)")
return parser.parse_args()
def main():
args = parseArgs()
content = list(read_content_yaml(args.yaml_file))
with open(os.path.join(args.output_dir, "index.html"), "w") as outfile:
outfile.writelines(generate_html(content))
with open(os.path.join(args.output_dir, "feed.xml"), "w") as outfile:
outfile.writelines(generate_feed(content))
if __name__ == "__main__":
main()