Esempio n. 1
0
def index():
    if request.method == 'POST':
        thing = request.form.get('url')
        if thing:
            if '://' not in thing:
                thing = 'http://' + thing

            # Verify the URL
            parsed = urlparse(thing)
            if parsed.scheme not in ('http', 'https'):
                return "I only take HTTP or HTTPS URLs, dummy"

            urlhash = hashlib.sha1(thing).hexdigest()
            try:
                url = Url.get(Url.url_hash == urlhash)
            except:
                url = Url()
                url.url = thing
                url.url_hash = urlhash
                url.created = datetime.datetime.now()
                url.save()

                # hokay. got us an ID, let's make a key.
                url.key = base36_encode(url.id)
                url.save()

            return render_template('added.html', short_url="http://{0}/{1}".format(request.headers['host'], url.key))
        else:
            return "You didn't give me shit"
    else:
        return render_template('index.html')