-
Notifications
You must be signed in to change notification settings - Fork 9
/
homepage.py
173 lines (138 loc) · 5.45 KB
/
homepage.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
# -*- coding: utf-8 -*-
"""
HomePage
--------
How my homepage reads RSS feeds and puts them in one place
:copyright: (c) 2010 by Steven Harms
:license: BSD
"""
from flask import Flask,request,render_template,abort,g
from flask import g
import sqlite3
import time
import os.path
import feedparser
from datetime import datetime
DATABASE = '/var/tmp/sharms-homepage-cache.sqlite'
DEBUG = False
SECRET_KEY = 'ksd241241kndkndk1ndk123442ievjfiee'
app = Flask(__name__)
app.config.from_object(__name__)
@app.template_filter('datetimeformat')
def datetimeformat(value, format='%Y-%m-%d %H:%M'):
return value.strftime(format)
def connect_db():
"""Returns a new connection to the sqlite database"""
return sqlite3.connect(app.config['DATABASE'], detect_types=sqlite3.PARSE_DECLTYPES)
def init_db():
"""Create the database if it doesn't exist"""
if not os.path.isfile(app.config['DATABASE']):
app.logger.debug('DB disappeared, making a new one')
f = app.open_resource('schema.sql')
db = connect_db()
db.cursor().executescript(f.read())
db.commit()
def query_db(query, args=(), one = False):
"""Query database returning dictionary"""
cur = g.db.execute(query, args)
rv = [dict((cur.description[idx][0], value)
for idx, value in enumerate(row)) for row in cur.fetchall()]
return (rv[0] if rv else None) if one else rv
def populate_database():
init_db()
if data_is_stale():
load_twitter()
load_github()
load_wordpress()
load_picasa()
load_delicious()
def data_is_stale():
"""Find the last entry in the sqlite database to determine if we need to
refresh the data. This stops us from pulling them each request"""
try:
last_updated = g.db.cursor().execute('select last_refresh from entries order by last_refresh desc limit 1').fetchone()[0]
except:
return True
if not last_updated or (datetime.now() - last_updated).seconds > 10800:
return True
return False
def load_twitter():
twitter = feedparser.parse("http://twitter.com/statuses/user_timeline/14377703.rss")
g.db.cursor().execute('DELETE FROM entries WHERE source = "twitter"')
for entry in twitter.entries:
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?, ?)',
(None,
entry['link'],
"http://www.sharms.org/static/twitter_1.png",
entry['summary'],
"twitter",
datetime.strptime(entry['updated'][:-6], '%a, %d %b %Y %H:%M:%S'),
datetime.now()))
g.db.commit()
def load_picasa():
picasa = feedparser.parse("http://picasaweb.google.com/data/feed/base/user/thisdyingdream/albumid/5501252408388252785?alt=rss&kind=photo&hl=en_US")
g.db.cursor().execute('DELETE FROM entries WHERE source = "picasa"')
for entry in picasa.entries:
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?, ?)',
(None,
entry['link'],
"http://www.sharms.org/static/picture.png",
entry['media_description'],
"picasa",
datetime.strptime(entry['updated'][:-6], '%Y-%m-%dT%H:%M:%S'),
datetime.now()))
g.db.commit()
def load_wordpress():
wordpress = feedparser.parse("http://www.sharms.org/blog/feed/")
g.db.cursor().execute('DELETE FROM entries WHERE source = "wordpress"')
for entry in wordpress.entries:
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?, ?)',
(None,
entry['link'],
"http://www.sharms.org/static/wordpress.png",
entry['title'],
"wordpress",
datetime.strptime(entry['updated'][:-6], '%a, %d %b %Y %H:%M:%S'),
datetime.now()))
g.db.commit()
def load_delicious():
delicious = feedparser.parse("http://feeds.delicious.com/v2/rss/stevenharms?count=10")
g.db.cursor().execute('DELETE FROM entries WHERE source = "delicious"')
for entry in delicious.entries:
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?, ?)',
(None,
entry['link'],
"http://www.sharms.org/static/page_white_link.png",
entry['title'],
"wordpress",
datetime.strptime(entry['updated'][:-6], '%a, %d %b %Y %H:%M:%S'),
datetime.now()))
g.db.commit()
def load_github():
github = feedparser.parse("http://github.com/sharms.atom")
g.db.cursor().execute('DELETE FROM entries WHERE source = "github"')
for entry in github.entries:
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?, ?)',
(None,
entry['link'],
"http://www.sharms.org/static/cog.png",
entry['title'],
"github",
datetime.strptime(entry['updated'][:-6], '%Y-%m-%dT%H:%M:%S'),
datetime.now()))
g.db.commit()
@app.before_request
def before_request():
init_db()
g.db = connect_db()
@app.after_request
def after_request(response):
g.db.close()
return response
@app.route('/')
def index():
populate_database()
results = query_db("select * from entries order by updated desc")
return render_template('index.html', results = results)
if __name__ == '__main__':
app.run()