Skip to content

Instantly share code, notes, and snippets.

@rod-dot-codes
Created July 3, 2015 13:39
Show Gist options
  • Save rod-dot-codes/f8987e780cf24dcb73eb to your computer and use it in GitHub Desktop.
Save rod-dot-codes/f8987e780cf24dcb73eb to your computer and use it in GitHub Desktop.
Simple Krakenize built for Django
from django.conf import settings
import requests
import simplejson as json
def krakenize(url, height=None, width=None):
'''
Krakenizes an image url and returns a file object.
@return-example:
b'{"file_name":"P1jX7RP.jpg","original_size":68979,
"kraked_size":65234,"saved_bytes":3745,
"kraked_url":"https://dl.kraken.io/api/b74fe7e276ebf07178f9f1657
cee1f30/P1jX7RP.jpg","success":true}'
'''
params = {
'auth': {
'api_key': settings.KRAKEN_IO_API_KEY,
'api_secret': settings.KRAKEN_IO_API_SECRET,
},
'url': url,
'wait': True
}
if height is not None or width is not None:
if height is not None and width is not None:
params['resize'] = {
'width': height,
'height': width,
'strategy': 'crop'
}
else:
raise Exception("You need to provide a width and height")
url = 'https://api.kraken.io/v1/url'
resp = requests.post(url, data=json.dumps(params), headers={'Content-type': 'application/json', 'Accept': 'text/plain'})
resp_j = json.loads(resp.content)
if resp_j["success"]:
return resp_j["kraked_url"]
else:
raise Exception("Kraken.io returned an error")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment