Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@wannabehero
Copy link

Hi!

You have very nice lib, by the way :)

I've noticed, that it lacks "update_with_media" so I decided to add it.

The usage is quite straightforward:

api.update_with_media(photo, status=text)

photo - is a bytestring like StringIO(file.read()) - it appears to be more convenient for me. If it is a problem, I can change it to a file-like object (or make it optional).

I'm fully ready to add any changes to this solution :)

@monocasual
Copy link

Hi, I've tested your code and I got Error Code 189 from Twitter API. That's because _pack_photo has a wrong body.

The following is a raw-but-working version:

def _pack_photo(photo, status):
    """Takes photo as a bytestring"""
    BOUNDARY = 'Tw3ePy'
    body = []
    body.append('--' + BOUNDARY)
    body.append('Content-Disposition: form-data; name="status"')
    body.append('')
    body.append(status.encode('utf-8'))
    body.append('--' + BOUNDARY)
    body.append('Content-Disposition: form-data; name="media[]"; filename="{}"'.format(photo))
    body.append('Content-Type: application/octet-stream')
    body.append('')
    body.append(open(photo, 'rb').read())
    body.append('--' + BOUNDARY + '--')
    body.append('')
    body = '\r\n'.join(body)

    print body

    # build headers
    headers = {
        'Content-Type': 'multipart/form-data; boundary=Tw3ePy',
        'Content-Length': str(len(body))
    }

    return headers, body

@wannabehero
Copy link
Author

So, you read file inside the function and I'm - outside. That's the key difference :)

@rdenadai
Copy link

I use the code from @spronin and work great!

@Aaron1011
Copy link
Contributor

@spronin: Could you rebase this against master. please?

@Aaron1011
Copy link
Contributor

@spronin: This has been implemented in PR #364

@Aaron1011 Aaron1011 closed this Dec 22, 2013
@wannabehero
Copy link
Author

@Aaron1011 Ok, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants