Login

Bit.ly url shortener

Author:
BluePeppers
Posted:
November 29, 2010
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

A small function to convert a url to another shortened via the bit.ly service. Requires a username and password in django settings.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.conf import settings
from urllib import urlencode
from urllib2 import urlopen

def shorten_url(long_url):
     username = settings['BITLY_USERNAME']
     password = settings['BITLY_PASSWORD']
     bitly_url = "http://api.bit.ly/v3/shorten?login={0}&apiKey={1}&longUrl={2}&format=txt"
     req_url = urlencode(bitly_url.format(username, password, long_url)
     short_url = urlopen(req_url).read()
     return short_url

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

rix (on November 29, 2010):

Shouldn't it be: settings.BITLY_USERNAME ?

For more advanced shortening you can check this out: https://github.com/jcfigueiredo/python-bitly

#

Please login first before commenting.