Login

Bit.ly url shortener

Author:
BluePeppers
Posted:
November 29, 2010
Language:
Python
Version:
Not specified
Tags:
bit.ly rest-api url-shortening
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

Comments

rix (on November 29, 2010):
<p>Shouldn't it be: settings.BITLY_USERNAME ?</p> <p>For more advanced shortening you can check this out: https://github.com/jcfigueiredo/python-bitly</p>

#

Please login first before commenting.