| commit 7: | 7bd3f4150903 |
| parent 6: | 7af7abde0db7 |
| branch: | default |
| tags: | tip |
Typo, thanks @zyegfryed for the reminder
12 months ago
bgk /
code.py
| r7:7bd3f4150903 | 133 loc | 4.0 KB | embed / history / annotate / raw / |
|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | #!/usr/bin/env python
import web
import shelve
from random import choice
# Settings
SERVICE_URL = 'http://bgk.me/'
SHELVE_FILENAME = "bgkurls.db"
ADMIN = "/admin" # obfuscate it!
POST_REDIRECT_URL = 'http://www.biologeek.com/journal/%s/'
THOUGHT_REDIRECT_URL = 'http://www.biologeek.com/bistrot/%s/'
PHOTO_REDIRECT_URL = 'http://www.biologeek.com/photos/%s/'
# Messages
INDEX_MESSAGE = """<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset=utf-8>
<title>URL shortener administration</title>
</head>
<body>
<header><h1>My own URL shortener that doesn't suck.</h1></header>
<section>
Get your own at <a href="http://code.welldev.org/bgk/"
title="Access mercurial repository">http://code.welldev.org/bgk/</a>.
</section>
</body>
</html>
"""
FAIL_MESSAGE = 'Redirection failed, verify your link...'
# URLs
urls = (
"/", "Index",
ADMIN, "Admin",
ADMIN+"/done/(.*)", "AdminDone",
"/p/(\d+)", "RedirectToPost",
"/t/(\d+)", "RedirectToThought",
"/pic/(\d+)", "RedirectToPhoto",
"/(.*)", "RedirectToOthers",
)
app = web.application(urls, globals())
# Stolen from django.contrib.auth.models
def random_shortcut(length=10,
allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
return ''.join([choice(allowed_chars) for i in range(length)])
class Index:
def GET(self):
return INDEX_MESSAGE
class Admin:
def GET(self):
admin_form = web.form.Form(
web.form.Textbox("url", description="Long URL"),
web.form.Textbox("shortcut",description="Shortcut"),
)
admin_template = web.template.Template("""$def with(form, adminurl)
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset=utf-8>
<title>URL shortener administration</title>
</head>
<body onload="document.getElementById('url').focus()">
<header><h1>Admin</h1></header>
<form method="POST" action="$adminurl">
$:form.render()
<input type="submit" value="Shorten this long URL">
</form>
</body>
</html>
""")
return admin_template(admin_form(), ADMIN)
def POST(self):
data = web.input()
shortcut = str(data.shortcut) or random_shortcut()
storage = shelve.open(SHELVE_FILENAME)
if storage.has_key(shortcut) or not data.url:
response = web.badrequest()
else:
storage[shortcut] = data.url
response = web.seeother(ADMIN+'/done/'+shortcut)
storage.close()
return response
class AdminDone:
def GET(self, short_name):
admin_done_template = web.template.Template("""$def with(new_url)
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset=utf-8>
<title>URL shortener administration</title>
</head>
<body>
<header><h1>Done!</h1></header>
<p>You created: $new_url</p>
<p><a href="http://twitter.com/home?status=$new_url"
title="Tweet it!">Tweet it?</a></p>
</body>
</html>
""")
return admin_done_template(SERVICE_URL+short_name)
class RedirectToPost:
def GET(self, post_id):
return web.redirect(POST_REDIRECT_URL % post_id)
class RedirectToThought:
def GET(self, thought_id):
return web.redirect(THOUGHT_REDIRECT_URL % thought_id)
class RedirectToPhoto:
def GET(self, photo_id):
return web.redirect(PHOTO_REDIRECT_URL % photo_id)
class RedirectToOthers:
def GET(self, short_name):
storage = shelve.open(SHELVE_FILENAME)
short_name = str(short_name) # shelve does not allow unicode keys
if storage.has_key(short_name):
response = web.redirect(storage[short_name])
else:
response = FAIL_MESSAGE
storage.close()
return response
if __name__ == "__main__":
app.run()
|
