-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterBot.py
55 lines (42 loc) · 1.65 KB
/
TwitterBot.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
import tweepy, time, sys
from States import *
kconsumer_key = '' #your consumer key here
kconsumer_secret = '' #your consumer secret here
kaccess_key = '' #your access key here
kaccess_secret = '' #your access secret here
auth = tweepy.OAuthHandler(kconsumer_key, kconsumer_secret)
auth.set_access_token(kaccess_key, kaccess_secret)
api = tweepy.API(auth)
readTweets = []
def updateLocationCount(location):
location = location.split()
for i in location:
for (k,v) in states.items():
if i == k or i == v:
stateCount[k] += 1
def main(keyWord, sampleSize):
query = keyWord
maxTweets = sampleSize
searchedTweets = [status for status in tweepy.Cursor(api.search, q=query).items(maxTweets)]
for status in searchedTweets:
if status.id in readTweets:
break
try:
readTweets.append(status.id)
rawLocation = status.author.location
cleanLocation = ""
for char in rawLocation:
if str.isspace(char) or str.isalpha(char):
cleanLocation += char
if cleanLocation != "":
updateLocationCount(cleanLocation)
except:
continue
def exportData(fileName):
outputFile = open(fileName, "a")
outputFile.write("State Name, State Code, Latitude, Longitude, Number of Tweets \n")
for (key, value) in states.items():
latitude = stateCoordinates[key][0]
longitude = stateCoordinates[key][1]
outputFile.write(value + ',' + key + ',' + str(latitude) + ',' +
str(longitude) + ',' + str(stateCount[key]) + "\n")