-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgoogle_scratcher.py
executable file
·346 lines (314 loc) · 11.9 KB
/
google_scratcher.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
#!/usr/bin/env python
"""
This file is dedicated to obtain a .csv record report for Google Custom Search
Data.
"""
# Standard library
import datetime as dt
import os
import sys
import traceback
# Third-party
import pandas as pd
import query_secrets
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
today = dt.datetime.today()
API_KEYS = query_secrets.API_KEYS
API_KEYS_IND = 0
CWD = os.path.dirname(os.path.abspath(__file__))
DATA_WRITE_FILE = (
f"{CWD}"
f"/data_google_custom_search_{today.year}_{today.month}_{today.day}.csv"
)
DATA_WRITE_FILE_TIME = (
f"{CWD}"
f"/data_google_custom_search_time_"
f"{today.year}_{today.month}_{today.day}.csv"
)
DATA_WRITE_FILE_COUNTRY = (
f"{CWD}"
f"/data_google_custom_search_country_"
f"{today.year}_{today.month}_{today.day}.csv"
)
SEARCH_HALFYEAR_SPAN = 20
PSE_KEY = query_secrets.PSE_KEY
def get_license_list():
"""Provides the list of license from 2018's record of Creative Commons.
Returns:
np.array: An np array containing all license types that should be
searched via Programmable Search Engine.
"""
cc_license_data = pd.read_csv(f"{CWD}/legal-tool-paths.txt", header=None)
license_pattern = r"((?:[^/]+/){2}(?:[^/]+)).*"
license_list = (
cc_license_data[0]
.str.extract(license_pattern, expand=False)
.dropna()
.unique()
)
return license_list
def get_lang_list():
"""Provides the list of language to find Creative Commons usage data on.
Returns:
pd.DataFrame: A Dataframe whose index is language name and has a column
for the corresponding language code.
"""
languages = pd.read_csv(
f"{CWD}/google_lang.txt", sep=": ", header=None, engine="python"
)
languages[0] = languages[0].str.extract(r'"([^"]+)"')
languages = languages.set_index(1)
selected_languages = languages[
languages.index.isin(
[
"Arabic",
"Chinese (Simplified)",
"Chinese (Traditional)",
"English",
"French",
"Indonesian",
"Portuguese",
"Spanish",
]
)
].sort_index()
return selected_languages
def get_country_list(select_all=False):
"""Provides the list of countries to find Creative Commons usage data on.
Args:
select_all:
A boolean indicating whether the returned list will have all
countries.
Returns:
pd.DataFrame: A Dataframe whose index is country name and has a column
for the corresponding country code.
"""
countries = pd.read_csv(CWD + "/google_countries.tsv", sep="\t")
countries["Country"] = countries["Country"].str.replace(",", " ")
countries = countries.set_index("Country").sort_index()
if select_all:
return countries
selected_countries = countries.loc[
[
"India",
"Japan",
"United States",
"Canada",
"Brazil",
"Germany",
"United Kingdom",
"Spain",
"Australia",
"Egypt",
],
:,
].sort_index()
return selected_countries
def get_request_url(license=None, country=None, language=None, time=False):
"""Provides the API Endpoint URL for specified parameter combinations.
Args:
license:
A string representing the type of license, and should be a segment
of its URL towards the license description. Alternatively, the
default None value stands for having no assumption about license
type.
country:
A string representing the country code of country that the search
results would be originating from. Alternatively, the default None
value or "all" stands for having no assumption about country of
origin.
language:
A string representing the language that the search results are
presented in. Alternatively, the default None value or "all" stands
for having no assumption about language of document.
time:
A boolean indicating whether this query is related to video time
occurrence.
Returns:
string: A string representing the API Endpoint URL for the query
specified by this function's parameters.
"""
try:
api_key = API_KEYS[API_KEYS_IND]
base_url = (
r"https://customsearch.googleapis.com/customsearch/v1"
f"?key={api_key}&cx={PSE_KEY}&q=_"
)
if time:
base_url = f"{base_url}&dateRestrict=m{time}"
if license != "no":
base_url = f"{base_url}&linkSite=creativecommons.org"
if license is not None:
base_url = f'{base_url}{license.replace("/", "%2F")}'
else:
base_url = f'{base_url}{"/licenses".replace("/", "%2F")}'
if country is not None:
base_url = f"{base_url}&cr={country}"
if language is not None:
base_url = f"{base_url}&lr={language}"
return base_url
except Exception as e:
if isinstance(e, IndexError):
print("Depleted all API Keys provided", file=sys.stderr)
else:
raise e
def get_response_elems(license=None, country=None, language=None, time=False):
"""Provides the metadata for query of specified parameters
Args:
license:
A string representing the type of license, and should be a segment
of its URL towards the license description. Alternatively, the
default None value stands for having no assumption about license
type.
country:
A string representing the country code of country that the search
results would be originating from. Alternatively, the default None
value or "all" stands for having no assumption about country of
origin.
lang:
A string representing the language that the search results are
presented in. Alternatively, the default None value or "all" stands
for having no assumption about language of document.
time:
A boolean indicating whether this query is related to video time
occurrence.
Returns:
dict: A dictionary mapping metadata to its value provided from the API
query of specified parameters.
"""
try:
request_url = get_request_url(license, country, language, time)
max_retries = Retry(
total=5,
backoff_factor=10,
status_forcelist=[400, 403, 408, 500, 502, 503, 504],
# 429 is Quota Limit Exceeded, which will be handled alternatively
)
session = requests.Session()
session.mount("https://", HTTPAdapter(max_retries=max_retries))
with session.get(request_url) as response:
response.raise_for_status()
search_data = response.json()
search_data_dict = {
"totalResults": search_data["searchInformation"]["totalResults"]
}
return search_data_dict
except Exception as e:
if isinstance(e, requests.exceptions.HTTPError):
global API_KEYS_IND
API_KEYS_IND += 1
print(
"Changing API KEYS due to depletion of quota", file=sys.stderr
)
return get_response_elems(license, country, language, time)
else:
print(f"Request URL was {request_url}", file=sys.stderr)
raise e
def set_up_data_file():
"""Writes the header row to file to contain Google Query data."""
header_title = "LICENSE TYPE,No Priori,"
selected_countries = get_country_list()
all_countries = get_country_list(select_all=True)
selected_languages = get_lang_list()
header_title = (
"LICENSE TYPE,No Priori,"
f"{','.join(selected_countries.index)},"
f"{','.join(selected_languages.index)}"
)
header_title_time = (
"LICENSE TYPE,"
f"{','.join([str(6 * i) for i in range(SEARCH_HALFYEAR_SPAN)])}"
)
header_title_country = "LICENSE TYPE," f"{','.join(all_countries.index)}"
with open(DATA_WRITE_FILE, "w") as f:
f.write(f"{header_title}\n")
with open(DATA_WRITE_FILE_TIME, "w") as f:
f.write(f"{header_title_time}\n")
with open(DATA_WRITE_FILE_COUNTRY, "w") as f:
f.write(f"{header_title_country}\n")
def record_license_data(license_type=None, time=False, country=False):
"""Writes the row for LICENSE_TYPE to file to contain Google Query data.
Args:
license:
A string representing the type of license, and should be a segment
of its URL towards the license description. Alternatively, the
default None value stands for having no assumption about license
type.
time:
A boolean indicating whether this query is related to video time
occurrence.
country:
A boolean indicating whether this query is related to country
occurrence.
"""
if license_type is None:
data_log = "all"
else:
data_log = f"{license_type}"
if country:
all_countries = get_country_list(select_all=True)
for current_country in all_countries.iloc[:, 0]:
country_license_data = get_response_elems(
license=license_type, country=current_country
)
data_log = f"{data_log},{country_license_data['totalResults']}"
data_log = f"{data_log}\nAll Documents"
for current_country in all_countries.iloc[:, 0]:
country_overall_data = get_response_elems(
license="no", country=current_country
)
data_log = f"{data_log},{country_overall_data['totalResults']}"
with open(DATA_WRITE_FILE_COUNTRY, "a") as f:
f.write(f"{data_log}\n")
elif time:
for i in range(SEARCH_HALFYEAR_SPAN):
time_data = get_response_elems(license=license_type, time=i * 6)
data_log = f"{data_log},{time_data['totalResults']}"
with open(DATA_WRITE_FILE_TIME, "a") as f:
f.write(f"{data_log}\n")
else:
selected_countries = get_country_list()
selected_languages = get_lang_list()
no_priori_search = get_response_elems(license=license_type)
data_log += f",{no_priori_search['totalResults']}"
for country_name in selected_countries.iloc[:, 0]:
response = get_response_elems(
license=license_type, country=country_name
)
data_log = f"{data_log},{response['totalResults']}"
for language_name in selected_languages.iloc[:, 0]:
response = get_response_elems(
license=license_type, language=language_name
)
data_log = f"{data_log},{response['totalResults']}"
with open(DATA_WRITE_FILE, "a") as f:
f.write(f"{data_log}\n")
def record_all_licenses():
"""Records the data of all license types findable in the license list and
records these data into the DATA_WRITE_FILE and DATA_WRITE_FILE_TIME as
specified in that constant.
"""
license_list = get_license_list()
record_license_data(time=False)
record_license_data(time=True)
record_license_data(country=True)
for license_type in license_list:
record_license_data(license_type, time=False)
record_license_data(license_type, time=True)
def main():
set_up_data_file()
record_all_licenses()
if __name__ == "__main__":
try:
main()
except SystemExit as e:
sys.exit(e.code)
except KeyboardInterrupt:
print("INFO (130) Halted via KeyboardInterrupt.", file=sys.stderr)
sys.exit(130)
except Exception:
print("ERROR (1) Unhandled exception:", file=sys.stderr)
print(traceback.print_exc(), file=sys.stderr)
sys.exit(1)