Deadulus
Deadulus

Reputation: 321

Random tweet with appended image

I have written a twitterbot in python using Tweepy. Here is my code:

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

filename = open ('Shakespeare.txt', 'r')
tweettext = filename.readlines()
filename.close()
randomChoice = random.randrange(len(tweettext))
print (tweettext[randomChoice])
api.update_status(status=tweettext[randomChoice])

This works perfectly whenever I run it from my SSH on my raspberry pi - my tweet updates my Twitter feed. My problem is: I would like to add a picture of Shakespeare (always the same picture) to each random tweet.

One solution is:

file = open('Your_image.png', 'rb')
data = file.read()
r = api.request('statuses/update_with_media', {'status':'Your tweet'}, {'media[]':data})
print(r.status_code)

However, I do not know how to incorporate this into my existing code. Any suggestions would be very much appreciated.

I would also like to randomise my picture upload. Here is the code I found to select a random image from /home/pi:

'def get_random_image_from_folder(folder):
media_list = list()
for dirpath, dirnames, files in os.walk(folder):
for f in files:
media_list.append(os.path.join(dirpath, f))

media = random.choice(media_list)
return media, len(media_list)'

My problem is: how do I code the api status update? The code i entered is here:

'api.update_status(status=tweettext[randomChoice], media=random.choice(media_list))'

However, this produces the following error:

'Traceback (most recent call last): File "Shakebot.py", line 30, in api.update_status(status=tweettext[randomChoice], media=random.choice(media_list)) NameError: name 'media_list' is not defined'

I don't understand because the 'media_list" is defined in the image location.

Upvotes: 2

Views: 1162

Answers (2)

Juan E.
Juan E.

Reputation: 1819

update_with_media is deperecated according to Twitter Developers.

You should use media_upload (Twitter docs; Tweepy docs are outdated, but you can check the code at the github repo), this will return a media_id_string as part of the response on upload, then you can send a tweet with the text and the list with the media_id_string (or strings, you can attach up to 4 images). Here is your code using media_upload:

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

filename = open ('Shakespeare.txt', 'r')
tweettext = filename.readlines()
filename.close()

media_list = list()
response = api.media_upload('Your_image.png')
media_list.append(response.media_id_string)

randomChoice = random.randrange(len(tweettext))
print (tweettext[randomChoice])
api.update_status(status=tweettext[randomChoice], media_ids=media_list)

Upvotes: 3

Vikash Singh
Vikash Singh

Reputation: 14001

If you want to incorporate image upload part in your code. You can do it like this:

file = open('Your_image.png', 'rb')
data = file.read()

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

filename = open ('Shakespeare.txt', 'r')
tweettext = filename.readlines()
filename.close()
randomChoice = random.randrange(len(tweettext))
print (tweettext[randomChoice])
r = api.request('statuses/update_with_media', {'status':tweettext[randomChoice]}, {'media[]':data})
print(r.status_code)

Upvotes: 0

Related Questions