-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
72 lines (54 loc) · 1.93 KB
/
bot.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import giphypop
import json
import requests
import sys
import time
import tweepy
from keys import *
from tweepy.error import TweepError
from wolf import Wolf
# sys.setdefaultencoding("utf-8")
# authenticate with twitter & wolfram alpha
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# grab all tweets with "#tweetthewolf"
cursor = tweepy.Cursor(api.search, q="tweetthewolf")
for status in cursor.items():
try:
print("TWEET: " + str(status.text))
print("FROM: " + str(status._json["user"]["screen_name"]))
wolf = Wolf(WOLFRAM_KEY, status.text)
print("REQUEST URL: " + wolf.result())
res = wolf.request()["queryresult"]
if res["success"] and res.get("pods"):
answer = ""
for pod in res["pods"]:
if pod["subpods"][0]["plaintext"] and len(pod["subpods"][0]["plaintext"] + answer) < 110:
answer += pod["subpods"][0]["plaintext"] + "\n"
# tag questioner in response
answer = "@" + status._json["user"]["screen_name"] + " " + answer
print("ANSWER: " + answer)
else:
answer = "@" + status._json["user"]["screen_name"] + " " + giphypop.translate("confused").url
api.update_status(status=answer, in_reply_to_status_id = status.id)
#raise TweepError("RequestError: Wolfram|Alpha query was unsuccessful")
except TweepError as err:
print("TweepError: " + str(err))
print()
break
except KeyError as err:
print("KeyError: " + str(err))
continue
else:
try:
api.update_status(status=answer, in_reply_to_status_id = status.id)
except TweepError as err:
print("TweepError: " + str(err))
print()
break
finally:
#print()
pass