-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
executable file
·215 lines (165 loc) · 6.3 KB
/
code.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import os
import feed
import web
import webopenid
# Chdir into this module file's directory
moduledir = os.path.dirname(__file__)
if moduledir:
os.chdir(moduledir)
# Data path to store this app's db file, fecthed feed entries
datapath = 'data'
if not os.path.exists(datapath):
os.makedirs(datapath)
# Webpy config
web.config.debug = True
# Webpy app instance
app = web.auto_application()
# Webpy database
db = web.database(dbn='sqlite', db=os.path.join(datapath, 'feed2mobi.db'))
# Webpy template
def pass_auth():
'''Ref auth_info_processor'''
return web.ctx.get('account_id') and web.ctx.get('account_actived')
render = web.template.render(base='layout',
globals={'openid_form': webopenid.openid_form,
'pass_auth': pass_auth})
# Feed manager
mgr = feed.FeedManager(db, datapath=datapath)
# ----------------------------------------
# OpenID service
# ----------------------------------------
web.config.session_parameters.cookie_name = "openid_session_id"
session = web.session.Session(app, web.session.DiskStore('data/sessions'))
webopenid.session = session
import openid.store.filestore
webopenid.store = openid.store.filestore.FileOpenIDStore('data/store')
app.add_mapping(r'/openid', 'webopenid.host')
def auth_info_processor(handler):
oid = webopenid.status()
if oid:
account_id, account_actived = mgr.account(oid)
web.ctx.account_id = account_id
web.ctx.account_actived = account_actived
return handler()
app.add_processor(auth_info_processor)
def require_auth(fn):
def new_func(*args, **kws):
if web.ctx.get('account_id'):
if web.ctx.get('account_actived'):
return fn(*args, **kws)
else:
raise web.forbidden('Account has been locked')
else:
raise web.seeother(web.ctx.home + web.http.url('/'))
return new_func
# ----------------------------------------
# Bizz web handlers
# ----------------------------------------
class top(app.page):
path='^/(?:top)?(?:\?o=[0-9]+)?$'
pagesize = 15
def GET(self):
i = web.input()
offset = int(i.get('o', 0))
feeds = mgr.list(account=web.ctx.get('account_id'),
offset=offset, limit=self.pagesize+1)
feeds, prevOffset, nextOffset = self.calcPageData(feeds, offset)
return render.list(feeds, prevOffset, nextOffset, self.page())
def page(self):
return ('top' if web.ctx.path=='/' else web.ctx.path[1:])
def calcPageData(self, feeds, offset):
return (feeds if len(feeds) <= self.pagesize else feeds[:-1],\
None if offset == 0 else offset-self.pagesize,\
None if len(feeds) <= self.pagesize else offset+self.pagesize)
class new(top):
path='^/new(?:\?o=[0-9]+)?$'
def GET(self):
i = web.input()
offset = int(i.get('o', 0))
feeds = mgr.listNew(web.ctx.get('account_id'),
offset=offset, limit=self.pagesize+1)
feeds, prevOffset, nextOffset = self.calcPageData(feeds, offset)
return render.list(feeds, prevOffset, nextOffset, self.page())
class subscribed(top):
path='^/subscribed(?:\?o=[0-9]+)?$'
@require_auth
def GET(self):
i = web.input()
offset = int(i.get('o', 0))
feeds = mgr.listSubscribed(web.ctx.get('account_id'),
offset=offset, limit=self.pagesize+1)
feeds, prevOffset, nextOffset = self.calcPageData(feeds, offset)
return render.list(feeds, prevOffset, nextOffset, self.page())
class subscribe(app.page):
path='^/subscribe/([0-9]+)$'
@require_auth
def GET(self, feed):
mgr.subscribe(feed, web.ctx.get('account_id'))
raise web.seeother(web.ctx.home + web.http.url('/subscribed'))
class subscribe1(app.page):
path='^/subscribe$'
@require_auth
def POST(self):
i = web.input()
mgr.subscribe(i.feed, web.ctx.get('account_id'))
raise web.seeother(web.ctx.home + web.http.url('/subscribed'))
class unsubscribe(app.page):
path='^/unsubscribe/([0-9]+)$'
@require_auth
def GET(self, feed):
mgr.unsubscribe(feed, web.ctx.get('account_id'))
raise web.seeother(web.ctx.home + web.http.url('/subscribed'))
class delivery(app.page):
@require_auth
def GET(self):
delivery = list(db.select(['account'],
what='delivery_hour hour,delivery_address address,delivery_actived actived,delivery_bundle bundle',
where='id=$account',
vars={'account': web.ctx.get('account_id')}))
return render.delivery(delivery[0])
@require_auth
def POST(self):
i = web.input()
actived = int(i.get('actived',0))
hour = int(i.get('hour', 8))
bundle = int(i.get('bundle', 20))
address = i.get('address', None)
if address:
address = address.strip()
db.update('account',
where='id=$account',
delivery_actived = actived,
delivery_hour = hour,
delivery_address = address,
delivery_bundle = bundle,
vars={'account': web.ctx.get('account_id')})
raise web.seeother(web.ctx.home + web.http.url('/delivery'))
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser('Usage: %prog [options]')
parser.add_option('--update', dest='update', action='store_true',
help='Update contents of feeds')
parser.add_option('--kindlegen', dest='kindlegen', action='store_true',
help='Run kindlegen and send files')
options, args = parser.parse_args()
if options.update or options.kindlegen:
if options.update:
mgr.update()
else:
# cron job always runs before delivery hour
hour = datetime.datetime.now().hour + 1
if hour > 23:
hour = 0
mgr.kindlegen(hour)
else:
app.run()
# uWSGI requires this variable
application = app.wsgifunc()
# Local Variables: **
# comment-column: 56 **
# indent-tabs-mode: nil **
# python-indent: 4 **
# End: **