-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLamaEventsScr.py
328 lines (265 loc) · 11.2 KB
/
LamaEventsScr.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
!!!WARNING!!!: This docstrings are basically useless, If you want to change them on documentation, change the LamaeventsScr.rst file manually!
The Program flow is as follows:
1- Connect to MongoDB (MangoLab)
2- Retrieve tweets from twiqs.nl every hour.
3- Call the event detection.
4- Clear the former results from the database.
5- Run the time-to-event estimation module.
6- Write the new results to database, after each time-to-event estimation.
Trouble Shooting:
- The url for twiqs.nl may change from time to time.
- Twiqs.nl may not provide tweets in time. Therefore this particular hour will not be taken into account.
Usage;
You can define this variables as minutes;
-'looptime' is waiting time for every loop
-'requestwait' is waiting time for the next request
All loggings are written to 'LamaEvents.log'
You can see the last loggings with this command line code::
>> tail -F Lamaevents.log
"""
import sys
import time
import random
import smtplib
import pymongo
import logging
import requests
import configparser
from datetime import date, datetime, timedelta
import event_pairs
import calculations
#!HINT! : Change this variables to decide to waiting times (use minutes)
looptime = 1
requestwait = 2
requestloop = int(30/requestwait)
#Logging Configurations;
logging.basicConfig(
format='%(asctime)s, %(levelname)s: %(message)s',
filename='LamaEvents.log',
datefmt='%d-%m-%Y, %H:%M',
level=logging.INFO)
logging.info('Lama Events Script Started')
#Get all the private configurations;
config = configparser.ConfigParser()
config.read('/scratch/fkunneman/lamaevents/oauth.ini')
#MongoLab OAuth;
client_host = config.get('LE_script_db', 'client_host')
client_port = int(config.get('LE_script_db', 'client_port'))
db_name = config.get('LE_script_db', 'db_name')
#user_name = config.get('LE_script_db', 'user_name')
#passwd = config.get('LE_script_db', 'passwd')
#Twiqs OAuth;
user_name2 = config.get('LE_script_twiqs', 'user_name')
passwd2 = config.get('LE_script_twiqs', 'passwd')
#Sends mail to 'toaddrs' about warnings and errors.
def send_mail(msg):
#Mail OAuth;
fromaddr = config.get('LE_script_mail', 'fromaddr')
toaddrs = config.get('LE_script_mail', 'florian') #to address options = hurrial, florrian, antalb, ebasar
username = config.get('LE_script_mail', 'user_name')
password = config.get('LE_script_mail', 'password')
subject = "Lama Events"
message = 'Subject: %s\n\n%s' % (subject, msg)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, message)
server.quit()
#!IDEA! = Add try-except block for the connection part;
#MongoLab Connection;
try:
connection = pymongo.MongoClient(client_host, client_port)
ledb = connection[db_name] #Database
#ledb.authenticate(user_name, passwd)
lecl = ledb.lecl #Collection
logging.info('Connected to DB')
except Exception:
logging.error("Database Connection Failed. Script Stopped.")
send_mail("Lama Events Error : Database Connection Failed. Script Stopped.")
sys.exit("Error : Database Connection Failed!")
pass
ep = event_pairs.Event_pairs("coco_out/","tmp/",f = True,cities = "citylist_all-nl.txt")
logging.info('Event Detection Initialised')
#Get the cookie for twiqs.nl;
s = requests.Session()
r = s.post("http://145.100.58.95/cgi-bin/twitter", data={"NAME":user_name2, "PASSWD":passwd2})
logging.info('Cookie Created')
#Twiqs.nl parameters;
payload = {'SEARCH': 'echtalles', 'DATE': 'yyyymmddhh-yyyymmddhh', 'DOWNLOAD':True, 'SHOWTWEETS':True}
payload_lost = {'SEARCH': 'echtalles', 'DATE': 'yyyymmddhh-yyyymmddhh', 'DOWNLOAD':True, 'SHOWTWEETS':True}
#DATE = <start,end> --> start and end should point to the same hour in order to get tweets about an hour
#!IDEA! = Argparser can be used to get system parameters
#load info on missed files
lost_tweets = []
with open("tmp/lostevents.txt") as f:
lost_tweets = [x.strip() for x in f.readlines()]
f.close()
def RequestTweets(t):
"""
Fetches the tweets from twiqs.nl
Warning = The url may need to be updated from time to time!
"""
try:
output1st = requests.get("http://145.100.58.95/cgi-bin/twitter", params=t, cookies=s.cookies)
# print("output1st",output1st)
except:
print("output1st = false")
output1st = False
return output1st
def event_procedure(d,e=True):
print(d)
print("fetching",d['DATE'],"from twiqs")
#payload['DATE'] = pDate #It will remain same until next hour.
logging.info('Tweet hour : '+ d['DATE'])
#Reset the time reminder;
timereminder = 0
success = True
#Request to Twiqs;
output = False
while not output:
output = RequestTweets(d)
logging.info('First Request Completed')
#Check the cookie;
#withoutcookie = '#user_id\t#tweet_id\t#DATE='+pDate+'\t#SEARCHTOKEN=echtalles\n'
#if output.text[:70] == withoutcookie: #if the cookie doesn't have access right to download the tweets, it will skip this hour.
# logging.warning("Cookie doesn't have access right to download. It skipped the tweets at " + tweethour + '. You have to check your cookie configuration!')
# send_mail("Lama Events Warning : Cookie doesn't have access right to download. It skipped the tweets at " + tweethour)
# continue
#!IDEA! = If the cookie is wrong, write the code(call the relevant method) for getting a new one here.
# else:
# logging.info('Cookie is Fine')
#Check the result of request;
dumpoutput = '#user_id\t#tweet_id\t#date\t#time\t#reply_to_tweet_id\t#retweet_to_tweet_id\t#user_name\t#tweet\t#DATE='+d['DATE']+'\t#SEARCHTOKEN=echtalles\n'
# print("output.text",len(output.text),output.text[-900:])
if output.text[:1000] == dumpoutput: #If there isn't any tweet try the request again for 10 times.
logging.info("There isn't any tweet yet. Starting to request tweets every "+ str(requestwait) +" minutes, maximum "+ str(requestloop) +" times")
#print("dumpoutput")
for i in range(0,requestloop):
output = False
while not output:
time.sleep(60*requestwait) #Wait for the search done at twiqs.nl before the next request
output = RequestTweets(d)
if output.text[:1000] == dumpoutput: #If there isn't any tweet again, it will skip this hour.
# print("skip hour")
logging.info('No tweet at ' + str(i))
#lost_tweets.insert(0,d['DATE'])
#success = False
else:
logging.info('Tweets came on '+ str(i))
# print("tweets collected")
break
else:
logging.info('Tweets are O.K.')
#Check the results one last time if there isn't any tweet send an e-mail;
if output.text[:1000] == dumpoutput: #If there isn't any tweet again, it will skip this hour.
logging.warning('Still there is not any tweet! It skipped the tweets at '+ d["DATE"])
send_mail("Lama Events Warning : There isn't any tweet for this hour from twiqs.nl :" + d["DATE"])
lost_tweets.insert(0,d['DATE'])
print("no tweets last attempt")
success = False
if success:
#Event Detection; (refer to Florian Kunneman for any issue)
EventDicts = ep.detect_events(output.text[:-1],e) # [:-1] = ignoring the last '\n' at the bottom of the file.
if e:
logging.info('Event Detection Completed')
for event in EventDicts:
event['keylist'] = []
for m in event['keyterms']:
#mt = m[0].title() #capitalization
mt = m[0]
event['keylist'].append(mt)
if DeleteFormerEvents:
lecl.remove({ }) #Delete the old events from database
#we should check the free space
logging.info('Former events are deleted from the database')
new_events = calculations.merge_event_sets(EventDicts)
else:
logging.info('Former events are NOT deleted from the database')
form_events = []
for p in lecl.find():
p['date'] = p['date'].date()
form_events.append(p)
print("form events",len(form_events))
if len(form_events) > 0:
merged_events = calculations.merge_event_sets(form_events[:],EventDicts)
merged_events = calculations.merge_event_sets([],merged_events)
else:
merged_events = calculations.merge_event_sets([],EventDicts)
new_events = []
merge_ids = []
for c,event in enumerate(merged_events):
#print(c)
#print(event)
try:
v = lecl.find_one({'_id': event['_id']})
merge_ids.append(event['_id'])
v['keylist'] = event['keylist']
v['score'] = event['score']
v['tweets'] = event['tweets']
except KeyError:
new_events.append(event)
#new_events = merged_events[len(form_events):]
#delete merged events
all_ids = set([x['_id'] for x in form_events])
bad_ids = list(all_ids - set(merge_ids))
for bid in bad_ids:
lecl.remove({'_id' : bid})
for v in new_events: #For every detected event
#TimeToEventEstimation Calculations;
createDate = datetime.now() #TTE Estimation will be added to the current time
randomTTE = random.uniform(0.0, 193.0) #random number for estimation (for now)
hh, premm = divmod(randomTTE, 1)
mm = (60*premm)*0.1
v['Estimation'] = createDate + timedelta(hours=int(hh), minutes=int(mm))
#Convert date formats to datetime format;
#to avoid this error : "bson.errors.InvalidDocument: Cannot encode object: datetime.date(2015, 6, 3)"
v['date'] = datetime.combine(v['date'], datetime.min.time())
if DeleteTweetDetails:
#del v['keyterms']
for i in v['tweets']:
del i['date'], i['date references'], i['text'], i['entities']
else:
#If you don't delete details; convert date formats to datetime format;
for i in v['tweets']:
i['date'] = datetime.combine(i['date'], datetime.min.time())
#Write to database event by event;
try:
lecl.insert(v)
except:
print("cant insert, check database", dir(lecl))
break
if DeleteTweetDetails:
logging.info("Tweet Details Deleted")
logging.info("New Events Written to Database")
#If True, don't contain details of tweets except ids and users. Also don't contain the keyterms of events after keeping them in keylist.
DeleteTweetDetails = False
#If True, delete the former events from mongo db.
DeleteFormerEvents = False
timereminder = 0
while True:
time.sleep(60*looptime) #Check every <looptime> minutes if you are in the next hour.
#Time Calculations;
nowDate = datetime.now()
nowDate_earlier = nowDate - timedelta(hours=1) #Get the previous hour. Because you can get tweets for the last hour from twiqs.nl.
tweethour = nowDate_earlier.strftime("%H:00") #Just for showing off the hour which tweets requested.
nes = nowDate_earlier.strftime("%Y%m%d%H") #'yyyymmddhh' twiqs.nl format.
pDate = nes+'-'+nes #Twiqs.nl needs this format. Start and end time should be the same to retrieve tweets for one hour.
#Every 5th time remind the current situation;
timereminder += 1
if timereminder == 1:
logging.info('Waiting for the next hour')
#Continue waiting if you are in the same hour. Otherwise process the next hour:
if payload['DATE'] == pDate:
if len(lost_tweets) > 0:
payload_lost['DATE'] = lost_tweets.pop()
event_procedure(payload_lost,e=False)
#with open("tmp/lostevents.txt","r") as f:
# lost_tweets.extend([x.strip() for x in f.readlines()])
# lost_tweets = list(set(lost_tweets))
with open("tmp/lostevents.txt","w") as f:
f.write("\n".join(lost_tweets))
f.close()
else:
payload['DATE'] = pDate
event_procedure(payload)