This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opmlfeed.py
113 lines (87 loc) · 3.52 KB
/
opmlfeed.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
import datetime
import logging
from urllib import unquote
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
import queries
import data
import constants
import misc
import xml.sax.saxutils
import xe
from feed.opml import *
class OPMLHandler(webapp.RequestHandler):
def get(self, u):
username = unquote(u)
rss_token = self.request.get(constants.TOKEN_PARAM_NAME,
default_value=None)
if not rss_token:
u = misc.HTTP_authenticate()
if not u:
self.response.headers['WWW-Authenticate'] = \
'Basic realm=%s' % constants.REALM
self.response.set_status(401)
return
else:
u = queries.getUserByRSSToken(rss_token)
if not u:
self.response.set_status(403)
return
self._generateOPML(u)
def post(self):
self.response.set_status(405)
self.response.headers.add_header('Allow', 'GET')
def put(self):
self.response.set_status(405)
self.response.headers.add_header('Allow', 'GET')
def head(self):
self.response.set_status(405)
self.response.headers.add_header('Allow', 'GET')
def options(self):
self.response.set_status(405)
self.response.headers.add_header('Allow', 'GET')
def delete(self):
self.response.set_status(405)
self.response.headers.add_header('Allow', 'GET')
def trace(self):
self.response.set_status(405)
self.response.headers.add_header('Allow', 'GET')
# -- implementation method below ---
def _generateOPML(self, u):
self.response.headers['Content-Type'] = 'application/opml+xml'
self.response.headers['Content-Disposition'] = 'inline; filename=%s.opml' % u.screen_name
res = queries.loadGroups(u)
tstamp = datetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S GMT")
logging.debug(tstamp)
xmldoc, opml = new_xmldoc_opml()
opml.head.title = "Tweetonica feeds of %s" % u.screen_name
opml.head.date_created = tstamp
opml.head.owner_name = u.screen_name
opml.head.owner_id = "http://twitter.com/%s" % u.screen_name
opml.head._element_names.remove('owner_email')
del(opml.head.__dict__['owner_email'])
lastupdated = datetime.datetime(2009, 1, 1)
for x in res.keys():
rssurl = misc.getGroupRSS_URL(u.screen_name,
u.rss_token,
x,
u.use_HTTP_auth)
outline = Outline("", created=0.0)
outline.attrs["description"] = misc.getGroupRSS_title(u.screen_name, x)
outline.attrs["xmlUrl"] = rssurl
outline.attrs["type"] = "rss"
outline.attrs["version"] = "RSS"
opml.body.append(outline)
if res[x]['memberships_last_updated'] > lastupdated:
lastupdated = res[x]['memberships_last_updated']
opml.head.date_modified = lastupdated.strftime("%a, %d %b %Y %H:%M:%S GMT")
self.response.out.write(unicode(xmldoc))
def main():
logging.getLogger().setLevel(logging.DEBUG)
logging.debug("Starting")
app = webapp.WSGIApplication([('%s/(.+)'%constants.OPML_PATH_PREFIX, \
OPMLHandler)], debug=True)
util.run_wsgi_app(app)
if __name__ == '__main__':
main()