-
Notifications
You must be signed in to change notification settings - Fork 5
/
Twitter_Geocoding.py
273 lines (196 loc) · 9.88 KB
/
Twitter_Geocoding.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
# ----------------- IMPORTING -----------------
import urllib.request
import json
import requests
import logging
import time
import datetime
import os.path
import csv
import sys
sys.path.insert(1, 'Resources')
import caching
# ---------------------------------------------
############## INPUT VARIABLES ################
# Optional input for partial processing
start_at = 0
# Directory Input
api_tokens = 'tokens/bing_tokens.csv
# Optional working with batches
batch = True # If TRUE, batch number has to be specified according to file names
batch_nr = 1
if batch:
log_dir = 'LOGS/batch%s_LOG_Twitter_Geocode.log' % batch_nr
import_dir = 'data/raw_data/batch%s_github_geoloc_export.json' % batch_nr
export_dir = 'data/raw_data/batch%s_twitter_geoloc_export.json' % batch_nr
export_dir_missing = 'data/raw_data/batch%s_twitter_geloc_not_found.csv' % batch_nr
else:
log_dir = 'LOGS/LOG_Twitter_Geocode.log'
import_dir = 'data/raw_data/github_geoloc_export.json'
export_dir = 'data/raw_data/twitter_geoloc_export.json'
export_dir_missing = 'data/raw_data/twitter_geloc_not_found.csv'
###############################################
# ----------- AUTHENTIFIKATION INFO -----------
# Stack with not limited tokens reached
with open(api_tokens, 'r', encoding='utf-8-sig') as tokens:
reader = csv.reader(tokens, delimiter = ',')
# Stack with tokens that have not reached limit
token_ready = list(reader)
# Stack with limited tokens
token_wait = []
bingMapsKey = token_ready.pop(0)[0]
# ---------------------------------------------
# ------------- GLOBAL VARIABLES --------------
count = 0
export_geoloc = {}
not_found = [] # List of locations not found
maxResults = 5
url = 'http://dev.virtualearth.net/REST/v1/Locations/%s?&maxResults=%s&key=%s'
processed = 0
completed = 0
empty = 0
er_500 = 0
er_404 = 0
er_400 = 0
locations = {}
logging.basicConfig(filename=log_dir, filemode='a',
format='%(asctime)s [%(levelname)s] - %(message)s',
datefmt='%d-%m-%y %H:%M:%S', level=logging.INFO)
# ---------------------------------------------
# ------------- SWITCHING TOKENS --------------
def switch(bingMapsKey):
token_wait.append(bingMapsKey)
if len(token_ready) != 0:
bingMapsKey = token_ready.pop(0)[0]
else:
bingMapsKey = token_wait.pop(0)
response = requests.get(url % (loc_http, maxResults, bingMapsKey))
if (response.headers['X-MS-BM-WS-INFO']) == 1:
check_wait(response)
logging.info("Switching token to: " + str(bingMapsKey))
return(bingMapsKey)
# ---------------------------------------------
# ------------------ WAITING ------------------
# Function: Waits until the pull limits are renewed
def check_wait(response):
logging.info("waiting for API counter reset ... ")
# Checks every minute if limit is renewed
while response.headers['X-MS-BM-WS-INFO'] == 1:
time.sleep(60)
logging.info("API rate limit reset!")
logging.info("Continuing pull requests ...")
# ---------------------------------------------
# --------------- LOADING FILE ----------------
def json_load(file):
if os.path.exists(file) == True:
with open(file, 'r+') as outfile:
return(json.load(outfile))
# ---------------------------------------------
logging.info('Loading of external file ...')
# Loading data
export = json_load(import_dir)
logging.info('File successfully loaded ...')
logging.info('Processing of useres initiated ...')
log_starttime = datetime.datetime.now()
try:
for user in export:
count += 1
if count >= start_at:
try:
try:
loc_str = export[user]['twitter_username']['twitter_location']
except:
loc_str = ''
if loc_str:
loc_str = loc_str.lower()
# Checks if user input is already associated with a location
if loc_str in locations:
geo_info = locations[loc_str]
# If input is not associated with location, input is crawled
else:
# Query string tranformed into http readable string
loc_http = urllib.parse.quote(loc_str, safe='')
# Creating API Query
response = requests.get(url % (loc_http, maxResults, bingMapsKey))
# Checking if token limit reached
try:
if response.headers['Cache-Control'] == 'no-cache' and response.headers['X-MS-BM-WS-INFO'] == 1:
logging.warning("Pull rate limit for reached")
bingMapsKey = switch(bingMapsKey)
response = requests.get(url % (loc_http, maxResults, bingMapsKey))
except:
print(response.headers)
not_found.append([str(user),loc_str])
# crwaling of info
if response.status_code == 200:
address_index = 0
geo_info = {}
try:
for geo_loc in response.json()['resourceSets'][0]['resources']:
address_index += 1
geo_loc['address']['confidence'] = geo_loc['confidence']
geo_loc['address']['userTyped'] = loc_str
geo_info[str(address_index)] = geo_loc['address']
# Adding longitude and latitude coordinates
geo_info[str(address_index)]['coordinates'] = {'latitude': geo_loc['point']['coordinates'][0],
'longitude': geo_loc['point']['coordinates'][1]}
locations[loc_str] = geo_info # Adds associated location to user input
except:
not_found.append([str(user),loc_str])
er_404 += 1
# Counting successfully of processed users
completed += 1
# Error handeling
elif response.status_code == 404:
not_found.append([str(user),loc_str])
er_404 += 1
geo_info = {}
elif response.status_code == 400:
not_found.append([str(user),loc_str])
er_400 += 1
geo_info = {}
elif response.status_code >= 500:
not_found.append([str(user),loc_str])
er_500 += 1
logging.warning("Fatal Error 500 - Server sided Error")
if er_500 >= 5:
logging.warning("Exiting Sys.")
logging.info('Last processed User: '+ str(user))
sys.exit()
else:
not_found.append([str(user),loc_str])
logging.warning("Unknown Error: " + str(response.status_code))
geo_info = {}
else:
geo_info = None
empty += 1
# Adding geo_loc information
export_geoloc[str(user)] = export[user]
if loc_str:
export_geoloc[user]['twitter_username']['twitter_location'] = geo_info
# Exporting user data as json
processed += 1
if processed % 100 == 0:
# Saving new gained info every 100 users
caching.cache(export_geoloc, export_dir, json)
caching.cache(not_found, export_dir_missing, csv)
not_found = []
export_geoloc = {}
except Exception as e:
logging.warning(e)
logging.warning(str(e))
logging.info("Last processed user: " + str(processed))
caching.cache(export_geoloc, export_dir, json)
caching.cache(not_found, export_dir_missing, csv)
not_found = []
export_geoloc = {}
logging.info(str(completed) + " of " + str(len(export)) + " users succesfully processed")
logging.info(str(empty) + " location spaces are empty")
logging.info(str(er_404) + " locations could not be found")
logging.info(str(er_500 + er_400) + " other errors occured")
log_endtime = datetime.datetime.now()
log_runtime = (log_endtime - log_starttime)
logging.info("Total runtime: " + str(log_runtime))
except Exception as e:
logging.warning(e)
logging.warning(str(e))