Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Pynozz, Flask-based URL Shortener
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="PousseyWashington" data-source="post: 290900" data-attributes="member: 46132"><p>I recently made a simple URL shortener in Flask.</p><p></p><p>Some of the features include:</p><ul> <li data-xf-list-type="ul">Database rate limiting</li> <li data-xf-list-type="ul">JSON mini-API</li> <li data-xf-list-type="ul">Ban support</li> <li data-xf-list-type="ul">URL previewing</li> </ul><p>Here are a few screenshots and a code preview.</p><p>[SPOILER]</p><p>(keep in mind that I suck at designing and this was made in 5 minutes)</p><p><img src="http://i.imgur.com/UDHWcJo.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><img src="http://i.imgur.com/SAgjQBl.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><img src="http://i.imgur.com/4i3qtkg.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><img src="http://i.imgur.com/Yq08gHB.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>[PHP]@app.route('/api/generate_url', methods=['POST'])</p><p>def generate_url():</p><p> long_url = request.form['url']</p><p> ip = request.remote_addr</p><p></p><p> if check_for_ban(ip) == True:</p><p> return jsonify(success='False', message='You have been banned due to misuse of this service.')</p><p></p><p> if url_for('index', _external=True) in long_url:</p><p> return jsonify(success='False', message='You are not allowed to shorten local links.')</p><p></p><p> if len(long_url) >= 1000:</p><p> return jsonify(success='False', message='Your URL is over 1000 characters.')</p><p></p><p> if not re.match('https?://', long_url) or ' ' in long_url:</p><p> return jsonify(success='False', message='Your URL is invalid.')</p><p></p><p> while True:</p><p> short_url = ''.join(random.choice(string.letters + string.digits) for x in range(4))</p><p> url = check_for_short_url(short_url)</p><p> if url == False: break</p><p></p><p> cursor = conn.cursor()</p><p></p><p> cursor.execute("SELECT COUNT(1) FROM rate_limits WHERE ip = '%s'" % ip)</p><p> if not cursor.fetchone()[0]:</p><p> cursor.execute("INSERT INTO rate_limits (ip) VALUES ('%s')" % ip)</p><p> else:</p><p> cursor.execute("SELECT amount FROM rate_limits WHERE ip = '%s'" % ip)</p><p> amount = cursor.fetchone()[0]</p><p> if amount == 0:</p><p> return jsonify(success='False', message='You have shortened too many URLs today. Try again tomorrow.')</p><p></p><p> cursor.execute("UPDATE rate_limits SET amount = amount -1 WHERE ip = '%s'" % ip)</p><p></p><p> cursor.execute("INSERT INTO urls (long_url, short_url, ip) VALUES ('%s', '%s', '%s')" % (long_url, short_url, ip))</p><p></p><p> conn.commit()</p><p> cursor.close()</p><p></p><p> return jsonify(success='True', url=url_for('index', _external=True) + short_url, message='Your URL was created!')[/PHP]</p><p>[/SPOILER]</p><p></p><p>You can find the source on my <a href="https://github.com/brackson/Pynozz" target="_blank">Github repo</a>. It includes installation information along with the license.</p></blockquote><p></p>
[QUOTE="PousseyWashington, post: 290900, member: 46132"] I recently made a simple URL shortener in Flask. Some of the features include: [LIST] [*]Database rate limiting [*]JSON mini-API [*]Ban support [*]URL previewing [/LIST] Here are a few screenshots and a code preview. [SPOILER] (keep in mind that I suck at designing and this was made in 5 minutes) [IMG]http://i.imgur.com/UDHWcJo.png[/IMG] [IMG]http://i.imgur.com/SAgjQBl.png[/IMG] [IMG]http://i.imgur.com/4i3qtkg.png[/IMG] [IMG]http://i.imgur.com/Yq08gHB.png[/IMG] [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!')[/PHP] [/SPOILER] You can find the source on my [URL='https://github.com/brackson/Pynozz']Github repo[/URL]. It includes installation information along with the license. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Pynozz, Flask-based URL Shortener
Top