-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlNeedsWants.py
723 lines (559 loc) · 30.2 KB
/
atlNeedsWants.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
from twython import Twython, TwythonError
from threading import Timer
import time
from secrets import *
from random import randint
import nltk
from nltk.corpus import PlaintextCorpusReader
from nltk.corpus import cmudict
from nltk.corpus import stopwords
import curses
from curses.ascii import isdigit
from math import exp
import csv
import datetime
import sys
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
adjectives = [
"another",
"new",
"your",
"my",
"better"
]
articles = ["a", "an", "the"]
noun = ["NN", "NNP", "NNS"]
ignoreWords = [
"the",
"atlanta",
"georgia",
"atl",
"we",
"i",
"they",
"rt",
"https",
"&",
"amp",
"today",
"day",
"tomorrow",
"tonight",
"n't",
"'re",
"'ll"]
month2Num = {
'Jan' : "01",
'Feb' : "02",
'Mar' : "03",
'Apr' : "04",
'May' : "05",
'Jun' : "06",
'Jul' : "07",
'Aug' : "08",
'Sep' : "09",
'Oct' : "10",
'Nov' : "11",
'Dec' : "12"
}
#holds data for all runs within a day. Reset every day
wantNeedDictionaries = [] #collective data for wants and needs and word count together
wantDictionaries = [] #collective data for wants and number of times a want is used
needDictionaries = [] #collective data for needs and number of times a need is used
timesRun = 0 #number of times run in the current day
dayCount = 0 #number of days run
needs = {} #data for needs and number of times a need is used for this day
wants = {} #data for wants and number of times a want is used for this day
numTweetsUsed = 0
wantNeed = {} #data for wants and needs and word count together
wordCount = {} #holds data for all the words used in the tweets. The code that uses this is commented out since we are looking at wants and needs only
wantLines = [] #lines to append to wantsList csv
needLines = [] #lines to append to needsList csv
latestTweetID = 0 #store the id of the latest tweet pulled so we know where to pick up from where we were
def wordIsOK(word):
"""
Checks if a word is worth looking at (it's not a stopword or punctuation, etc.)
"""
stop = stopwords.words('english')
if (word.lower() not in stop) and (word[:7] != "//t.co/"):
if (word.strip(".,:;'()-=+&!@#$%?/\[]{}^*_\"`~") != ""):
if (len(word.strip(".,:;'()-=+&!@#$%?/\[]{}^*_\"")) != 1):
if (word.lower() not in ignoreWords):
return True
return False
def getGeocodeTweetWordCount():
"""
Returns a dictionary of words used in tweets of all the users in the user list and the
stats of each word (number of times word used and the users who used that word)
"""
global needs
global wants
global numTweetsUsed
global wantNeed
global wordCount
global latestTweetID
weekAgo = (datetime.datetime.now() - datetime.timedelta(days=7)).strftime("%Y-%m-%d") #the date from a week ago (we'll pull tweets til we hit this date)
weekAgoHit = False #tells us if we hit the weekAgo date and can stop
earliestDate = "" #holds the date of the earliest tweet on the current page we're pulling from
earliestID = 0 #Id number of the earliest tweet we've pulled from the page
idList = [] #store IDs of tweets we've already used
try:
tweets = twitter.search(q = "", count = 100, geocode = "33.7490,-84.3880,20mi" ) #Get 100 latest tweets with this geocode (atlanta lat long)
except:
time.sleep(15)
tweets = twitter.search(q = "", count = 100, geocode = "33.7490,-84.3880,20mi" )
firstTweetTime = tweets["statuses"][0]['created_at'][11:19]
print(firstTweetTime)
latestTweetID = tweets["statuses"][0]['id']
#find out when was half an hour ago
minute = int(firstTweetTime[3:5])
if minute < 30:
hourAgo = [int(firstTweetTime[:2])-1, 60 - (30 - minute)]
else:
hourAgo = [int(firstTweetTime[:2]), minute - 30]
hourAgoHit = False;
for tweet in tweets["statuses"]: #For each tweet that we have gotten
if tweet['text'].encode('utf8').decode('utf8')[:2] != "RT":
tweetDate = tweet['created_at'][-4:] + "-" + month2Num[tweet['created_at'][4:7]] + "-" + tweet['created_at'][8:10] #Get its date
# if (int(tweetDate[:4]) < int(weekAgo[:4])): #Check if it's been written within
# print("Hitting year if statement\n") #the past week
# weekAgoHit = True
# if (int(tweetDate[5:7]) < int(weekAgo[5:7]) and int(tweetDate[:4]) <= int(weekAgo[:4])):
# print("hitting month if statement\n")
# weekAgoHit = True
# if (int(tweetDate[8:10]) < int(weekAgo[8:10]) and int(tweetDate[5:7]) <= int(weekAgo[5:7]) and int(tweetDate[:4]) <= int(weekAgo[:4])):
# print("hitting day if statement \n" + weekAgo + " " + tweetDate)
# weekAgoHit = True
# if not weekAgoHit: #If it's within the week
# earliestDate = tweetDate #Make it the earliest tweet that we've looked at so far
# earliestID = tweet['id']
# idList.append(tweet['id'])
# wordCount = updateWordCount(tweet, wordCount) #Add its words to the wordCount dictionary
# numTweetsUsed = numTweetsUsed + 1
# else:
# break #Break out of the loop if we've gone past a week
#check if we've gone past half an hour
tweetTime = tweet['created_at'][11:19]
print(tweetTime)
if int(tweetTime[:2]) <= hourAgo[0] and int(tweetTime[3:5]) <= hourAgo[1]:
hourAgoHit = True;
elif tweetTime[:2] == "23" and hourAgo[0] == 0 and int(tweetTime[3:5]) <= hourAgo[1]:
hourAgoHit = True;
if not hourAgoHit:
earliestDate = tweetDate #Make it the earliest tweet that we've looked at so far
earliestID = tweet['id']
idList.append(tweet['id'])
wordCount = updateWordCount(tweet, wordCount) #Add its words to the wordCount dictionary
numTweetsUsed = numTweetsUsed + 1
else:
break
# weekAgoHit = True
numPages = 1
try: #repeat the process, but with earlier tweets
# while not weekAgoHit and numPages <= 50:
while not hourAgoHit:
tweets = twitter.search(q = "", count = 100, geocode = "33.7490,-84.3880,20mi", max_id = earliestID) #get tweets made earlier than earliestID
print("\n\n\n\n\n")
for tweet in tweets["statuses"]:
try:
if tweet['id'] not in idList and tweet['text'].encode('utf8').decode('utf8')[:2] != "RT":
tweetDate = tweet['created_at'][-4:] + "-" + month2Num[tweet['created_at'][4:7]] + "-" + tweet['created_at'][8:10]
# if (int(tweetDate[:4]) < int(weekAgo[:4])):
# print("Hitting year if statement\n")
# weekAgoHit = True
# if (int(tweetDate[5:7]) < int(weekAgo[5:7]) and int(tweetDate[:4]) <= int(weekAgo[:4])):
# print("hitting month if statement\n")
# weekAgoHit = True
# if (int(tweetDate[8:10]) < int(weekAgo[8:10]) and int(tweetDate[5:7]) <= int(weekAgo[5:7]) and int(tweetDate[:4]) <= int(weekAgo[:4])):
# print("hitting day if statement \n" + weekAgo + " " + tweetDate)
# weekAgoHit = True
# if not weekAgoHit:
# earliestDate = tweetDate
# earliestID = tweet['id']
# idList.append(tweet['id'])
# wordCount = updateWordCount(tweet, wordCount)
# numTweetsUsed = numTweetsUsed + 1
# else:
# break
#check if we've gone past half hour mark
tweetTime = tweet['created_at'][11:19]
print(tweetTime)
if int(tweetTime[:2]) <= hourAgo[0] and int(tweetTime[3:5]) <= hourAgo[1]:
hourAgoHit = True;
elif tweetTime[:2] == "23" and hourAgo[0] == 0 and int(tweetTime[3:5]) <= hourAgo[1]:
hourAgoHit = True;
if not hourAgoHit:
earliestDate = tweetDate #Make it the earliest tweet that we've looked at so far
earliestID = tweet['id']
idList.append(tweet['id'])
wordCount = updateWordCount(tweet, wordCount) #Add its words to the wordCount dictionary
numTweetsUsed = numTweetsUsed + 1
else:
break
except Exception as err:
print(err)
numPages = numPages + 1
except Exception as err:
print(err)
print(numTweetsUsed, "tweets in total")
return wordCount
def updateWordCount(tweet, wordCount):
"""
Updates the wordCount dictionary as well as the wants and needs dictionaries
"""
global needs
global wants
weekAgo = (datetime.datetime.now() - datetime.timedelta(days=7)).strftime("%Y-%m-%d") #the date a week ago
# tweetList = []
# userTimeline = twitter.get_user_timeline(screen_name=user, count=200, since=weekAgo)
user = tweet['user']['screen_name'].encode('utf8').decode('utf8') #username
tweetText = tweet['text'].encode('utf8').decode('utf8') #tweet text
# print("created_at: " + tweet['created_at'])
tweetDate = tweet['created_at'][-4:] + "-" + month2Num[tweet['created_at'][4:7]] + "-" + tweet['created_at'][8:10] #Date tweet created
try:
print(tweetText + " " + tweetDate + " " + user)
except Exception as err:
print(err)
tweetSents = nltk.sent_tokenize(tweetText)
for sent in tweetSents:
tweetWords = nltk.word_tokenize(sent) #A list of the tweet words/punctuation
tweetPOS = nltk.pos_tag(sent) #A list of the tweet words and their Part-of-Speech tag
wordIndex = 0 #Start at beginning index of 0
for word in tweetWords: #for each word in the tweet
if wordIsOK(word) and word.lower() != user.lower(): #Check if its ok and that its not the user's username
# try:
# if word.lower() not in wordCount: #If the word isn't already in the wordCount dictionary
# print("ADDING " + word.lower())
# wordCount[word.lower()] = [0,[]]; #add it
# else:
# print(word.lower() + " already in dict")
# wordCount[word.lower()][0] = wordCount[word.lower()][0] + 1 #increase the number of times it's been used
# if(user not in wordCount[word.lower()][1]): #Add the username of user who used the word
# wordCount[word.lower()][1].append(user)
# except Exception as err:
# print(err)
try:
updateNeeds(word, wordIndex, tweetWords, tweetPOS, user, tweet['created_at'])
updateWants(word, wordIndex, tweetWords, tweetPOS, user, tweet['created_at'])
except Exception as err:
print(err)
wordIndex = wordIndex + 1
return(wordCount)
def updateNeeds(word, wordIndex, tweetWords, tweetPOS, user, tweetDate):
"""
Updates the dictionary of words that follow "need" in tweets
word is the current word being looked at in the tweet
wordIndex is the index of the word
tweetWords is a list of all the words in the tweet
tweetPOS is a list of the parts of speech of every word in the tweet
user is the name of the user who tweeted
"""
global needs #Get the global variable needs dictionary
global wantNeed
global needLines
if word.lower() == "need" or word.lower() == "needs" or word.lower() == "needed" or word.lower() == "needing": #If the word is a form of "need"
if wordIndex + 1 < len(tweetWords) and tweetWords[wordIndex + 1] == "to": #if it's followed by "to"
if wordIsOK(tweetWords[wordIndex + 2].lower()):
try:
endIndex = 2
addList = ["to", tweetWords[wordIndex + endIndex]] #Get the phrase "to [verb]"
print("Adding '" + ' '.join(addList).lower() + "' to NEEDS")
add = ' '.join(addList).lower()
if add not in needs: #If the phrase isn't already in the needs dictionary
needs[add] = [0, []] #Add it
needs[add][0] = needs[add][0] + 1 #Increase the number of times the phrase was used
if (user not in needs[add][1]): #Add the username to the list of users who have used this phrase
needs[add][1].append(user)
date = tweetDate[-4:] + "-" + month2Num[tweetDate[4:7]] + "-" + tweetDate[8:10]
time = tweetDate[11:19]
# needs_csv.writerow([add, user, date, time])
needLines.append([add, user, date, time])
if add not in wantNeed:
wantNeed[add] = ["n",0]
elif wantNeed[add][0] == "w":
wantNeed[add][0] = "wn"
wantNeed[add][1] = wantNeed[add][1] + 1
except Exception as err:
print(err)
elif wordIndex + 1 < len(tweetWords):
if wordIsOK(tweetWords[wordIndex + 1].lower()) or tweetWords[wordIndex + 1].lower() in articles or tweetWords[wordIndex + 1].lower() in ignoreWords: #if there's another word following the "need" word
try:
endIndex = 1
addList = [tweetWords[wordIndex + endIndex]] #keep adding words to the phrase until you've hit something other than an adj or adv
while(wordIndex + endIndex + 1 < len(tweetWords) and ( tweetWords[wordIndex + endIndex].lower() in articles or tweetWords[wordIndex + endIndex] == "and" or not(tweetPOS[wordIndex + endIndex][1] in noun) or tweetWords[wordIndex + endIndex].lower() in adjectives)):
endIndex += 1
if(tweetWords[wordIndex + endIndex] == "." or tweetWords[wordIndex + endIndex] == "," or tweetWords[wordIndex + endIndex] == "for" or tweetWords[wordIndex + endIndex] == "of" or tweetWords[wordIndex + endIndex] == "https"):
break
addList.append(tweetWords[wordIndex + endIndex])
# addList.append(tweetPOS[wordIndex + endIndex][1])
print("Adding '" + ' '.join(addList).lower() + "' to NEEDS")
# needs.append(tweetWords[wordIndex + 1].lower())
add = ' '.join(addList).lower()
if add not in needs: #If it isn't already in the dictionary
needs[add] = [0, []] #add it
needs[add][0] = needs[add][0] + 1 #Increase the number of times it's been used
if (user not in needs[add][1]): #Add the username to the list of users who have used this phrase
needs[add][1].append(user)
date = tweetDate[-4:] + "-" + month2Num[tweetDate[4:7]] + "-" + tweetDate[8:10]
time = tweetDate[11:19]
# needs_csv.writerow([add, user, date, time])
needLines.append([add, user, date, time])
if add not in wantNeed:
wantNeed[add] = ["n",0]
elif wantNeed[add][0] == "w":
wantNeed[add][0] = "wn"
wantNeed[add][1] = wantNeed[add][1] + 1
except Exception as err:
print(err)
def updateWants(word, wordIndex, tweetWords, tweetPOS, user, tweetDate):
"""
Updates the dictionary of words that follow "want" in a tweet
word is the current word being looked at in the tweet
wordIndex is the index of the word
tweetWords is a list of all the words in the tweet
tweetPOS is a list of the parts of speech of every word in the tweet
user is the name of the user who tweeted
"""
global wants #Get the global variable wants
global wantNeed
global wantLines
# global wants_csv
if word.lower() == "want" or word.lower() == "wants" or word.lower() == "wanted" or word.lower() == "wanting": #If the word is a form of "want"
if wordIndex + 1 < len(tweetWords) and tweetWords[wordIndex + 1] == "to": #If it's followed by "to"
if wordIsOK(tweetWords[wordIndex + 2].lower()):
try:
endIndex = 2
addList = ["to", tweetWords[wordIndex + endIndex]] #Get the phrase "to [verb]"
print("Adding '" + ' '.join(addList).lower() + "' to WANTS")
add = ' '.join(addList).lower()
if add not in needs: #If it is not already in the dictionary
wants[add] = [0, []] #add it
wants[add][0] = wants[add][0] + 1 #increment number of times it's been used
if (user not in wants[add][1]): #If the user isn't already in the list of users who have used the word
wants[add][1].append(user) #add their username to the list
date = tweetDate[-4:] + "-" + month2Num[tweetDate[4:7]] + "-" + tweetDate[8:10]
time = tweetDate[11:19]
# wants_csv.writerow([add, user, date, time])
wantLines.append([add, user, date, time])
if add not in wantNeed:
wantNeed[add] = ["w",0]
elif wantNeed[add][0] == "n":
wantNeed[add][0] = "wn"
wantNeed[add][1] = wantNeed[add][1] + 1
except Exception as err:
print(err)
elif wordIndex + 1 < len(tweetWords): #Otherwise, add the words following "want"
if wordIsOK(tweetWords[wordIndex + 1].lower()) or tweetWords[wordIndex + 1].lower() in articles or tweetWords[wordIndex + 1].lower() in ignoreWords:
try:
endIndex = 1
addList = [tweetWords[wordIndex + endIndex]] #Keep adding all the adjectives or other words in the phrase until you hit a noun or something
while(wordIndex + endIndex + 1 < len(tweetWords) and (tweetWords[wordIndex + endIndex].lower() in articles or tweetWords[wordIndex + endIndex] == "and" or not(tweetPOS[wordIndex + endIndex][1] in noun) or tweetWords[wordIndex + endIndex].lower() in adjectives)):
endIndex += 1
if(tweetWords[wordIndex + endIndex] == "." or tweetWords[wordIndex + endIndex] == "," or tweetWords[wordIndex + endIndex] == "for" or tweetWords[wordIndex + endIndex] == "of" or tweetWords[wordIndex + endIndex] == "https"):
break
addList.append(tweetWords[wordIndex + endIndex])
# addList.append(tweetPOS[wordIndex + endIndex][1])
print("Adding '" + ' '.join(addList).lower() + "' to WANTS")
# needs.append(tweetWords[wordIndex + 1].lower())
add = ' '.join(addList).lower()
if add not in needs: #Add word/phrase to the dictionary
wants[add] = [0, []]
wants[add][0] = wants[add][0] + 1 #And Increment the number of times it's been used
if (user not in wants[add][1]):
wants[add][1].append(user) #Add username to the list of users who have used this word/phrase
date = tweetDate[-4:] + "-" + month2Num[tweetDate[4:7]] + "-" + tweetDate[8:10]
time = tweetDate[11:19]
# wants_csv.writerow([add, user, date, time])
wantLines.append([add, user, date, time])
if add not in wantNeed:
wantNeed[add] = ["w",0]
elif wantNeed[add][0] == "n":
wantNeed[add][0] = "wn"
wantNeed[add][1] = wantNeed[add][1] + 1
except Exception as err:
print(err)
def atlGeocodeTwitterSummary():
"""
Writes a csv file that outlines the words that were used in ATL tweets
"""
# tweetWords = getGeocodeTweetWordCount();
tweetWords = wordCount
open_csv = open("summaryGeocode" + str(dayCount) + ".csv", "w", newline='')
summary_csv = csv.writer(open_csv)
#summary_csv.writerow([str(numTweetsUsed) + " tweets in total"])
# summary_csv.writerow(["Word", "Number of times used", "Users who used Word"])
summary_csv.writerow(["Word", "Number of times used"])
rowsList = []
for word in tweetWords:
# usersString = ""
# for user in tweetWords[word][1]:
# usersString = usersString + user + ", "
# row = [word, tweetWords[word][0], usersString]
row = [word, tweetWords[word][0]]
placeFound = False
currI = 0
while not placeFound:
if currI == len(rowsList) or row[1] >= rowsList[currI][1]:
placeFound = True
rowsList.insert(currI, row)
else:
currI = currI + 1
for row in rowsList:
try:
summary_csv.writerow(row)
except Exception as err:
print(err)
open_csv.close()
def writeWantNeed():
"""
Writes a file that tallies up the number of times a phrase was used
and whether or not it was used as a want, a need, or both
"""
open_csv = open("wantNeed" + str(dayCount) + ".csv", "w", newline='')
summary_csv = csv.writer(open_csv)
#summary_csv.writerow([str(numTweetsUsed) + " tweets in total"])
summary_csv.writerow(["Phrase", "W/N", "Number of Uses"])
for entry in wantNeed:
summary_csv.writerow([entry, wantNeed[entry][0], wantNeed[entry][1]])
open_csv.close()
def writeNeedsWantsSummaries():
"""
Writes individual files for Needs and Wants depicting the phrases for Needs/Wants,
number of times used, and the users who said them. This overwrites an existing file
made for the day, updating it with the new data.
"""
open_csv = open("needsSummary" + str(dayCount) + ".csv", "w", newline='')
summary_csv = csv.writer(open_csv)
#summary_csv.writerow([str(numTweetsUsed) + " tweets in total"])
# summary_csv.writerow(["Needs", "Number of times used", "Users"])
summary_csv.writerow(["Needs", "Number of times used"])
rowsList = []
for word in needs:
# print(word)
# usersString = ""
# for user in needs[word][1]:
# usersString = usersString + user + ", "
# row = [word, needs[word][0], usersString]
row = [word, needs[word][0]]
placeFound = False
currI = 0
while not placeFound:
if currI == len(rowsList) or row[1] >= rowsList[currI][1]:
placeFound = True
rowsList.insert(currI, row)
else:
currI = currI + 1
for row in rowsList:
try:
summary_csv.writerow(row)
except Exception as err:
print(err)
open_csv.close()
open_csv = open("wantsSummary" + str(dayCount) + ".csv", "w", newline='')
summary_csv = csv.writer(open_csv)
# summary_csv.writerow(["Wants", "Number of times used", "Users"])
summary_csv.writerow(["Wants", "Number of times used"])
rowsList = []
for word in wants:
# print(word)
# usersString = ""
# for user in wants[word][1]:
# usersString = usersString + user + ", "
# row = [word, wants[word][0], usersString]
row = [word, wants[word][0]]
placeFound = False
currI = 0
while not placeFound:
if currI == len(rowsList) or row[1] >= rowsList[currI][1]:
placeFound = True
rowsList.insert(currI, row)
else:
currI = currI + 1
for row in rowsList:
try:
summary_csv.writerow(row)
except Exception as err:
print(err)
open_csv.close()
def appendNeedLines():
"""
Updates thes needsList csv file that holds a list of all the needs, their time stamps, dates, and user who tweeted them
"""
newNeedLines = needLines[::-1]
openNeeds_csv = open("needsList" + str(dayCount) + ".csv", "a", newline='')
needs_csv = csv.writer(openNeeds_csv)
for line in newNeedLines:
needs_csv.writerow(line)
openNeeds_csv.close()
def appendWantLines():
"""
Updates thes wantsList csv file that holds a list of all the wants, their time stamps, dates, and user who tweeted them
"""
newWantLines = wantLines[::-1]
openWants_csv = open("wantsList" + str(dayCount) + ".csv", "a", newline='')
wants_csv = csv.writer(openWants_csv)
for line in newWantLines:
wants_csv.writerow(line)
openWants_csv.close()
firstRun = True
def runBot():
global dayCount
global timesRun
global wantNeedDictionaries
global wantDictionaries
global needDictionaries
global needs
global wants
global numTweetsUsed
global wantNeed
global wordCount
global firstRun
global timesRun
global dayCount
global wantLines
global needLines
if timesRun == 0:
if not firstRun:
#append the data of the last run to the cumulative data for the day
wantNeedDictionaries.append(wantNeed)
wantDictionaries.append(wants)
needDictionaries.append(needs)
firstRun = False
# Reset Everything
needs = {}
wants = {}
numTweetsUsed = 0
wantNeed = {}
wordCount = {}
openWants_csv = open("wantsList" + str(dayCount) + ".csv", "w", newline='')
wants_csv = csv.writer(openWants_csv)
openNeeds_csv = open("needsList" + str(dayCount) + ".csv", "w", newline='')
needs_csv = csv.writer(openNeeds_csv)
wants_csv.writerow(["Want", "User", "Date", "Time"])
needs_csv.writerow(["Need", "User", "Date", "Time"])
openWants_csv.close()
openNeeds_csv.close()
wantLines = []
needLines = []
getGeocodeTweetWordCount() #update the dictionaries
# atlGeocodeTwitterSummary() #this makes a csv file of the entire wordcount from tweets
writeNeedsWantsSummaries() #update wantSummary and needSummary csv files
writeWantNeed() #update wantNeed csv file
appendWantLines()
appendNeedLines()
timesRun = timesRun + 1
if timesRun == 48:
timesRun = 0
dayCount = dayCount + 1
print("Finished Iteration #" + str(timesRun) + " on day #" + str(dayCount))
def setInterval(func, sec):
def func_wrapper():
setInterval(func, sec)
func()
t = Timer(sec, func_wrapper)
t.start()
return t
runOnce = False
runBot()
if not runOnce:
setInterval(runBot, 60*30) # runs every 30 minutes