Skip to content

Commit

Permalink
- More chars in random
Browse files Browse the repository at this point in the history
- Even less code lines
- Slightly more speed because of importing all bottle module
- New post request router
- New syntax of wildcard filter <tinchy_id>
  • Loading branch information
bender rodriges committed Dec 15, 2011
1 parent ee092c3 commit 5cfa805
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions tinchy.py
Original file line number Original file line Diff line number Diff line change
@@ -1,20 +1,19 @@
from bottle import route, run, request, redirect import bottle, random, shelve
import random, shelve


def generate_tinchy_id(): def generate_tinchy_id():
return ''.join(random.sample('abcdefghijklmnopqrstuvwxyz1234567890', 3)) return ''.join(random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890', 3))


@route('/') @route('/')
def index(): def index():
return """ return """
<h1>Get your tinchy URL!</h1> <h1>Get your tinchy URL!</h1>
<form method="post"> <form method="post">
<input type="text" name="url" /> <input type="text" name="url" value="http://" />
<input type="submit" value="Make it tinchy!" /> <input type="submit" value="Make it tinchy!" />
</form> </form>
""" """


@route('/', method='POST') @post('/')
def make_tinchy_url(): def make_tinchy_url():
fatty_url = request.POST.get('url', '') fatty_url = request.POST.get('url', '')
if fatty_url[:7] == "http://": if fatty_url[:7] == "http://":
Expand All @@ -24,18 +23,16 @@ def make_tinchy_url():
tinchy_id = generate_tinchy_id() tinchy_id = generate_tinchy_id()
storage[tinchy_id] = fatty_url storage[tinchy_id] = fatty_url
storage.close() storage.close()
tinchy_url = request.url + tinchy_id return 'Your tinchy url is: <a href="%s">%s</a>' % ( request.url + tinchy_id )
return 'Your tinchy url is: <a href="{0}">{0}</a>'.format(tinchy_url)
return 'Boo, <a href="%s">enter a proper url this time</a>.' % request.url return 'Boo, <a href="%s">enter a proper url this time</a>.' % request.url


@route('/:tinchy_id') @route('/<tinchy_id>')
def redirect_to_fatty_url(tinchy_id): def redirect_to_fatty_url(tinchy_id):
storage = shelve.open("tinchy") storage = shelve.open("tinchy")
if tinchy_id not in storage: if tinchy_id not in storage:
storage.close() storage.close()
return "Nope, this tinchy URL wasn't found." return "Nope, this tinchy URL wasn't found."
fatty_url = storage[tinchy_id]
storage.close() storage.close()
redirect(fatty_url) redirect(storage[tinchy_id])


run(host='localhost', port=8080) run(host='localhost', port=8080)

0 comments on commit 5cfa805

Please sign in to comment.