Search code examples
pythonpython-requestspushbullet

Python and pushbullet api: send a file


I'm trying to send a file using Pushbullet following their API docs. This is my function:

def push_file(AccessToken, file_name):
    f = open(file_name, 'rb')
    file_type = mimetypes.guess_type(file_name)[0]

    print("Uploading {0}...".format(file_name))
    try:
        data = {
            'file_name': file_name, 
            'file_type' : file_type
        }

        resp = requests.post(UPLOAD_REQUEST_URL, data=data, auth=(AccessToken, '')).json()
        if resp.get('error') != None:
            print("Error: {0}".format(resp.get('error')['message']))
            return

        file_url = resp.get('file_url')
        print(file_url)
        resp = requests.post(resp.get('upload_url'), data=resp.get('data'), auth=(AccessToken, ''), files={'file': f})

        data = { 
            'type' : 'file', 
            'file_name' : file_name, 
            'file_type' : file_type, 
            'file_url' : file_url, 
            'body' : ''
        }
        resp = requests.post(PUSH_URL, data=data, auth=(AccessToken, '')).json()


    except requests.exceptions.ConnectionError:
        traceback.print_exc()
    f.close()

But I keep getting:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='s3.amazonaws.com', port=443): Max retries exceeded with url: /pushbullet-uploads (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)

If I use another AccessToken I still get this error, even if it's the first time I post to that url.


Solution

  • The upload process is unfortunately not very good and will hopefully be improved soon. It is the one request that does not obey the JSON rule. There is a curl example that shows this (https://docs.pushbullet.com/#upload-request), but understanding curl syntax is basically impossible.

    Here's an example that I just typed up and seems to work:

    import requests
    import json
    
    ACCESS_TOKEN = '<your access token here>'
    resp = requests.post('https://api.pushbullet.com/v2/upload-request', data=json.dumps({'file_name': 'image.jpg'}), headers={'Authorization': 'Bearer ' + ACCESS_TOKEN, 'Content-Type': 'application/json'})
    if resp.status_code != 200:
        raise Exception('failed to request upload')
    r = resp.json()
    resp = requests.post(r['upload_url'], data=r['data'], files={'file': open('image.jpg', 'rb')})
    if resp.status_code != 204:
        raise Exception('failed to upload file')
    print r['file_name'], r['file_type'], r['file_url']