Reputation: 55
I am trying to create a Twitter bot that tweets a random caption and corresponding image from a json file every 30 minutes. So far I have:
import tweepy
from time import sleep
import random
import json
with open('test.json', 'r') as f:
info = json.load(f)
print('Twitter Bot')
CONSUMER_KEY = 'XXXXXX'
CONSUMER_SECRET = 'XXXXXX'
ACCESS_KEY = 'XXXXXX'
ACCESS_SECRET = 'XXXXXX'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
randomChoice = random.randrange(len(info))
print (info[randomChoice])
api.update_status(status=info[randomChoice])
Json File:
[
{
"text": "hi",
"image": "/image1.jpg"
},
{
"text": "hello",
"image": "/image2.jpg"
}
]
But the tweet only shows: {'text': 'hello', 'image': '/image2.jpg'}. How can I make it so that only the image and text are shown? Also, how do I set it so that this happens every 30 minutes? I am new to programming and would appreciate any help that is provided!
Upvotes: 2
Views: 392
Reputation: 169304
You need something like:
api.update_with_media(info[randomChoice]['image'],
status=info[randomChoice]['text'])
However, you need to be sure either:
To handle running this every 30 minutes, you can use the schedule
module:
import schedule
import time
def job():
print("I'm working...")
# your tweepy code here
schedule.every(30).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
Upvotes: 2