-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrain.py
More file actions
143 lines (115 loc) · 4.17 KB
/
Copy pathbrain.py
File metadata and controls
143 lines (115 loc) · 4.17 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import time
import requests
from tweepy import OAuthHandler
import tweepy
json_data = ""
dataCheck = False
import say_something as TALK
import brain_api as AIBot
consumer_key = '1MUDOQv80dafJg6sMrjr3YRol'
consumer_secret = 'uizOJFjZ8nGmgvCbPLupeQiE5fhjNYFDeqXlNXif7CtJyjIM6H'
access_token = '4864903228-PVG0k3kFy8ojtLMFhUIewkpjUybpPJo1WBVDFdD'
access_secret = '3l7rIzf7bp0twTHHvJm6hKQeW2cY8Z8xBBZQHmJhWVUs0'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
# Gets tweets to @ArmHackathonBot
from tweepy import Stream
from tweepy.streaming import StreamListener, json
class MyListener(StreamListener):
globals()
def on_data(self, dataTwitter):
global dataCheck
text = json.loads(dataTwitter)['text'].replace("@ArmHackathonBot ", "")
isTalkToSteve = "#TalkToSteve" in text
text = text.replace("#PublicOpinion", "")
text = text.replace("#TalkToSteve", "")
name = json.loads(dataTwitter)['user']['name']
nameTwitter = json.loads(dataTwitter)['user']['screen_name']
#get data
if isTalkToSteve:
AIBot.query(text)
else:
processTweet(text,name,nameTwitter)
dataCheck = True
return True
def on_error(self, status):
print(status)
if status == 420 :
return False
time.sleep(0.5)
return True
def begin_streaming():
twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(track=['@ArmHackathonBot','#PublicOpinion','#TalkToSteve'], async=True)
# Search twitter
def searchTwitter(info):
query = info
max_tweets = 500
searched_tweets = []
last_id = -1
while len(searched_tweets) < max_tweets:
count = max_tweets - len(searched_tweets)
try:
new_tweets = api.search(q=query, count=count, max_id=str(last_id - 1), lang='en')
if not new_tweets:
break
searched_tweets.extend(new_tweets)
last_id = new_tweets[-1].id
except tweepy.TweepError as e:
# depending on TweepError.code, one may want to retry or wait
# to keep things simple, we will give up on an error
break
list_tweets = []
for tweet in searched_tweets:
text = tweet.text.replace("RT", "")
list_tweets.append(text)
return list_tweets
# Convert sentiment api output to single + or - value
def convertSentiment(data):
total = 0
for i in data :
if i['result'] == 'Positive':
total = total + 1
elif i['result'] == 'Negative':
total = total - 2
return total
# Input txt list, returns sentiment values as a Json List
def sendRecieveMeaningCloud(tweets):
url = 'http://sentiment.vivekn.com/api/batch/'
data = tweets
r = requests.post(url, data=json.dumps(data))
return r
# input list of tweets, output single value.
def sentimentValue(tweets):
r = sendRecieveMeaningCloud(tweets)
data = json.loads(r.content)
sentiment = convertSentiment(data)
return sentiment
def formJSON(sentimentValue,text, name, nameTwitter):
data = {}
data['sentimentValue'] = sentimentValue
data['text'] = text
data['name'] = name
data['nameTwitter'] = nameTwitter
return json.dumps(data)
def processTweet(text, name, nameTwitter):
value = sentimentValue(searchTwitter(text))
emotion = "unhappy" if (value < 0) else "happy"
adjective = "negative" if (value < 0) else "positive"
TALK.say_message("!%s I have just received a tweet. It appears that on the subject of %s, the public's perception is %s" % (emotion, text, adjective))
# json_data = formJSON(value,text,name,nameTwitter)
def searchTwitterForValue(text):
value = sentimentValue(searchTwitter(text))
emotion = "unhappy" if (value < 0) else "happy"
adjective = "negative" if (value < 0) else "positive"
TALK.say_message("!%s It appears that the public's perception of your question is %s" % (emotion, adjective))
return value
def newData():
global json_data
global dataCheck
if dataCheck :
dataCheck = False
return json_data
else:
return False