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

  1. Google URL Shortner using Python requests by akshayw39 1 month, 2 weeks ago
  2. Goo.gl URL shortener by aabele 4 years, 9 months ago
  3. int_to_base36 and base36_to_int by simon 6 years, 12 months ago
  4. A custom URL shortening view, for use with rev=canonical by simon 6 years, 2 months ago
  5. Create short URL redirects for site urls. by matt.geek.nz 6 years, 4 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.