Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tjosten committed Aug 28, 2011
0 parents commit 5318503
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
43 changes: 43 additions & 0 deletions push.py
@@ -0,0 +1,43 @@
from flask import Flask, render_template, g, jsonify
import sqlite3
import time

DATABASE = 'push.sqlite'
app = Flask(__name__)

def connect_db():
return sqlite3.connect(DATABASE)

def query_db(query, args=(), one=False):
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

@app.before_request
def before_request():
g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'):
g.db.close()

@app.route('/')
def hello_world():
return render_template('index.html')

@app.route('/push/', methods=['POST'])
def push():
while(1):
push = query_db('select * from push where read = 0 limit 1');
if len(push) is not 0:
g.db.execute('update push set read = 1 where id = %d' % push[0]['id'])
g.db.commit()
return jsonify(key=push[0]['key'], value=push[0]['value'])
time.sleep(0.1)
pass

if __name__ == '__main__':
app.debug = True
app.run()
Binary file added push.sqlite
Binary file not shown.
37 changes: 37 additions & 0 deletions templates/index.html
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>python-push</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
console.log('[document] ready');

initPush();
});

initPush = function() {
console.log('[push] request fired, waiting');
$.post("/push/", { timestamp: (new Date().getTime()/1000) },
function(response) {
console.log('[push] got push!');

switch(response.key) {
case 'msg':
$('#response').append('<h1>'+response.value+'</h1>');
break;
}

initPush();
}, 'json');
};
</script>

</head>
<body>
<div id="response"></div>
</body>
</html>

0 comments on commit 5318503

Please sign in to comment.