Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.2 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Programming > Libraries

    shorten 1.0.1

    Download button

    No screenshots available
    Downloads: 109  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Charlie Liban | More programs
    MIT/X Consortium Lic... / FREE
    June 20th, 2012, 10:19 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    shorten description

    A Python library for generating short URLs

    shorten is a Python module to create your own URL-shortening functionality. It's gevent-safe, so you can use it with Gunicorn and Flask/Django.

    For example, you can create your own little shortening API:

    # For example, run as:
    # gunicorn -w 4 example:app

    import re
    from redis import Redis
    from flask import Flask, request, redirect, url_for, jsonify
    from werkzeug import iri_to_uri
    from shorten import shortener

    app = Flask(__name__)
    app_name = 'short.ur'

    redis = Redis()

    def is_url(string):
     return re.match('http(s){0,1}://.+\..+', string, re.I) is not None

    def to_short_token(key):
     return key.split(to_short_key(''), 1)[1]

    def to_short_key(token):
     return '{0}:key:{1}'.format(app_name, token)

    counter_key = '{0}:counter'.format(app_name)

    shortener = shortener('redis', redis=redis,
     counter_key=counter_key,
     formatter=to_short_key)

    @app.route('/')
    def shorten():
     try:
     url = request.args['u'].strip()
     if not is_url(url):
     return jsonify({'error' : 'not a valid url'})
     else:
     token = to_short_token(shortener.insert(url))
     return jsonify({'short' : url_for('bounce', token=token)})
     except KeyError, e:
     return jsonify({'error' : 'no url to shorten'})

    @app.route('/< token >')
    def bounce(token):
     try:
     token = to_short_key(token)
     return redirect(iri_to_uri(shortener[token]))
     except KeyError:
     return jsonify({'error' : 'no short url found'})

    if __name__ == '__main__':
     app.run('0.0.0.0')



    Product's homepage

    Requirements:

    · Python

      


    TAGS:

    short URL | Python library | Python | short | URL

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM