def generate_short_url(long_url):
 if ((settings.BITLY_LOGIN is None) or (settings.BITLY_API_KEY is None)):
  return ''
 keys = {'format': 'json', 'longUrl': long_url, 'login': settings.BITLY_LOGIN, 'apiKey': settings.BITLY_API_KEY}
 params = urlencode(keys)
 resp = requests.post(settings.BITLY_API_URL, params).json()
 if (resp['status_code'] == 200):
  short_url = resp.get('data', {}).get('url', '')
  return short_url
 elif (resp['status_code'] == 401):
  raise BitlyUnauthorizedException("Unauthorized access to bitly's API")
 elif (resp['status_code'] == 403):
  raise BitlyRateLimitException("Rate limit exceeded while using bitly's API.")
 else:
  raise BitlyException("Error code: {0} recieved from bitly's API.".format(resp['status_code']))

def generate_short_url ( long_url ) : if ( ( settings . BITLY_LOGIN is None ) or ( settings . BITLY_API_KEY is None ) ) : return '' keys = { 'format' : 'json' , 'longUrl' : long_url , 'login' : settings . BITLY_LOGIN , 'apiKey' : settings . BITLY_API_KEY } params = urlencode ( keys ) resp = requests . post ( settings . BITLY_API_URL , params ) . json ( ) if ( resp [ 'status_code' ] == 200 ) : short_url = resp . get ( 'data' , { } ) . get ( 'url' , '' ) return short_url elif ( resp [ 'status_code' ] == 401 ) : raise BitlyUnauthorizedException ( "Unauthorized access to bitly's API" ) elif ( resp [ 'status_code' ] == 403 ) : raise BitlyRateLimitException ( "Rate limit exceeded while using bitly's API." ) else : raise BitlyException ( "Error code: {0} recieved from bitly's API." . format ( resp [ 'status_code' ] ) )
return a shortned url for a given long_url via bitlys api .