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. Browser-native date input field by kytta 1 month ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 2 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 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.