PousseyWashington
why did I buy an upgrade
- Apr 29, 2014
- 31
- 12
I recently made a simple URL shortener in Flask.
Some of the features include:
You can find the source on my
Some of the features include:
- Database rate limiting
- JSON mini-API
- Ban support
- URL previewing
(keep in mind that I suck at designing and this was made in 5 minutes)
PHP:
@app.route('/api/generate_url', methods=['POST'])
def generate_url():
long_url = request.form['url']
ip = request.remote_addr
if check_for_ban(ip) == True:
return jsonify(success='False', message='You have been banned due to misuse of this service.')
if url_for('index', _external=True) in long_url:
return jsonify(success='False', message='You are not allowed to shorten local links.')
if len(long_url) >= 1000:
return jsonify(success='False', message='Your URL is over 1000 characters.')
if not re.match('https?://', long_url) or ' ' in long_url:
return jsonify(success='False', message='Your URL is invalid.')
while True:
short_url = ''.join(random.choice(string.letters + string.digits) for x in range(4))
url = check_for_short_url(short_url)
if url == False: break
cursor = conn.cursor()
cursor.execute("SELECT COUNT(1) FROM rate_limits WHERE ip = '%s'" % ip)
if not cursor.fetchone()[0]:
cursor.execute("INSERT INTO rate_limits (ip) VALUES ('%s')" % ip)
else:
cursor.execute("SELECT amount FROM rate_limits WHERE ip = '%s'" % ip)
amount = cursor.fetchone()[0]
if amount == 0:
return jsonify(success='False', message='You have shortened too many URLs today. Try again tomorrow.')
cursor.execute("UPDATE rate_limits SET amount = amount -1 WHERE ip = '%s'" % ip)
cursor.execute("INSERT INTO urls (long_url, short_url, ip) VALUES ('%s', '%s', '%s')" % (long_url, short_url, ip))
conn.commit()
cursor.close()
return jsonify(success='True', url=url_for('index', _external=True) + short_url, message='Your URL was created!')
You can find the source on my
You must be registered for see links
. It includes installation information along with the license.
Last edited: