-
Notifications
You must be signed in to change notification settings - Fork 4
/
twitter_crawler_project.py
210 lines (177 loc) · 7.31 KB
/
twitter_crawler_project.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
import sys
import os
import json
import time
import re
from datetime import datetime
from twarc import Twarc
access_token = "Your tokens here"
access_token_secret= ""
client_key = ""
client_secret = ""
total_keywords = {}
languages = ["es", "en"]
RESTART_TIME = 14400
PROJECTS_FOLDER = '/var/data/inputs/projects/'
def main():
"""
Main program
"""
# argument check
if len(sys.argv) > 1:
# if argument file exists
if os.access(sys.argv[1], os.R_OK):
input_file = sys.argv[1]
else:
sys.stderr.write("ERROR, NEED VALID PROJECT FILE!\n")
sys.stderr.write("%s NOT ACCESSIBLE!\n"%sys.argv[1])
sys.exit(1)
else:
sys.stderr.write("ERROR, NEED PROJECT FILE\n")
sys.exit(1)
# check if data folder exists or create it
if not os.path.isdir("data"):
os.makedirs("data")
# keep running stream function (every hour)
while True:
# string of streaming words
print "Starting"
keys = ""
lines = []
projects = []
# open file for read
with open(input_file, "r") as fr:
string_txt = fr.read()
projects = json.loads(string_txt)
for project in projects:
keys += ",".join(project["synonyms"]) + ","
print("Projects %s" % str(projects))
keys = keys.rstrip(",")
# create Twarc class
t = Twarc(client_key, client_secret, access_token, access_token_secret)
# call stream function every hour
if stream(keys, projects, t) != True:
sys.stderr.write("ERROR, STREAM QUITS\n")
sys.exit(1)
def stream(query, projects, t):
"""
Stream tweets from twitter and save them to file every hour
Args:
lines - array of streaming words
t - Twarc class
Returns:
boolean - True (OK) / False (Error)
"""
# make timestamps
timestr = time.strftime("%Y-%m-%d_%H-%M-%S")
datestr = time.strftime("%Y-%m-%d")
# get total time for check time
start_time = time.time()
# create directories and files for keywords
for project in projects:
project_id = project["id"]
#Creating folders
if not os.path.isdir(PROJECTS_FOLDER + "projects"):
os.makedirs(PROJECTS_FOLDER + "projects")
# for keyword
if not os.path.isdir(PROJECTS_FOLDER + "/%s" % datestr):
os.makedirs(PROJECTS_FOLDER + "/%s" % datestr)
# for date
if not os.path.isdir(PROJECTS_FOLDER + "/%s/%s" % (datestr, project_id)):
os.makedirs(PROJECTS_FOLDER + "/%s/%s" % (datestr, project_id))
if not os.path.isdir(PROJECTS_FOLDER + "/%s/%s/twitter" % (datestr, project_id)):
os.makedirs(PROJECTS_FOLDER + "/%s/%s/twitter" % (datestr, project_id))
## create json file for writing data
#with open(filepath(project_id, datestr, timestr)+".json", "w") as fw:
# #fw.write("[")
# fw.write("")
while True:
try:
# find lines in twitter
print "Query string: %s" % query
for tweet in t.stream(query):
# regex to find keyword
for project in projects:
project_id = project["id"]
filename = filepath(project_id, datestr, timestr)
check = 0
is_tweet = False
if 'lang' in tweet.keys() and tweet['lang'] in languages:
for keyword in project["synonyms"]:
# create list of words in keyword
wlist = keyword.split()
# length of this list
w_length = len(wlist)
# for every word in keyword
for w in wlist:
# check if word is in tweet
word_match = re.search(r"\b%s\b" % w, tweet["text"], re.IGNORECASE)
if word_match:
check += 1
if check== w_length:
tweet["synonym_found"] = keyword
is_tweet = True
break
if len(wlist)==1:
word_match = re.search(r"@%s\b" % keyword, tweet["text"], re.IGNORECASE)
if word_match:
tweet["synonym_found"] = "@%s" % keyword
is_tweet = True
break
if is_tweet:
break
# if every word from keyword is in tweet, save to file
if is_tweet:
if has_not_words(tweet["text"],project["nots"]):
#print "Has not words: '%s' in '%s'" % (project["nots"], tweet["text"])
print "Has not words "
continue
print "%s - Tweet for %s" % (datetime.now(), project["name"])
stop=False
if(tweet['geo']!=None):
print "Inverting geo"
(lat,lon) = tweet['geo']['coordinates']
tweet['geo']['coordinates'] = [lon, lat]
with open(filename + ".txt", "a") as fw:
tweet['project_id'] = project_id
tweet['project_name'] = project['name']
dumped_json = json.dumps(tweet)
fw.write(dumped_json)
fw.write("\n")
# exit every hour and start function again
if start_time+RESTART_TIME < time.time():
return True
# except for quit application
except KeyboardInterrupt:
sys.stdout.write("QUIT\n")
sys.exit(0)
return False
def format_tweet(tweet, project):
structure = {"source":"twitter",
"brand": project["name"],
"synonym_found": tweet["synonym_found"],
"synonyms": project["synonyms"],
"nots": project["nots"],
"project_id": project["id"],
"raw": tweet,
"text": tweet["text"],
"time": parse_twitter_time(tweet["created_at"]),
"url": "http://twitter.com/statuses/%s" % tweet["id_str"],
"lang": tweet["lang"]
}
return json.dumps(structure)
def filepath(project_id, datestr, timestr):
#return PROJECTS_FOLDER+str(project_id)+"/"+datestr+"/twitter/"+timestr
return PROJECTS_FOLDER+datestr+"/"+str(project_id)+"/twitter/"+timestr
def parse_twitter_time(twitter_time):
twitter_time = re.sub("[\-\+]\d\d\d\d ", "", twitter_time)
date = datetime.strptime(twitter_time, "%a %b %d %H:%M:%S %Y")
return date.strftime("%Y%m%d%H%M%S")
def has_not_words(text, nots_array):
for not_phrase in nots_array:
match = re.search(not_phrase, text, re.IGNORECASE)
if match:
return True
return False
if __name__ == "__main__":
main()