-
Notifications
You must be signed in to change notification settings - Fork 1
/
feedpipe.py
executable file
·311 lines (219 loc) · 7.45 KB
/
feedpipe.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/python
import feedparser
import feed.atom as FA
from uuid import uuid4
from datetime import datetime
class FeedPipe(object):
""" FeedPipe is a tool to mutate feeds; it's your very own Yahoo Pipes.
`It was originally written in Perl <https://metacpan.org/pod/Feed::Pipe>`_
by `Vince Veselosky <http://www.control-escape.com/>`_, and ported to
Python by `Arthur Axel fREW Schmidt <https://blog.afoolishmanifesto.com>`_
for use on AWS Lambda.
Args:
title (str): title of feed; defaults to "Combined Feed"
id (str): id of feed; defaults to autogenerated uuid
updated (str of iso8601 datetime): last time feed was updated; defaults
to current time
Example::
import FeedPipe from feedpipe;
fp = FeedPipe(title => "Mah Bukkit") \\
.cat('1.xml', '2.rss', '3.atom') \\
.grep(lambda e: 'lolrus' in e.title.text) \\
.sort \\
.head
print(fp.as_xml())
.. _entry:
A few of the most important methods in FeedPipe use entry_ objects.
Entry objects are `sorta documented
<http://home.blarg.net/~steveha/pyfeed.html>`_,
but not really. Basically they have the following properties:
* id
* title
* updated
* content
* link
* summary
* published
Bugs:
Currently not all of `Atom 1.0
<http://atomenabled.org/developers/syndication/>`_, which the feeds are
translated to, is supported. Specifically the following fields are not
translated:
* contributor
* source
* rights
Patches to support them very welcome.
"""
def __init__(self,
title="Combined Feed",
id=None,
updated=None):
self.title = title
if id is None:
id = 'urn:' + str(uuid4())
self.id = id
if updated is None:
updated = datetime.now().isoformat()
self.updated = updated
self.entries = []
def cat(self, feeds):
"""Adds list of new entry_ objects to the FeedPipe
.. _feed:
* Filenames
* URLs
* a string containing XML
Example::
fp.cat([
'https://blog.afoolishmanifesto.com/index.xml',
'./foo.xml',
])
Args:
feeds (List[feed_]): an array of feeds
Returns:
self: you know, for chaining!
"""
for feed in feeds:
data = feedparser.parse(feed)
for e in data.entries:
entry = FA.Entry()
if 'id' in e:
entry.id = e.id
if 'title' in e:
entry.title = e.title
if 'updated' in e:
entry.updated = e.updated
if 'author' in e:
entry.author = FA.Author(e.author)
if 'content' in e:
entry.content = e.content[0].value
if 'link' in e:
link = FA.Link(e.link)
entry.link = link
if 'summary' in e:
entry.summary = e.summary
if 'tags' in e:
entry.categories = FA.Collection(
FA.Category(tag.term,
scheme=tag.scheme,
label=tag.label)
for tag in e.tags)
if 'published' in e:
try:
entry.published = e.published
except ValueError:
entry.published = datetime.strptime(
e.published, '%a, %d %b %Y %H:%M:%S %Z'
).isoformat()
self.entries.append(entry)
return self
def grep(self, filter):
""" Filters the entries in the FeedPipe
Example::
fp.grep(lambda e: "Video" not in e.title.text)
Args:
filter (callback): callback takes an entry; return value is True if
entry should be included.
Returns:
self: for chaining
"""
self.entries = [x for x in self.entries if filter(x)]
return self
def head(self, length=10):
""" Truncates feed starting at the beginning
Example::
fp.head()
Args:
length (int): how many entries to include; defaults to 10
Returns:
self: for chaining
"""
self.entries = self.entries[:length]
return self
def map(self, transform):
""" Transforms entries in the FeedPipe
Example::
def fix_title(e):
e.title = "[STATION] " + e.title.text
return e
fp.map(fix_title)
Args:
transform (callback): callback takes entry_ and returns it,
possibly modified
"""
self.entries = [transform(x) for x in self.entries]
return self
def reverse(self):
""" Reverses the entries in the FeedPipe
Example::
fp.reverse()
Returns:
self: for chaining
"""
self.entries.reverse()
return self
def _default_key(e):
if e.updated:
return e.updated.text
elif e.published:
return e.published.text
def sort(self, cmp=None, key=_default_key, reverse=True):
""" Sorts the entries in the FeedPipe
Example::
fp.sort(key=lambda e: e.title.text)
Args:
cmp, key, and reverse; matches the interface of `the sorter
builtin
<https://docs.python.org/2/library/functions.html#sorted>`_.
``key`` defaults to updated/published and ``reverse`` defaults
to True, so that the default sort is newest at the beginning.
"""
self.entries.sort(cmp=cmp, key=key, reverse=reverse)
return self
def tail(self, length=10):
""" Truncates the FeedPipe from the end
Example::
fp.tail()
Args:
length (int): how many entries to include, starting at the end.
Defaults to 10.
Returns:
self: for chaining
"""
self.entries = self.entries[len(self.entries) - length:]
return self
def as_atom_obj(self):
""" Object for interacting with XML directly
Example::
fp.as_atom_obj()
Returns:
`feed.atom.Feed <http://home.blarg.net/~steveha/pyfeed.html>`_.
"""
feed = FA.Feed()
feed.generator = 'feedpipe 0.0.1'
if self.title:
feed.title = self.title
if self.id:
feed.id = self.id
if self.updated:
feed.updated = self.updated
for e in self.entries:
feed.entries.append(e)
return feed
def as_xml(self):
""" Gets the actual XML
Example::
fp.as_xml()
Returns:
feed: XML encoded in string
"""
xmldoc = FA.XMLDoc()
xmldoc.root_element = self.as_atom_obj()
return str(xmldoc)
def count(self):
"""
Example::
fp.count()
Returns:
int: count of entries in the FeedPipe
"""
return len(self.entries)