Skip to content

Instantly share code, notes, and snippets.

@jmoz
jmoz / pagination.py
Created January 17, 2013 00:03
A Pagination class in Python.
class Pagination(object):
"""A Pagination object to be used for querying and displaying pagination links on frontend
Example usage:
>>> p = Pagination(total=15, per_page=5, current_page=1)
>>> p.start
0
>>> p.pages
[1, 2, 3]
@jmoz
jmoz / redis_sorted_set_jmoz
Created November 30, 2012 12:47
Redis sorted set timeline
redis 127.0.0.1:6379> zrevrange items:sorted-timestamp:ALL:jmoz 0 9 WITHSCORES
1) "Twitter-274182202210799616"
2) "1354205105"
3) "Foursquare-50b768b1e4b0c0274666efa6"
4) "1354197169"
5) "Foursquare-50b68d55e4b060c79257ccaf"
6) "1354141013"
7) "Instagram-334846753149593155_1412856"
8) "1354136865"
9) "Foursquare-50b67b51e4b037d8ee3feff8"
@jmoz
jmoz / foursquare_checkins.py
Created November 28, 2012 00:01
Python foursquare checkins
import foursquare
f = foursquare.Foursquare(access_token=OAUTH_TOKEN)
print f.users.checkins(USER_ID=USER_ID)
@jmoz
jmoz / flask_oauth.py
Created November 27, 2012 16:38
Flask OAuth Twitter
@app.route('/oauth_authorized')
@twitter.authorized_handler
def oauth_authorized(resp):
next_url = request.args.get('next') or url_for('index')
if resp is None:
flash('Authorization denied. Thanks for nothing.', 'error')
return redirect(next_url)
if RedisUser.find(resp['user_id']) is None:
flash('Authorized but unknown user.', 'error')
@jmoz
jmoz / flask_jmoz.py
Created November 27, 2012 16:25
Flask action code for JMOZ
@app.route('/')
def index():
items = client.get_items(config.App.USER_ID, config.App.LIMIT_FRONTPAGE)
return render_template('index.html', items=items)
@jmoz
jmoz / php_httprequest.php
Created November 27, 2012 13:19
HTTPRequest in PHP, requests in Python
<?php
$r = new HttpRequest('http://pinterestapi.co.uk/jwmoz/pins', HttpRequest::METH_GET);
$r->send();
echo $r->getResponseBody();
@jmoz
jmoz / install.sh
Created November 26, 2012 18:59
Bash install script for jmoz.co.uk
#!/bin/bash
. /home/web/.pythonbrew/etc/bashrc
cd `dirname $0`
echo "* Pythonbrew use venv jmoz"
pythonbrew venv use jmoz
echo "* Pip install requirements"
pip install -r reqs
echo "* Supervisor restart gunicorn-jmoz"
supervisorctl restart gunicorn-jmoz
@jmoz
jmoz / git_post-receive_jmoz
Created November 26, 2012 18:57
Git post-receive for jmoz.co.uk
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
echo Update to branch $branch
if [ "master" == "$branch" ]; then
echo "git checkout -f master branch!!!"
unset GIT_DIR
GIT_WORK_TREE=/home/web/sites/jmoz.co.uk git checkout -f
bash /home/web/sites/jmoz.co.uk/install.sh
fi
@jmoz
jmoz / jmoz_supervisor.conf
Created November 26, 2012 18:40
Supervisor configuration for Gunicorn
[program:gunicorn-jmoz]
command=/home/web/.pythonbrew/venvs/Python-2.7.3/jmoz/bin/gunicorn index:app
directory=/home/web/sites/jmoz.co.uk
user=nobody
autostart=true
autorestart=true
redirect_stderr=True
@jmoz
jmoz / jmoz_nginx.conf
Created November 26, 2012 18:33
Nginx configuration for gunicorn
# redirect www to non-www
server {
listen 80;
server_name www.jmoz.co.uk;
rewrite ^ http://jmoz.co.uk$request_uri?;
}
server {
listen 80;
server_name jmoz.co.uk;