-
Notifications
You must be signed in to change notification settings - Fork 9
/
feeds.py
194 lines (152 loc) · 7.13 KB
/
feeds.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
import logging
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from feedgen.feed import FeedGenerator
logger = logging.getLogger('flamingo.plugins.Feeds')
def make_urls_absolute(html, base_url):
soup = BeautifulSoup(html, 'html.parser')
for element in soup.find_all(['img', 'script', 'asciinema-player']):
try:
element['src'] = urljoin(base_url, element['src'])
except KeyError:
# not all elements might have a src attribute
continue
for link in soup.find_all('a'):
try:
link['href'] = urljoin(base_url, link['href'])
except KeyError:
# not all elements might have an href attribute
continue
return str(soup)
class Feeds:
def pre_build(self, context):
FEEDS_DOMAIN = getattr(context.settings, 'FEEDS_DOMAIN', '/')
FEEDS = getattr(context.settings, 'FEEDS', [])
for feed_config in FEEDS:
try:
content = {
'type': 'feed',
'feed_type': feed_config['type'],
'output': feed_config['output'],
'url': '/' + feed_config['output'],
}
if 'lang' in feed_config:
content['lang'] = feed_config['lang']
fg = FeedGenerator()
if 'lang' in feed_config:
fg.language(feed_config['lang'])
fg.id(feed_config['id'])
fg.title(feed_config['title'])
# set parameters needed for rss-feeds
if feed_config['type'] in ['rss', 'podcast']:
fg.description(feed_config['description'])
fg.link(href=feed_config['link'], rel='self')
fg.link(
href=feed_config['link_alternate'], rel='alternate'
)
# setup podcast environment
if feed_config['type'] == 'podcast':
fg.load_extension('podcast')
fg.podcast.itunes_image(feed_config['podcast_image'])
if 'itunes_owner' in feed_config:
fg.podcast.itunes_owner(**feed_config['itunes_owner'])
if 'itunes_category' in feed_config:
fg.podcast.itunes_category(
feed_config['itunes_category']
)
if 'itunes_explicit' in feed_config:
fg.podcast.itunes_explicit(
feed_config['itunes_explicit']
)
if 'itunes_author' in feed_config:
fg.podcast.itunes_author(feed_config['itunes_author'])
for i in feed_config['contents'](context):
fe = fg.add_entry()
# setup required entry attributes
fe_title = i['title'] or i['content_title']
fe_link = {
'href': '{}{}'.format(FEEDS_DOMAIN, i['url']),
'rel': 'alternate'
}
if 'entry-id' in feed_config:
fe_id = feed_config['entry-id'](i)
else:
fe_id = i['output']
if 'updated' in feed_config:
fe_updated = feed_config['updated'](i)
else:
fe_updated = ''
if 'published' in feed_config:
fe_published = feed_config['published'](i)
else:
fe_published = ''
if 'podcast' in i:
fe_podcast_url = i['podcast'].get('url', '')
fe_podcast_size = i['podcast'].get('size', 0)
fe_podcast_type = i['podcast'].get(
'type', 'audio/mpeg'
)
else:
fe_podcast_url = ''
fe_podcast_size = ''
# default value; will never be reported as missing
fe_podcast_type = 'audio/mpeg'
# check entry attributes
missing_attributes = []
if not fe_id:
missing_attributes.append('id')
if not fe_title:
missing_attributes.append('title')
if not fe_updated:
missing_attributes.append('updated')
if not fe_published:
missing_attributes.append('published')
if feed_config['type'] == 'podcast':
if not fe_podcast_url:
missing_attributes.append('podcast->url')
if not fe_podcast_size:
missing_attributes.append('podcast->size')
if missing_attributes:
logger.error('%s is missing attributes: %s',
i['path'] or i['i18n_path'] or i,
', '.join(missing_attributes))
return
# optional attributes
fe.id(fe_id)
fe.title(fe_title)
fe.updated(fe_updated)
fe.published(fe_published)
fe.link(fe_link)
if i['content_body']:
exitcode, output = context.pre_render(i)
output = make_urls_absolute(output, fe_link['href'])
if 'html_filter' in feed_config:
output = feed_config['html_filter'](output)
fe.content(output, type='html')
if i['authors']:
for author in i['authors']:
fe.author({
'name': author,
})
if i['summary']:
summary = str(i['summary'])
if 'html_filter' in feed_config:
summary = feed_config['html_filter'](summary)
fe.summary(summary, type='html')
if feed_config['type'] == 'podcast':
fe.enclosure(
fe_podcast_url,
str(fe_podcast_size),
fe_podcast_type,
)
# generate output
if feed_config['type'] == 'atom':
content['content_body'] = fg.atom_str().decode()
elif feed_config['type'] in ['rss', 'podcast']:
content['content_body'] = fg.rss_str().decode()
else:
raise ValueError(f'Unkown Feed type {feed_config["type"]}')
context.contents.add(**content)
except Exception:
logger.error("feed '%s' setup failed", feed_config['id'],
exc_info=True)