Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using updateStatusWithMedia with in-memory image data #191

Closed
danxshap opened this issue May 6, 2013 · 14 comments
Closed

Using updateStatusWithMedia with in-memory image data #191

danxshap opened this issue May 6, 2013 · 14 comments

Comments

@danxshap
Copy link

danxshap commented May 6, 2013

Back in 2.7, I could make the following call:

t._media_update(settings.TWITTER_SHARE_URL,
    {'media[]': image_data}, status=message)

But the internal _media_update function has since been removed, so I can't upgrade Twython. Any chance it could come back or some other solution for posting in-memory image data could be added?

Or perhaps I'm missing something and there's a way to do this with the latest Twython?

Thanks!

@michaelhelmick
Copy link
Collaborator

You can do

t.post(URL, params={'status': message}, files={'file': open('report.jpg', 'rb')})

That should work!

Sent from my iPhone

On May 6, 2013, at 3:35 PM, Daniel Shapiro notifications@github.com wrote:

Back in 2.7, I could make the following call:

t._media_update(settings.TWITTER_SHARE_URL,
{'media[]': image_data}, status=message)
But the internal _media_update function has since been removed, so I can't upgrade Twython. Any chance it could come back or some other solution for posting in-memory image data could be added?

Or perhaps I'm missing something and there's a way to do this with the latest Twython?

Thanks!


Reply to this email directly or view it on GitHub.

@danxshap
Copy link
Author

danxshap commented May 6, 2013

@michaelhelmick in that case isn't open() looking for a file on the disk? I don't have any files on the disk, I just have image bytes in a variable, and I can't save to the disk for performance reasons.

@michaelhelmick
Copy link
Collaborator

Can you put the bytes into a StringIO?

from PIL import Image
from StringIO import StringIO
i = Image.open(StringIO(my_bytes))

Or do you absolutely only need to pass bytes? There might be a way. But I'm driving and hard to look it up right now haha

Sent from my iPhone

On May 6, 2013, at 3:45 PM, Daniel Shapiro notifications@github.com wrote:

@michaelhelmick in that case isn't open() looking for a file on the disk? I don't have any files on the disk, I just have image bytes in a variable, and I can't save to the disk for performance reasons.


Reply to this email directly or view it on GitHub.

@danxshap
Copy link
Author

danxshap commented May 6, 2013

@michaelhelmick hmm, when I pass a PIL Image like you suggested above, it fails when requests tries to call .read(), which doesn't exist on the PIL image object, and if I try passing just the StringIO I get a "Missing or invalid url parameter" error.

@ryanmcgrath
Copy link
Owner

As in you've tried...

from StringIO import StringIO
from twython import Twython
t = Twython(...)
t.post(URL, params = {'status': message}, files={'file': StringIO(...)})

@danxshap
Copy link
Author

danxshap commented May 6, 2013

@ryanmcgrath Yes sir.

Traceback:
File "/Users/dshap/.virtualenvs/ycharts/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/sites/ycharts/apps/sharing/views.py" in share_twitter
  249.             t.post(settings.TWITTER_SHARE_URL, params={'status': message}, files={'file': StringIO(image_data)})
File "/Users/dshap/.virtualenvs/ycharts/lib/python2.7/site-packages/twython/twython.py" in post
  217.         return self.request(endpoint, 'POST', params=params, files=files, version=version)
File "/Users/dshap/.virtualenvs/ycharts/lib/python2.7/site-packages/twython/twython.py" in request
  209.         content = self._request(url, method=method, params=params, files=files, api_call=url)
File "/Users/dshap/.virtualenvs/ycharts/lib/python2.7/site-packages/twython/twython.py" in _request
  186.                                 retry_after=response.headers.get('retry-after'))

Exception Type: TwythonError at /share/twitter
Exception Value: Twitter API returned a 403 (Forbidden), Missing or invalid url parameter

This is Twython 2.9.1, requests 1.2.0, and requests-oauthlib 0.3.1

@ryanmcgrath
Copy link
Owner

Hmmm, the following seems to post successfully for me (e.g, no 403, etc). Try this perhaps?

from StringIO import StringIO
t.post('/statuses/update', params = {'status': 'Testing'}, files = {
    'file': StringIO(open('sidebar_pic.png').read())
})

@ryanmcgrath
Copy link
Owner

Ah, hold, I see the issue.

@ryanmcgrath
Copy link
Owner

Here we go, this should do what you want. Just worked for me.

from StringIO import StringIO
from twython import Twython

t = Twython(...)
img = open('img_url').read()
t.post('/statuses/update_with_media', params = {'status': 'Testing New Status'}, files = {
    'media': StringIO(img)
    # 'media': ('OrThisIfYouWantToNameTheFile.lol', StringIO(img))
})

There's a larger question of whether we should have this native in the library, but... I wanna sleep on that one I think.

@danxshap
Copy link
Author

danxshap commented May 6, 2013

This above works great - thank you!

@ryanmcgrath
Copy link
Owner

Nice! I'm going to close this for now, but we might want to look at trying to detect if a StringIO object is passed to the media status update method in the future. Let us know if you notice anything else!

@michaelhelmick
Copy link
Collaborator

If you take a look at my "requests-Facebook" library, @ryanmcgrath, I am doing something pretty cool. I'm allowing the user to pass bytes (I think off the top of my head) so they can edit the image and whatnot before hand. In the readme there is an example of how to resize an image then post it to Facebook

Sent from my iPhone

On May 6, 2013, at 5:59 PM, Ryan McGrath notifications@github.com wrote:

Nice! I'm going to close this for now, but we might want to look at trying to detect if a StringIO object is passed to the media status update method in the future. Let us know if you notice anything else!


Reply to this email directly or view it on GitHub.

@ryanmcgrath
Copy link
Owner

I mean, that same thing can be accomplished by... editing the image beforehand, then passing it in via StringIO, no?

@michaelhelmick
Copy link
Collaborator

I think that's what I'm doing in the lib. I'd have to see. Haven't touched in a couple months

Sent from my iPhone

On May 6, 2013, at 7:53 PM, Ryan McGrath notifications@github.com wrote:

I mean, that same thing can be accomplished by... editing the image beforehand, then passing it in via StringIO, no?


Reply to this email directly or view it on GitHub.

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

No branches or pull requests

3 participants