How to save bytes to an image and access it from Bottle
- by Graham Smith
I'm working on an API wrapper for Snapchat using Python and Bottle, but in order to return the file (retrieved by the Python script) I have to save the bytes (returned by Snapchat) to a .jpg file. I'm not quite sure how I will do this and still be able to access the file so that it can be returned. Here's what I have so far, but it returns a 404.
@route('/image')
def image():
username = request.query.username
token = request.query.auth_token
img_id = request.query.id
return get_blob(username, token, img_id)
def get_blob(usr, token, img_id):
# Form URL and download encrypted "blob"
blob_url = "https://feelinsonice.appspot.com/ph/blob?id={}".format(img_id)
blob_url += "&username=" + usr + "×tamp=" + str(timestamp()) + "&req_token=" + req_token(token)
enc_blob = requests.get(blob_url).content
# Save decrypted image
FileUpload.save('/images/' + img_id + '.jpg')
img = open('images/' + img_id + '.jpg', 'wb')
img.write(decrypt(enc_blob))
img.close()
return static_file(img_id + '.jpg', root='/images/')