-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment+2_Vinit+Shah-DBQuery.py
More file actions
159 lines (116 loc) · 5.21 KB
/
Assignment+2_Vinit+Shah-DBQuery.py
File metadata and controls
159 lines (116 loc) · 5.21 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# coding: utf-8
# In[1]:
#!/usr/bin/python3
import time
import re
import requests
def message_matches(user_id, message_text):
'''
Check if the username and the word 'bot' appears in the text
'''
regex_expression = '.*@' + user_id + '.*bot.*'
regex = re.compile(regex_expression)
# Check if the message text matches the regex above
match = regex.match(message_text)
# returns true if the match is not None (ie the regex had a match)
return match != None
# In[2]:
def extract_name(message_text):
'''
Extract the name. The regex relies on the question having a specific form
'''
regex_expression = 'Show me articles on (.+)'
regex= re.compile(regex_expression)
matches = regex.finditer(message_text)
for match in matches:
return match.group(1)
# if there were no matches, return None
return None
# In[3]:
import MySQLdb as mdb
import sys
con = mdb.connect(host = 'localhost',
user = 'root',
passwd = 'DDOIHHH',
charset='utf8', use_unicode=True);
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM NYTimes2.Articles_Constant")
rows = cur.fetchall()
# In[8]:
def getData(topic):
data = rows
result = [ {"Title": row["title"], "Description": row["Description"],"Entity": row["entity"], "URL": row["url"], "Sentiment": row["sentiment_type"],
"Shares": row["Shares"]}
for row in rows if topic.lower() in row["title"].lower() or topic.lower() in row["entity"].lower() or topic.lower() in row["Description"].lower()
]
return result
# In[5]:
def create_message(username, topic):
'''
This function takes as input the username of the user that asked the question,
and the station_name that we managed to extract from the question (potentially it can be None)
We check the Citibike API and respond with the status of the Citibike stations.
'''
if topic != None:
# We want to address the user with the username. Potentially, we can also check
# if the user has added a first and last name, and use these instead of the username
message = "Thank you @{u} for abusing me. Here is a list of relevant articles on {s}. You will be charged a $5 fee. Please check your bank account now.\n\n".format(u=username, s=topic)
# Let's get the data from the Citibike API
matching = getData(topic)
# If we cannot find any matching station
if len(matching) == 0:
message += "I could not find anything on that topic.\n"
# Add the information for each station
for articles in matching:
Title = articles['Title']
Description = articles['Description']
About = articles['Entity']
Link = articles['URL']
senti = articles['Sentiment']
numShares = articles['Shares']
message += "Title: {a}. \n Description: {f}.\n Entity: {b}.\n Link: {c}\n Article Sentiment: {d}\n Shares: {e}\n\n".format(a=Title, f=Description, b=About, c=Link, d=senti, e=numShares)
else:
message = "Thank you @{u} for asking.".format(u=username)
message += "Unfortunately no else cares about it.\n"
message += "Ask me `Show me articles on ___.` and I will try to answer."
return message
# In[6]:
import json
secrets_file = 'slack_secret.json'
f = open(secrets_file, 'r')
content = f.read()
f.close()
auth_info = json.loads(content)
auth_token = auth_info["access_token"]
bot_user_id = auth_info["user_id"]
from slackclient import SlackClient
sc = SlackClient(auth_token)
# In[ ]:
# Connect to the Real Time Messaging API of Slack and process the events
if sc.rtm_connect():
# We are going to be polling the Slack API for recent events continuously
while True:
# We are going to wait 1 second between monitoring attempts
time.sleep(1)
# If there are any new events, we will get a response. If there are no events, the response will be empty
response = sc.rtm_read()
for item in response:
# Check that the event is a message. If not, ignore and proceed to the next event.
if item.get("type") != 'message':
continue
# Check that the message comes from a user. If not, ignore and proceed to the next event.
if item.get("user") == None:
continue
# Check that the message is asking the bot to do something. If not, ignore and proceed to the next event.
message_text = item.get('text')
if not message_matches(bot_user_id, message_text):
continue
# Get the username of the user who asked the question
response = sc.api_call("users.info", user=item["user"])
username = response['user'].get('name')
# Extract the station name from the user's message
topic = extract_name(message_text)
# Prepare the message that we will send back to the user
message = create_message(username, topic)
# Post a response to the #bots channel
sc.api_call("chat.postMessage", channel="#bots", text=message)