How to save bytes to an image and access it from Bottle

Posted by Graham Smith on Stack Overflow See other posts from Stack Overflow or by Graham Smith
Published on 2013-10-20T21:51:17Z Indexed on 2013/10/20 21:53 UTC
Read the original article Hit count: 259

Filed under:
|

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 + "&timestamp=" + 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/')

© Stack Overflow or respective owner

Related posts about python

Related posts about bottle