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