-
Notifications
You must be signed in to change notification settings - Fork 15
/
feedlist.py
565 lines (445 loc) · 18.1 KB
/
feedlist.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
""":mod:`libearth.feedlist` --- Feed list
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Test opml here
.. code-block:: xml
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>exported opml</title>
</head>
<body>
<outline text="category1" title="category1">
<outline type="rss" text="feed1" title="feed1"
xmlUrl="http://feed1.com/rss" htmlUrl="http://feed1.com/"/>
</outline>
<outline type="rss" text="feed2" title="feed2"
xmlUrl="http://feed2.com/rss" htmlUrl="http://feed2.com"/>
</body>
</opml>
FeedList can import opml file:
>>> from libearth.feedlist import FeedList
>>> feedlist = FeedList('feedlist.opml')
>>> print feedlist.title
exported opml
>>> print feedlist[0]
<libearth.feedlist.FeedCategory type='category' title=u'category1'>
>>> print feedlist[0][0]
<libearth.feedlist.Feed type='feed' title=u'feed1'>
>>> print feedlist[0][0].title
feed1
>>> print feedlist[0][0].xml_url
http://feed1.com/rss
change and save to file:
>>> from libearth.feedlist import FeedList
>>> feedlist = FeedList('feedlist.opml')
>>> feedlist.title = "changed title"
>>> feedlist.save_file()
with file name:
>>> feedlist.save_file('newfile.opml')
create opml without import:
>>> from libearth.feedlist import Feed, FeedList
>>> feedlist = FeedList()
>>> feed = Feed('rss2', 'title', 'http://feed1.com/rss', 'http://feed1.com/')
>>> feedlist.append(feed)
>>> feedlist.save_file('/tmp/test.opml')
or:
>>> from libearth.feedlist import Feed, FeedList
>>> feedlist = FeedList()
>>> feedlist.add('rss2', 'title', 'http://feed1.com/rss', 'http://feed1.com/')
>>> feedlist.save_file('/tmp/test.opml')
"""
from collections import MutableSequence
from datetime import datetime
from .codecs import Boolean, Integer, Rfc822, Version
from .compat import text_type
from .schema import (Attribute, Child, Codec, DecodeError, DocumentElement,
Element, Text, read, write)
from .tz import now
__all__ = ('AlreadyExistException', 'CommaSeparatedList', 'Feed',
'FeedCategory', 'FeedList', 'FeedTree', 'SaveOpmlError',)
class CommaSeparatedList(Codec):
"""Encode strings e.g. ``['a', 'b', 'c']`` into a comma-separated list
e.g. ``'a,b,c'``, and decode it back to a Python list. Whitespaces
between commas are ignored.
>>> codec = CommaSeparatedList()
>>> codec.encode(['technology', 'business'])
'technology,business'
>>> codec.decode('technology, business')
['technology', 'business']
"""
def encode(self, value):
if value is None:
res = ""
elif isinstance(value, text_type):
res = value
else:
res = ",".join(value)
return res
def decode(self, text):
if text is None:
lst = []
else:
lst = [elem.strip() for elem in text.split(',')]
return lst
class FeedTree(object):
"""Joint base type of :class:`FeedCategory`, :class:`Feed`. It has
two common attributes: :attr:`type` and :attr:`title`.
"""
def __init__(self, type, title):
self.type = type
self.title = title
def __repr__(self):
return '<{0.__module__}.{0.__name__} type={1!r} title={2!r}>'.format(
type(self), self.type, self.title
)
class FeedCategory(FeedTree, MutableSequence):
"""Category of feeds to organize. It can recursively contains other
categories as well. It implements :class:`~collections.MutableSequence`
interface.
:param title: human-readable title of the category.
it can replace with ``text`` when ``text`` is not present
:type title: :class:`str`
:param type: ``type`` attribute of ``outline`` element.
not used but OPML spec says `outline` element can has
`type` attribute
:type type: :class:`str`
:param text: ``text`` attribute of ``outline`` element.
it is set to :arg:`title` if omitted
:type text: :class:`str`
:param category: ``category`` attribute of ``outline`` element.
not used but OPML spec says ``outline`` element can have
``category`` attribute
:type category: :class:`str`
:param is_breakpoint: ``isBreakpoint`` attribute of ``outline`` element.
not used but OPML spec says ``outline`` element can
have ``isBreakpoint`` attribute
:type is_breakpoint: :class:`str`
:param created: ``created`` attribute for ``outline`` element.
:type created: :class:`datetime.datetime`
"""
type = 'category'
def __init__(self, title, type=None, text=None, category=None,
created=None):
super(FeedCategory, self).__init__('category', title)
self.text = text
self._type = type
self.text = text or title
if created is not None and not isinstance(created, datetime):
try:
codec = Rfc822()
created = codec.decode(created)
except DecodeError:
raise TypeError(
'Expected an instance of {0.__module__}.{0.__name__} or '
'valid RFC822 string, not {1!r}'.format(datetime, created))
else:
self.created = created
self.children = []
self.urls = [] # to avoid duplication of feeds for the same category
def get_all_feeds(self):
res = []
for obj in self.children:
if isinstance(obj, FeedCategory):
res += obj.get_all_feeds()
else:
res.append(obj)
return list(set(res))
def insert(self, index, value):
if not isinstance(value, FeedTree):
raise TypeError('expected an instance of {0.__module__}.'
'{0.__name__}, not {1!r}'.format(FeedTree, value))
if value.type == 'feed':
if value.xml_url in self.urls:
raise AlreadyExistException(
'{0!r} is already added'.format(value)
)
else:
self.urls.append(value.xml_url)
elif value.type == 'category':
if value in self:
raise AlreadyExistException(
'{0!r} is already added'.format(value)
)
elif self in value:
raise AlreadyExistException(
'{0!r} already contains {1!r}; '
'circular reference is not allowed'.format(value, self)
)
elif value is self:
raise AlreadyExistException('cannot contain itself')
self.children.insert(index, value)
def __contains__(self, key):
if not isinstance(key, FeedTree):
raise TypeError('key has to be an instance of {0.__module__}.'
'{0.__name__}, not {1!r}'.format(FeedTree, key))
if key in self.children:
return True
for child in self.children:
if child.type == 'category' and key in child:
return True
return False
def __iter__(self):
return iter(self.children)
def __len__(self):
return len(self.children)
def __getitem__(self, index):
return self.children[index]
def __setitem__(self, index, value):
if not isinstance(value, FeedTree):
raise TypeError('value must be an instance of {0.__module__}.'
'{0.__name__}, not {1!r}'.format(FeedTree, value))
self.children[index] = value
def __delitem__(self, index):
del self.children[index]
class Feed(FeedTree):
"""Subscription feed. This class has attributes to represent
an OPML ``outline`` element.
.. note::
:attr:`type` cannot be changed from ``'rss'``.
If you want to get ``type`` attribute of outline,
use :attr:`rsstype` instead.
:param rsstype: ``type`` attribute of ``outline`` element
:type rsstype: :class:`str`
:param title: ``title`` attribute of ``outline`` element
:type title: :class:`str`
:param xml_url: ``xmlUrl`` attribute of ``outline`` element
:type xml_url: :class:`str`
:param html_url: ``htmlUrl`` attribute of ``outline`` element
:type html_url: :class:`str`
:param text: ``text`` attribute of ``outline`` element.
set to :arg:`title` if omitted
:type text: :class:`str`
:param category: ``category`` attribute of ``outline`` element
:type category: :class:`str`
:param is_breakpoint: ``isBreakpoint`` attribute of ``outline`` element
:type is_breakpoint: :class:`str`
:param created: ``created`` attribute of ``outline`` element.
it is valid :rfc:`822` format
:type created: :class:`datetime.datetime`
"""
def __init__(self, rsstype, title, xml_url, html_url=None, text=None,
category=None, is_breakpoint=None, created=None):
super(Feed, self).__init__('feed', title)
self.rsstype = rsstype
self.xml_url = xml_url
self.html_url = html_url
self.text = text or title
self.category = category
self.is_breakpoint = is_breakpoint
self.created = created
class OutlineElement(Element):
"""Represent ``outline`` element of OPML document."""
text = Attribute('text', required=True)
title = Attribute('title')
type = Attribute('type')
xml_url = Attribute('xmlUrl')
html_url = Attribute('htmlUrl')
category = Attribute('category', CommaSeparatedList)
is_breakpoint = Attribute('isBreakpoint', Boolean)
created = Attribute('created', Rfc822)
children = Child('outline', 'OutlineElement', multiple=True)
class HeadElement(Element):
"""Represent ``head`` element of OPML document."""
title = Text('title')
date_created = Text('dateCreated', Rfc822)
date_modified = Text('dateModified', Rfc822)
owner_name = Text('ownerName')
owner_email = Text('ownerEmail')
owner_id = Text('ownerId')
docs = Text('docs')
expansion_state = Text('expansionState', CommaSeparatedList)
vert_scroll_state = Text('vertScrollState', Integer)
window_top = Text('windowTop', Integer)
window_bottom = Text('windowBottom', Integer)
window_left = Text('windowLeft', Integer)
window_right = Text('windowRight', Integer)
class BodyElement(Element):
"""Represent ``body`` element of OPML document.
:attr:`outline` contains child :class:`OutlineElement` objects.
"""
outline = Child('outline', OutlineElement, multiple=True)
class OpmlDoc(DocumentElement):
__tag__ = 'opml'
head = Child('head', HeadElement)
body = Child('body', BodyElement)
version = Attribute('version', Version, default=(2, 0))
class FeedList(MutableSequence):
"""Represent OPML document.
:param path: file path to save the document. if not present, the document
won't be saved but just on memory
:type path: :class:`str`
"""
#: (:class:`collections.Mapping`) Entire :class:`Feed` objects contained
#: by one or multiple categories. Hashed by triple of (:attr:`~Feed.type`,
#: :attr:`~Feed.title`, :attr:`~Feed.xml_url`).
all_feeds = None
def __init__(self, path=None, is_xml_string=False):
# default values FIXME: the following magic constants should be avoided
self.title = "EarthReader"
self.owner_name = "EarthReader"
self.owner_email = "earthreader@librelist.com"
self.owner_id = "earthreader.org"
self.docs = "http://dev.opml.org/spec2.html"
self.expansion_state = []
self.vert_scroll_state = 0
self.window_top = 0
self.window_left = 0
self.window_bottom = 0
self.window_right = 0
self.date_created = 0
self.date_modified = 0
self.path = path
self.feedlist = FeedCategory(self.title)
self.all_feeds = {}
if self.path:
self.open_file(is_xml_string)
if not path and not is_xml_string:
self.doc = OpmlDoc()
self.doc.head = HeadElement()
self.doc.body = BodyElement()
def open_file(self, is_xml_string):
if is_xml_string:
xml = self.path
self.doc = read(OpmlDoc, xml)
self.parse_doc()
else:
with open(self.path) as fp:
xml = fp.read()
self.doc = read(OpmlDoc, xml)
self.parse_doc()
def parse_doc(self):
self.title = self.doc.head.title
self.owner_name = self.doc.head.owner_name
self.owner_email = self.doc.head.owner_email
self.owner_id = self.doc.head.owner_id
self.docs = self.doc.head.docs
self.expansion_state = self.doc.head.expansion_state
self.vert_scroll_state = self.doc.head.vert_scroll_state
self.window_top = self.doc.head.window_top
self.window_left = self.doc.head.window_left
self.window_bottom = self.doc.head.window_bottom
self.window_right = self.doc.head.window_right
self.date_created = self.doc.head.date_created
self.date_modified = self.doc.head.date_modified
for outline in self.doc.body.outline:
self.feedlist.append(self.convert_from_outline(outline))
def save_file(self, filename=None):
"""Save the document as an OPML to the file.
If ``filename`` is not present, the path the constructor gave will
be used instead.
"""
self.doc.head.title = self.title
self.doc.head.owner_name = self.owner_name
self.doc.head.owner_email = self.owner_email
self.doc.head.owner_id = self.owner_id
self.doc.head.docs = self.docs
self.doc.head.expansion_state = self.expansion_state
self.doc.head.vert_scroll_state = self.vert_scroll_state
self.doc.head.window_top = self.window_top
self.doc.head.window_left = self.window_left
self.doc.head.window_bottom = self.window_bottom
self.doc.head.window_right = self.window_right
self.doc.body.outline[:] = []
for feed in self.feedlist:
self.doc.body.outline.append(self.convert_to_outline(feed))
timestamp = now()
self.doc.head.date_modified = timestamp
if not self.doc.head.date_created:
self.doc.head.date_created = timestamp
try:
filename = filename or self.path
with open(filename, 'w') as fp:
for chunk in write(self.doc):
fp.write(chunk)
except Exception as e:
raise SaveOpmlError(e)
def add_feed(self, type, title, xml_url, html_url=None, text=None,
category=None, is_breakoint=None, created=None):
feed = self.make_feed(type, title, xml_url, html_url, text,
category=None, is_breakoint=None, created=None)
self.feedlist.append(feed)
def insert(self, index, feed):
xml_url = feed.xml_url if hasattr(feed, 'xml_url') else None
key = (feed.type, feed.title, xml_url)
if key in self.all_feeds:
orig_feed = self.all_feeds.get(key)
self.feedlist.insert(index, orig_feed)
orig_feed.html_url = feed.html_url
orig_feed.text = feed.text
else:
self.feedlist.insert(index, feed)
def make_feed(self, type, title, xml_url, html_url=None, text=None,
category=None, is_breakoint=None, created=None):
"""Find the existing feed, or add one to include it into multiple
categories.
"""
text = text or title
key = (type, title, xml_url)
feed = self.all_feeds.get(key)
if feed:
feed.html_url = html_url
feed.text = text
else:
feed = Feed(type, title, xml_url, html_url, text, category=None,
is_breakpoint=None, created=None)
self.all_feeds[key] = feed
return feed
def get_all_feeds(self):
return self.feedlist.get_all_feeds()
def convert_from_outline(self, outline_obj):
if outline_obj.children:
title = outline_obj.title or outline_obj.text
type = outline_obj.type
text = outline_obj.text
xml_url = outline_obj.xml_url
html_url = outline_obj.html_url
category = outline_obj.category
is_breakpoint = outline_obj.is_breakpoint
created = outline_obj.created
res = FeedCategory(title)
for outline in outline_obj.children:
res.append(self.convert_from_outline(outline))
else:
type = outline_obj.type
title = outline_obj.title or outline_obj.text
xml_url = outline_obj.xml_url
html_url = outline_obj.html_url
text = outline_obj.text
category = outline_obj.category
is_breakpoint = outline_obj.is_breakpoint
created = outline_obj.created
res = self.make_feed(type, title, xml_url, html_url, text,
category, is_breakpoint, created)
return res
def convert_to_outline(self, feed_obj):
res = OutlineElement()
if feed_obj.type == 'category':
res.type = 'category'
res.text = feed_obj.text
res.title = feed_obj.title
res.children = []
for child in feed_obj:
res.children.append(self.convert_to_outline(child))
else:
res.type = feed_obj.rsstype
res.text = feed_obj.text
res.title = feed_obj.title
res.xml_url = feed_obj.xml_url
res.html_url = feed_obj.html_url
return res
def __contains__(self, key):
return key in self.feedlist
def __len__(self):
return len(self.feedlist)
def __iter__(self):
return iter(self.feedlist)
def __getitem__(self, key):
return self.feedlist[key]
def __setitem__(self, key, value):
self.feedlist[key] = value
def __delitem__(self, key):
del self.feedlist[key]
class AlreadyExistException(Exception):
pass
class SaveOpmlError(Exception):
pass