Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update twitter status with image using image url in tweepy?

Tags:

This is the code I've used,

#Twitter credentials access_token = config.get('twitter_credentials', 'access_token') access_token_secret = config.get('twitter_credentials', 'access_token_secret') consumer_key = config.get('twitter_credentials', 'consumer_key') consumer_secret = config.get('twitter_credentials', 'consumer_secret')  auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = API(auth)  img = "http://animalia-life.com/data_images/bird/bird1.jpg" api.update_with_media(img, status="Nice one") 

This is the error I'm getting

No such file or directory 

I know that I have to use a file from the local directory with the above command. Is there a way to use a URL while using update_with_media ?

like image 241
karthik.zorfy Avatar asked Jul 31 '15 14:07

karthik.zorfy


People also ask

How do you get a twitter URL for a picture?

Right-click an image you see on the Internet to display a menu. The menu will contain an option that allows you to copy the image's URL. The option's text may read “Copy Link Location,” “Copy Image URL” or “Copy Image Address.”

What is status in Tweepy?

The Status object in Tweepy module contains the information about a status/tweet. Here are the list of attributes in the Status object : created_at : The time the status was posted. id : The ID of the status.


1 Answers

You need use a local file to upload via tweepy. I would suggest using a library like requests to download the file first.

import requests import os   def twitter_api():     access_token = config.get('twitter_credentials', 'access_token')     access_token_secret = config.get('twitter_credentials', 'access_token_secret')     consumer_key = config.get('twitter_credentials', 'consumer_key')     consumer_secret = config.get('twitter_credentials', 'consumer_secret')      auth = OAuthHandler(consumer_key, consumer_secret)     auth.set_access_token(access_token, access_token_secret)     api = API(auth)     return api   def tweet_image(url, message):     api = twitter_api()     filename = 'temp.jpg'     request = requests.get(url, stream=True)     if request.status_code == 200:         with open(filename, 'wb') as image:             for chunk in request:                 image.write(chunk)          api.update_with_media(filename, status=message)         os.remove(filename)     else:         print("Unable to download image")   url = "http://animalia-life.com/data_images/bird/bird1.jpg" message = "Nice one" tweet_image(url, message) 
like image 181
Brobin Avatar answered Oct 06 '22 01:10

Brobin