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
|
#! /usr/local/bin/python
#-*- coding: utf-8 -*-
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 0.7 $"
__date__ = "$Date: 2010/02/15 $"
__copyright__ = "Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPLv3"
import sqlite3
import hashlib
import cherrypy
import ConfigParser
from datetime import datetime
from cherrypy.lib.static import serve_file
import feedgetter
config = ConfigParser.RawConfigParser()
config.read("./cfg/pyAggr3g470r.cfg")
path = config.get('global','path')
bindhost = "0.0.0.0"
cherrypy.config.update({ 'server.socket_port': 12556, 'server.socket_host': bindhost})
path = { '/css/style.css': {'tools.staticfile.on': True, \
'tools.staticfile.filename':path+'css/style.css'}}
htmlheader = """<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">\n<head>\n<link rel="stylesheet" type="text/css" href="/css/style.css"
/>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n
<title>pyAggr3g470r - RSS Feed Reader</title> </head>"""
htmlfooter = """This software is under GPLv3 license. You are welcome to copy, modify or
redistribute the source code according to the GPLv3 license.</div>
</body></html>"""
htmlnav = """<body><h1><a name="top"><a href="/">pyAggr3g470r - RSS Feed Reader</a></a></h1><a
href="http://bitbucket.org/cedricbonhomme/pyaggr3g470r/" rel="noreferrer" target="_blank">
pyAggr3g470r (source code)</a>
"""
class Root:
def index(self):
"""
Main page containing the list of feeds and articles.
"""
self.dic, self.dic_info = self.load_feed()
html = htmlheader
html += htmlnav
html += """<div class="right inner">\n"""
html += """<a href="/fetch/">Fetch all feeds</a>\n<br />\n"""
html += """<a href="/mark_as_read/All:">Mark all articles as read</a>\n<br />\n"""
html += """<a href="/management/">Management of feed</a>\n"""
html += """<form method=get action="/q/"><input type="text" name="querystring" value=""><input
type="submit" value="Search"></form>\n"""
html += "<hr />\n"
html += """Your feeds (%s):<br />\n""" % len(self.dic.keys())
for rss_feed_id in self.dic.keys():
html += """<a href="/#%s">%s</a> (<a href="/unread/%s"
title="Unread article(s)">%s</a> / %s)<br />\n""" % \
(rss_feed_id.encode('utf-8'), \
self.dic[rss_feed_id][0][5].encode('utf-8'), \
rss_feed_id, self.dic_info[rss_feed_id][1], \
self.dic_info[rss_feed_id][0])
html += """</div>\n<div class="left inner">\n"""
for rss_feed_id in self.dic.keys():
html += '<h2><a name="' + rss_feed_id + '">' + \
'<a href="' + self.dic[rss_feed_id][0][6].encode('utf-8') + \
'" rel="noreferrer" target="_blank">' + \
self.dic[rss_feed_id][0][5].encode('utf-8') + "</a></a></h2>\n"
# The main page display only 10 articles by feeds.
for article in self.dic[rss_feed_id][:10]:
if article[7] == "0":
# not readed articles are in bold
not_read_begin = "<b>"
not_read_end = "</b>"
else:
not_read_begin = ""
not_read_end = ""
html += article[1].encode('utf-8') + \
" - " + not_read_begin + \
"""<a href="/description/%s" rel="noreferrer" target="_blank">%s</a>""" % \
(article[0].encode('utf-8'), article[2].encode('utf-8')) + \
not_read_end + \
"<br />\n"
html += "<br />\n"
html += """<a href="/all_articles/%s">All articles</a>""" % (rss_feed_id,)
html += """ <a href="/mark_as_read/Feed_FromMainPage:%s">Mark all as read</a>""" % (rss_feed_id,)
if self.dic_info[rss_feed_id][1] != 0:
html += """ <a href="/unread/%s" title="Unread article(s)"
>Unread article(s) (%s)</a>""" % (rss_feed_id, \
self.dic_info[rss_feed_id][1])
html += """<h4><a href="/#top">Top</a></h4>"""
html += "<hr />\n"
html += htmlfooter
return html
def management(self):
"""
"""
self.dic, self.dic_info = self.load_feed()
html = htmlheader
html += htmlnav
html += """</div> <div class="left inner">\n"""
html += "<h1>Add Feeds</h1>\n"
html += """<form method=get action="add_feed/"><input type="text" name="v" value="">\n<input
type="submit" value="OK"></form>\n"""
html += "<h1>Delete Feeds</h1>\n"
html += """<form method=get action="del_feed/"><select name="feed_list">\n"""
for feed_id in self.dic.keys():
html += """\t<option value="%s">%s</option>\n""" % \
(feed_id, self.dic[feed_id][0][5].encode('utf-8'))
html += """</select></form>\n"""
html += "<hr />\n"
html += """The database contains a total of %s articles with
%s unread articles.<br /><br />""" % \
(sum([feed[0] for feed in self.dic_info.values()]),
sum([feed[1] for feed in self.dic_info.values()]))
html += """<form method=get action="/fetch/">\n<input
type="submit" value="Fetch all feeds"></form>\n"""
html += """<form method=get action="add_feed/">\n<input
type="submit" value="Delete all articles"></form>\n"""
html += "<hr />\n"
html += htmlfooter
return html
def q(self, querystring=None):
"""
Search for a feed. Simply search for the string 'querystring'
in the description of the article.
"""
param, _, value = querystring.partition(':')
feed_id = None
if param == "Feed":
feed_id, _, querystring = value.partition(':')
html = htmlheader
html += htmlnav
html += """</div> <div class="left inner">"""
html += """<h1>Articles containing the string <i>%s</i></h1><br />""" % (querystring,)
if feed_id is not None:
for article in self.dic[rss_feed_id]:
article_content = article[4].encode('utf-8') + article[2].encode('utf-8')
if querystring.lower() in article_content.lower():
if article[7] == "0":
# not readed articles are in bold
not_read_begin = "<b>"
not_read_end = "</b>"
else:
not_read_begin = ""
not_read_end = ""
html += article[1].encode('utf-8') + \
" - " + not_read_begin + \
"""<a href="/description/%s" rel="noreferrer" target="_blank">%s</a>""" % \
(article[0].encode('utf-8'), article[2].encode('utf-8')) + \
not_read_end
else:
for rss_feed_id in self.dic.keys():
for article in self.dic[rss_feed_id]:
article_content = article[4].encode('utf-8') + article[2].encode('utf-8')
if querystring.lower() in article_content.lower():
if article[7] == "0":
# not readed articles are in bold
not_read_begin = "<b>"
not_read_end = "</b>"
else:
not_read_begin = ""
not_read_end = ""
html += article[1].encode('utf-8') + \
" - " + not_read_begin + \
"""<a href="/description/%s" rel="noreferrer" target="_blank">%s</a>""" % \
(article[0].encode('utf-8'), article[2].encode('utf-8')) + \
not_read_end + """ from <i><a href="%s">%s</a></i><br />\n""" % \
(article[6].encode('utf-8'), article[5].encode('utf-8'))
html += "<hr />"
html += htmlfooter
return html
def fetch(self):
"""
Fetch all feeds
"""
feed_getter = feedgetter.FeedGetter()
feed_getter.retrieve_feed()
return self.index()
def description(self, article_id):
"""
Display the description of an article in a new Web page.
"""
html = htmlheader
html += htmlnav
html += """</div> <div class="left inner">"""
for rss_feed_id in self.dic.keys():
for article in self.dic[rss_feed_id]:
if article_id == article[0]:
if article[7] == "0":
self.mark_as_read("Article:"+article[3]) # update the database
html += """<h1><i>%s</i> from <a href="/all_articles/%s">%s</a></h1><br />""" % \
(article[2].encode('utf-8'), rss_feed_id, article[5].encode('utf-8'))
description = article[4].encode('utf-8')
if description:
html += description
else:
html += "No description available."
html += """<hr />\n<a href="%s">Complete story</a>\n""" % (article[3].encode('utf-8'),)
html += "<hr />\n" + htmlfooter
return html
def all_articles(self, feed_id):
"""
Display all articles of a feed ('feed_title').
"""
html = htmlheader
html += htmlnav
html += """<div class="right inner">\n"""
html += """<a href="/mark_as_read/Feed:%s">Mark all articles from this feed as read</a>""" % (feed_id,)
html += """<br />\n<form method=get action="/q/Feed"><input type="text" name="Feed:%s:querystring" value=""><input
type="submit" value="Search this feed"></form>\n""" % (feed_id,)
html += "<hr />\n"
html += """Your feeds (%s):<br />\n""" % len(self.dic.keys())
for rss_feed_id in self.dic.keys():
html += """<a href="/#%s">%s</a> (<a href="/unread/%s"
title="Unread article(s)">%s</a> / %s)<br />\n""" % \
(rss_feed_id.encode('utf-8'), \
self.dic[rss_feed_id][0][5].encode('utf-8'), \
rss_feed_id, self.dic_info[rss_feed_id][1], \
self.dic_info[rss_feed_id][0])
html += """</div> <div class="left inner">"""
html += """<h1>Articles of the feed <i>%s</i></h1><br />""" % (self.dic[feed_id][0][5].encode('utf-8'))
for article in self.dic[feed_id]:
if article[7] == "0":
# not readed articles are in bold
not_read_begin = "<b>"
not_read_end = "</b>"
else:
not_read_begin = ""
not_read_end = ""
html += article[1].encode('utf-8') + \
" - " + not_read_begin + \
"""<a href="/description/%s" rel="noreferrer" target="_blank">%s</a>""" % \
(article[0].encode('utf-8'), article[2].encode('utf-8')) + \
not_read_end + \
"<br />\n"
html += """\n<h4><a href="/">All feeds</a></h4>"""
html += "<hr />\n"
html += htmlfooter
return html
def unread(self, feed_id):
"""
Display all unread articles of a feed ('feed_title').
"""
html = htmlheader
html += htmlnav
html += """</div> <div class="left inner">"""
html += """<h1>Unread article(s) of the feed <a href="/all_articles/%s">%s</a></h1>
<br />""" % (feed_id, self.dic[feed_id][0][5].encode('utf-8'))
for article in self.dic[feed_id]:
if article[7] == "0":
html += article[1].encode('utf-8') + \
""" - <a href="/description/%s" rel="noreferrer" target="_blank">%s</a>""" % \
(article[0].encode('utf-8'), article[2].encode('utf-8')) + \
"<br />\n"
html += """<hr />\n<a href="/mark_as_read/Feed:%s">Mark all as read</a>""" % (feed_id,)
html += """\n<h4><a href="/">All feeds</a></h4>"""
html += "<hr />\n"
html += htmlfooter
return html
def load_feed(self):
"""
Load feeds in a dictionary.
"""
list_of_articles = None
try:
conn = sqlite3.connect("./var/feed.db", isolation_level = None)
c = conn.cursor()
list_of_articles = c.execute("SELECT * FROM rss_feed").fetchall()
c.close()
except:
pass
# The key of dic is the id of the feed:
# dic[feed_id] = (article_id, article_date, article_title,
# article_link, article_description, feed_title,
# feed_link, article_readed)
# dic_info[feed_id] = (nb_article, nb_article_unreaded)
dic, dic_info = {}, {}
if list_of_articles is not None:
for article in list_of_articles:
sha256_hash = hashlib.sha256()
sha256_hash.update(article[5].encode('utf-8'))
feed_id = sha256_hash.hexdigest()
sha256_hash.update(article[2].encode('utf-8'))
article_id = sha256_hash.hexdigest()
article_list = [article_id, article[0], article[1], \
article[2], article[3], article[4], article[5], article[6]]
if feed_id not in dic:
dic[feed_id] = [article_list]
else:
dic[feed_id].append(article_list)
# sort articles by date for each feeds
for feeds in dic.keys():
dic[feeds].sort(lambda x,y: compare(y[1], x[1]))
for rss_feed_id in dic.keys():
dic_info[rss_feed_id] = (len(dic[rss_feed_id]), \
len([article for article in dic[rss_feed_id] \
if article[7]=="0"]) \
)
return (dic, dic_info)
return (dic, dic_info)
def mark_as_read(self, target):
"""
Mark one (or more) article(s) as read by setting the value of the field
'article_readed' of the SQLite database to 1.
"""
param, _, identifiant = target.partition(':')
try:
conn = sqlite3.connect("./var/feed.db", isolation_level = None)
c = conn.cursor()
# Mark all articles as read.
if param == "All":
c.execute("UPDATE rss_feed SET article_readed=1")
# Mark all articles from a feed as read.
elif param == "Feed" or param == "Feed_FromMainPage":
c.execute("UPDATE rss_feed SET article_readed=1 WHERE feed_site_link='" + self.dic[identifiant][0][6] + "'")
# Mark an article as read.
elif param == "Article":
c.execute("UPDATE rss_feed SET article_readed=1 WHERE article_link='" + identifiant + "'")
conn.commit()
c.close()
except Exception, e:
pass
self.dic, self.dic_info = self.load_feed()
if param == "All" or param == "Feed_FromMainPage":
return self.index()
elif param == "Feed":
return self.all_articles(identifiant)
index.exposed = True
management.exposed = True
fetch.exposed = True
q.exposed = True
description.exposed = True
all_articles.exposed = True
mark_as_read.exposed = True
unread.exposed = True
def compare(stringtime1, stringtime2):
"""
Compare two dates in the format 'yyyy-mm-dd hh:mm:ss'.
"""
date1, time1 = stringtime1.split(' ')
date2, time2 = stringtime2.split(' ')
year1, month1, day1 = date1.split('-')
year2, month2, day2 = date2.split('-')
hour1, minute1, second1 = time1.split(':')
hour2, minute2, second2 = time2.split(':')
datetime1 = datetime(year=int(year1), month=int(month1), day=int(day1), \
hour=int(hour1), minute=int(minute1), second=int(second1))
datetime2 = datetime(year=int(year2), month=int(month2), day=int(day2), \
hour=int(hour2), minute=int(minute2), second=int(second2))
if datetime1 < datetime2:
return -1
elif datetime1 > datetime2:
return 1
else:
return 0
if __name__ == '__main__':
# Point of entry in execution mode
root = Root()
cherrypy.quickstart(root, config=path)
|