Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
@simonw
simonw / setting-up-sublime-text-2.txt
Created July 11, 2012 12:35
Setting up Sublime Text 2
First, install it from http://www.sublimetext.com/2
Next, install the package control extension from here:
http://wbond.net/sublime_packages/package_control
Installation instructions here: http://wbond.net/sublime_packages/package_control/installation
Restart Sublime, then hit Shift+Apple+P and search for "Package Control: Install Package"
@simonw
simonw / gist:3947083
Created October 24, 2012 16:19
Setting up port forwarding from inside an SSH session
localhost:~$ ssh example.com
ubuntu@example.com:~$
... ooh, I could really do with a port forwarding to something on here ...
Type ENTER TILDE CAPITAL-C
ssh>
Secret SSH shell!
ssh> -L8080:localhost:8080
@simonw
simonw / multiscrape.js
Created May 20, 2013 17:25
var entries = jQuery('div.blogEntry').multiscrape({title: 'h3', abstract: '.abstract', date: '.pubdate'});
jQuery.fn.multiscrape = function(opts) {
var scraped = [];
this.each(function() {
var $this = jQuery(this);
var obj = {};
jQuery.each(opts, function(key, value) {
var val;
if (jQuery.isFunction(value)) {
val = value.apply($this);
} else {
@simonw
simonw / reindenting_middleware.py
Created June 4, 2013 10:48
Because sometimes the fact that template tag indentation screws up the indentation in view source really bugs me. No, we don't run this in production.
import re
leading_tab_re = re.compile('^(\t+)')
class ReindentingMiddleware(object):
def process_response(self, request, response):
return response
if not response['Content-Type'].startswith('text/html'):
return response
content = response.content
lines = content.split('\n')
@simonw
simonw / gist:7000493
Created October 15, 2013 23:53
How to use custom Python JSON serializers and deserializers to automatically roundtrip complex types.
import json, datetime
class RoundTripEncoder(json.JSONEncoder):
DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
def default(self, obj):
if isinstance(obj, datetime.datetime):
return {
"_type": "datetime",
"value": obj.strftime("%s %s" % (
@simonw
simonw / baseconverter.py
Created February 27, 2014 01:23
A class for converting integers to encoded strings and back again.
"""
Convert numbers from base 10 integers to base X strings and back again.
Sample usage:
>>> base20 = BaseConverter('0123456789abcdefghij')
>>> base20.from_int(1234)
'31e'
>>> base20.to_int('31e')
1234
@simonw
simonw / update_pixstar.py
Last active August 29, 2015 13:57
Our pixstar album frame stopped automatically refreshing (it's meant to pull our PhotoRSS feed every 6 hours), so I wrote a script to click the "refresh" button.
"""
1. GET
extract csrf token <input type='hidden' name='csrfmiddlewaretoken' value='1RNvCJoYdwKwtoOAd7kM73sBKJxhzN50' />
2. POST https://www.pix-star.com/accounts/login/
next:/
username:...
password:...
csrfmiddlewaretoken:1RNvCJoYdwKwtoOAd7kM73sBKJxhzN50
- keep the cookies
3. POST album ID to https://www.pix-star.com/album/is2/downloading2/
@simonw
simonw / pixel_gif.py
Created April 9, 2014 07:27
Return a 1x1 pixel gif from Django (e.g. for a dynamic tracking pixel)
from django.http import HttpResponse
PIXEL_GIF_DATA = """
R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
""".strip().decode('base64')
def pixel_gif(request):
return HttpResponse(PIXEL_GIF_DATA, content_type='image/gif')
@simonw
simonw / nginx.conf
Created November 20, 2014 23:38
Configuration for running nginx not-as-root in a local directory (place files to serve in a child folder called "app")
daemon off;
events {
worker_connections 1024;
}
http {
access_log /dev/stdout;
error_log /dev/stderr;
@simonw
simonw / nginx.sh
Created November 25, 2014 00:36
Run nginx and serve the current directory contents (without needing a config file), logging to stdout/stderr
#!/bin/bash
if [ $# -ge 1 ]; then
PORT=$1
else
PORT=8000
fi
CONF_PATH=$(mktemp /tmp/nginx-tmp-config.XXXXXXXX) || { echo "Failed to create temp file"; exit 1; }
cat > $CONF_PATH <<- EOF
daemon off;