def shorten(): from models import Url Url.create_table() try: url = request.json user_url = url['url'].encode('utf-8') existing_url = Url.query.filter_by(original_url=user_url).first() if existing_url: return "http://127.0.0.1:5000/{}".format(existing_url.short_url) else: encoded_url = hashlib.sha1(user_url).hexdigest() record = Url() record.original_url = user_url record.short_url = encoded_url record.save_to_db() return "http://127.0.0.1:5000/{}".format(encoded_url) except Exception as e: raise e return "Error Occured. Try Later"
except: pass return result # Utility functions def base36_encode(num): """ stolen from the Wikipedia example, threw away shit I didn't need """ alphabet = '0123456789abcdefghijklmnopqrstuvwxyz' result = '' while num != 0: num, i = divmod(num, 36) result = alphabet[i] + result return result def base36_decode(stuff): return int(stuff, 36) if __name__ == "__main__": try: Url.create_table() except: pass # Pfft, whatever. app.run(debug=True)