forked from robincamille/bot-tutorial-jumpstart-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextbot.py
More file actions
executable file
·59 lines (45 loc) · 1.96 KB
/
Copy pathtextbot.py
File metadata and controls
executable file
·59 lines (45 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/opt/anaconda3/bin/python
# -*- coding: utf-8 -*-
# This bot tweets 7 random jeopardy questions at 7pm each night.
# Questions taken from https://www.reddit.com/r/datasets/comments/1uyd0t/200000_jeopardy_questions_in_a_json_file/
# follow at https://twitter.com/BotJumpstart
# Housekeeping: do not edit
import tweepy, time, random, json
from credentials import *
auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
# What the bot will tweet
filename = open('/Users/tculler/Desktop/bot-tutorial-jumpstart/JEOPARDY_QUESTIONS1.json')
data = json.load(filename)
categories = []
values = []
questions = []
answers = []
air_dates = []
for item in data:
categories.append(item['category'])
values.append(item['value'])
questions.append(item['question'][1:-1])
answers.append(item['answer'])
air_dates.append(item['air_date'])
tweet_tuples = list(zip(categories,values,questions,answers,air_dates))
# over the course of one minute, tweet jeopardy questions, waiting 8 seconds between each
timespan = time.time() + 60 * 1
while time.time() < timespan:
try:
tweet = random.choice(tweet_tuples)
question = tweet[0] + " for " + tweet[1] + "; " + tweet[2]
answer = "ANSWER: \n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n \n" + tweet[3] + "; \n\noriginally aired on " + tweet[4]
print('\n*******************\n')
print(question)
print(answer)
thread_start = api.update_status(status=question)
api.update_status(status=answer, in_reply_to_status_id=thread_start.id)
time.sleep(8)
except:
error_message = "Oop! Looks like Watson encountered an error. We're in real jeopardy now. *heavily distorted final jeopardy music begins playing over the loudspeaker...in the distance: sirens*"
print(error_message)
# api.update_status(status=error_message)
print("\nAll done!")
# To quit early: CTRL+C and wait a few seconds