Skip to content

Instantly share code, notes, and snippets.

@edma2
Created April 7, 2012 10:18
Show Gist options
  • Save edma2/2327206 to your computer and use it in GitHub Desktop.
Save edma2/2327206 to your computer and use it in GitHub Desktop.
import itertools
import sys
from bottle import route, run, redirect
ALPHANUM = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
def usage():
print 'usage: %s <hostname> <port>'
exit(-1)
if len(sys.argv) != 3:
usage()
hostname = sys.argv[1]
port = int(sys.argv[2])
urls = {}
@route('/')
def index():
return 'Nothing here.'
def codes():
for size in range(1, 4):
for p in itertools.permutations(list(ALPHANUM), size):
yield ''.join(p)
codes = codes()
@route('/get/:url')
def shorten(url):
code = codes.next()
urls[code] = url
return 'Your short url: %s/%s' % (hostname, code)
@route('/:code')
def find(code):
if code not in urls:
return 'Go away.'
url = 'http://' + urls[code]
redirect(url)
run(host=hostname, port=port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment