generate opml for byteflow-pyo-nebula

 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
def _gen_opml(opmldata, flattened=True):
"""
Generate OPML document from pythonic dict-like data
which contains optional head keys (title, dateCreated,
dateModified, ownerName, ownerEmail, ownerId) and
required body key with list of dict-like outlines.
For outline required keys are: type, text, xmlUrl.
Optional one are: description, htmlUrl, language.
Example:
opmldata = {
ownerId = 'http://pyobject.ru/about/',
body = [
dict(text='PyObject', type='pie',
xmlUrl='http://feeds.feedburner.com/pyobject',
language='ru'),
dict(text=u'Adam Gomaa', type='rss',
xmlUrl='http://adam.gomaa.us/blog/feed/',
htmlUrl='http://adam.gomaa.us/',
title=u'Adam Gomaa',
description='204 No Content Blog'),
]
}
Set arg flattened to False if you want to get ETree
istead of xml string.
"""
opml = ET.Element('opml', version='2.0')
head = ET.SubElement(opml, 'head')
for headtag in ('title', 'dateCreated', 'dateModified',
'ownerName', 'ownerEmail', 'ownerId'):
headval = opmldata.get(headtag)
if headval:
subel = ET.SubElement(head, headtag)
subel.text = unicode(headval)
docs = ET.SubElement(head, 'docs')
docs.text = "http://www.opml.org/spec2"
body = ET.SubElement(opml, 'body')
# we will make plain list of subscriptions, no (sub)folders
for outval in opmldata['body']:
outline = ET.SubElement(body, 'outline')
# required attrs
for attr in ('type', 'text', 'xmlUrl'):
outline.set(attr, outval[attr])
# title is not required, but it's value is the same as text have
outline.set('title', outval['text'])
# optional attrs
for attr in ('description', 'htmlUrl', 'language'):
val = outval.get(attr)
if val:
outline.set(attr, val)
if flattened:
return ET.tostring(opml, encoding='utf-8')
else:
return opml
def friends_opml(request):
"""
Generate OPML 2.0 document for friends blogroll
"""
from django.contrib.auth.models import User
blogadmin = User.objects.filter(is_superuser=True)[0]
opmldata = {}
opmldata['ownerName'] = blogadmin.get_full_name()
opmldata['ownerEmail'] = blogadmin.email
opmldata['body'] = []
for friend in FriendBlog.objects.active():
outline = {
'type': 'rss',
'text': friend.name,
'xmlUrl': friend.feed,
}
if friend.link:
outline['htmlUrl'] = friend.link
opmldata['body'].append(outline)
return HttpResponse(_gen_opml(opmldata), content_type='text/xml')