Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sharms committed Aug 3, 2010
1 parent e6f3772 commit be8bdfe
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
64 changes: 56 additions & 8 deletions homepage.py
Expand Up @@ -28,13 +28,18 @@
print app
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']):
print "DB disappeared, making a new one"
f = app.open_resource('schema.sql')
db = connect_db()
db.cursor().executescript(f.read())
Expand All @@ -47,20 +52,25 @@ def query_db(query, args=(), one = False):
for idx, value in enumerate(row)) for row in cur.fetchall()]
return (rv[0] if rv else None) if one else rv

def format_datetime(timestamp):
return datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d @ %H:%M')

def populate_database():
init_db()
if data_is_stale():
load_twitter()
load_github()
load_wordpress()
load_picasa()

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"""
last_updated = g.db.cursor().execute('select last_refresh from entries order by last_refresh desc limit 1').fetchone()[0]
if (datetime.now() - last_updated).seconds > 5000:
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():
Expand All @@ -69,25 +79,62 @@ def load_twitter():

for entry in twitter.entries:
print entry
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?)',
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:
print entry
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:
print entry
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_github():
github = feedparser.parse("http://github.com/sharms.atom")
g.db.cursor().execute('DELETE FROM entries WHERE source = "github"')

for entry in github.entries:
print entry
g.db.cursor().execute('INSERT INTO entries VALUES (?, ?, ?, ?, ?, ?)',
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'),
Expand All @@ -108,7 +155,8 @@ def after_request(response):
@app.route('/')
def index():
populate_database()
return render_template('index.html')
results = query_db("select * from entries order by updated desc")
return render_template('index.html', results = results)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=443)
Expand Down
3 changes: 2 additions & 1 deletion schema.sql
Expand Up @@ -3,7 +3,8 @@ drop table if exists entries;
create table entries (
id integer primary key autoincrement,
url string not null,
experpt string not null,
image_url string not null,
excerpt string not null,
source string not null,
updated timestamp not null,
last_refresh timestamp not null);
Expand Down
11 changes: 9 additions & 2 deletions static/sharms.css
@@ -1,2 +1,9 @@
body { font-family: 'Georgia', serif; font-size: 15px; color: #000; }

body { font-family: 'Georgia', serif; font-size: 15px; color: #000;
text-align: left; }
ul { margin-left: 0; }
li { list-style-type: none; margin-left: 0; }
.li-img { vertical-align: text-top; margin-right: 3px; }
#about { background-color: #EEE; }
#about div { padding: 5px; }
#footer { text-align: center; font-size: 70%; font-style: italic; }
#resources li { list-style-type: square; margin-left: 1em; }
44 changes: 43 additions & 1 deletion templates/index.html
@@ -1 +1,43 @@
Big huge tes
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Steven Harms</title>
<link rel="stylesheet" href="http://www.sharms.org/static/reset.css" />
<link rel="stylesheet" href="http://www.sharms.org/static/text.css" />
<link rel="stylesheet" href="http://www.sharms.org/static/960.css" />
<link rel="stylesheet" href="http://www.sharms.org/static/sharms.css" />
</head>
<body>
<div class="container_12">
<div class="grid_12" id="header">
<h1>Steven Harms</h1>
</div>
<div class="clear"></div>
<div class="grid_8" id="rss-updates">
<h2>Activities</h2>
<ul>
{% for result in results %}
<li><img src="{{ result.image_url }}" alt="{{ result.source }}" title="{{ result.source }}" class="li-img" /><i style="font-size: 78%;">{{ result.updated|datetimeformat }}</i> {{ result.excerpt }} <a href="{{result.url}}" style="font-size: 78%">more</a></li>
{% endfor %}
</ul>
</div>
<div class="grid_4" id="about">
<div>
<h2>About</h2>
<p>Steven Harms (sharms) is a open source hacker, system administrator and programmer.</p>
<h2>Resources</h2>
<ul id="resources">
<li><a href="http://www.sharms.org/blog">Blog</a></li>
<li><a href="http://www.github.com/sharms">Github</a></li>
<li><a href="http://www.twitter.com/stevenharms">Twitter</a></li>
</ul>
</div>
</div>
<div class="clear"></div>
<div class="grid_12" id="footer">
<p>(c) 2010 Steven Harms. Powered by <a href="http://flash.pocoo.org">Flask</a>. Icons provided by <a href="http://www.fatcow.com/free-icons/">Fatcow</a></p>
</div>
</div>
</body>
</html>

0 comments on commit be8bdfe

Please sign in to comment.