-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add statuses/update_with_media feature #364
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
Conversation
|
@vmeyet: This looks great! @joshthecoder: Does this look good to you? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vmeyet: I think you forgot to define text here. Did you mean to write tweet_text?
|
Yes looks fine. We can just fix the test locally and push it manually. |
Add statuses/update_with_media feature
|
@Aaron1011 @joshthecoder, thanks for taking care ! |
|
Could you show an example of updating with an object rather than a local file? I am hoping to be able to encode images I scrape and update them to a Twitter account. Thanks. |
|
@petulla the problem is the For my use I made a workaround, by monkey patching
May be it worth opening another issue, and to make a PR: there are some refacto needed with |
|
I had something similar but couldn't get it to work; your fix is also throwing an error for me. Any ideas? Here's the code: image = value[0] Error: TypeError: must be string or buffer, not cStringIO.StringI |
|
oups made a typo: |
|
The encoding seems to be working. Still throwing an error, though. Indeed pack_image is unhappy. ''' 707 raise TweepError('File is too big, must be less than 700kb.') TweepError: Unable to access file''' |
|
of course, you need to monkey patch the |
|
Do you mind breaking out how to do that? I have never done that before. |
|
the basic idea is: import base64
import requests
import tweepy
from cStringIO import StringIO
from tweepy.binder import bind_api
def _pack_image_from_url(url, max_size, form_field="image", file_type='image/png'):
#
# Checks for the url validity goes there
base64_encoded = base64.b64encode(StringIO(requests.get(url).content).read())
BOUNDARY = 'Tw3ePy Pa7cH3d'
body = []
body.append('--' + BOUNDARY)
body.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (form_field, url))
body.append('Content-Type: %s' % file_type)
body.append('Content-Transfer-Encoding: base64')
body.append('')
body.append(base64_encoded)
body.append('--' + BOUNDARY + '--')
body.append('')
body = '\r\n'.join(body)
headers = {
'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY,
'Content-Length': str(len(body))
}
return headers, body
def update_with_media_url(self, url, *args, **kwargs):
headers, post_data = _pack_image_from_url(url, 3072, form_field='media[]')
kwargs.update({'headers': headers, 'post_data': post_data})
return bind_api(
path='/statuses/update_with_media.json',
method = 'POST',
payload_type='status',
allowed_param = [
'status', 'possibly_sensitive', 'in_reply_to_status_id', 'lat', 'long',
'place_id', 'display_coordinates'
],
require_auth=True
)(self, *args, **kwargs)
def patch_tweepy():
tweepy.API.update_with_media_url = update_with_media_url
patch_tweepy()
# Now you can do
tweepy.API.update_with_media_url('http://mypic.jpg', status='my status')But you should definitely open an issue for the tweepy lib to support the feature in the future |
|
@Aaron1011 pretty cool how you are reactive. Thanks for that ! |
|
Hi I tried using the patch today but still aren't having any luck. I upgraded Tweepy to 2.3, which I assume has the file improvement. I'm using the following code. Statustext and twitter_api are already defined. Do I need to pass in the type rather than the filename? I'm a bit confused by your documentation here: #422 #this is our url, assigned to variable image
image = {an image url}
#get the filename from the url
imagename = image.split('/')[-1].split('#')[0].split('?')[0]
#convert the url to an encoded image format
imagere = StringIO(requests.get(image).content).read()
imagefile = base64.b64encode(imagere)
#here is my argument. Where am I going wrong?
twitter_api.update_with_media(file=imagefile, filename=imagename, status=statustext) |
|
@petulla: Sorry, I should have made it more clear.
|
See https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media
implement support for the update_with_media feature + test.
This code is based on another pull request (#180) but that PR is not compliant with the v1.1 of the Twitter api.
very small code added.
See discussion on issue #119