-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwikipedia_fetched.py
244 lines (199 loc) · 6.52 KB
/
wikipedia_fetched.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
#!/usr/bin/env python
"""
This file is dedicated to querying data from the Wikipedia API.
"""
# Standard library
import argparse
import csv
import os
import sys
import traceback
# Third-party
import requests
import yaml
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# Add parent directory so shared can be imported
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
# First-party/Local
import shared # noqa: E402
# Setup
LOGGER, PATHS = shared.setup(__file__)
# Log the start of the script execution
LOGGER.info("Script execution started.")
def parse_arguments():
"""
Parses command-line arguments, returns parsed arguments.
"""
LOGGER.info("Parsing command-line arguments")
parser = argparse.ArgumentParser(
description="Wikipedia Data Fetching Script"
)
parser.add_argument(
"--languages",
type=str,
nargs="+",
default=["en"],
help="List of Wikipedia language codes to query",
)
return parser.parse_args()
def set_up_data_file():
"""
Sets up the data file for recording results.
"""
LOGGER.info("Setting up the data file for recording results.")
header = (
"language,articles,edits,images,"
"users,activeusers,admins,jobqueue,views\n"
)
with open(
os.path.join(PATHS["data_phase"], "wikipedia_fetched.csv"), "w"
) as f:
f.write(header)
def get_request_url(lang="en"):
"""
Provides the API Endpoint URL for specified parameter combinations.
Args:
lang: A string representing the language for the Wikipedia API.
Returns:
string: The API Endpoint URL for the query.
"""
LOGGER.info(f"Generating request URL for language: {lang}")
base_url = (
r"https://{lang}.wikipedia.org/w/api.php"
"?action=query&meta=siteinfo"
"&siprop=statistics&format=json"
)
return base_url.format(lang=lang)
def get_response_elems(language="en"):
"""
Provides the metadata for query of specified parameters.
Args:
language: A string representing the language for the Wikipedia API.
Returns:
dict: A dictionary mapping metadata
to its value provided from the API query.
"""
LOGGER.info(f"Querying Wikipedia API for language: {language}")
try:
request_url = get_request_url(language)
max_retries = Retry(
total=5,
backoff_factor=10,
status_forcelist=[403, 408, 429, 500, 502, 503, 504],
)
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()
stats = search_data.get("query", {}).get("statistics", {})
stats["language"] = language
return stats
except Exception as e:
LOGGER.error(f"Error occurred during API request: {e}")
raise shared.QuantifyingException(f"Error fetching data: {e}", 1)
def record_results(stats):
"""
Records the data for a specific language into the CSV file.
Args:
stats: A dictionary of Wikipedia statistics.
"""
LOGGER.info(f"Recording data for language: {stats.get('language')}")
row = [
stats.get("language", ""),
stats.get("articles", 0),
stats.get("edits", 0),
stats.get("images", 0),
stats.get("users", 0),
stats.get("activeusers", 0),
stats.get("admins", 0),
stats.get("jobqueue", 0),
stats.get("views", 0),
]
with open(
os.path.join(PATHS["data_phase"], "wikipedia_fetched.csv"),
"a",
newline="",
) as f:
writer = csv.writer(f)
writer.writerow(row)
def retrieve_and_record_data(args):
"""
Retrieves and records the data for all specified languages.
"""
LOGGER.info("Starting data retrieval and recording.")
total_records_retrieved = 0
for lang in args.languages:
stats = get_response_elems(lang)
if stats:
record_results(stats)
total_records_retrieved += 1
return total_records_retrieved
def load_state():
"""
Loads the state from a YAML file, returns the last recorded state.
"""
if os.path.exists(PATHS["state"]):
with open(PATHS["state"], "r") as f:
return yaml.safe_load(f)
return {"total_records_retrieved (wikipedia)": 0}
def save_state(state: dict):
"""
Saves the state to a YAML file.
Args:
state: The state dictionary to save.
"""
with open(PATHS["state"], "w") as f:
yaml.safe_dump(state, f)
def main():
# Fetch and merge changes
shared.fetch_and_merge(PATHS["repo"])
args = parse_arguments()
state = load_state()
total_records_retrieved = state["total_records_retrieved (wikipedia)"]
LOGGER.info(f"Initial total_records_retrieved: {total_records_retrieved}")
goal_records = 1000 # Set goal number of records
if total_records_retrieved >= goal_records:
LOGGER.info(
f"Goal of {goal_records} records already achieved."
" No further action required."
)
return
# Log the paths being used
shared.log_paths(LOGGER, PATHS)
# Create data directory for this phase
os.makedirs(PATHS["data_phase"], exist_ok=True)
if total_records_retrieved == 0:
set_up_data_file()
# Retrieve and record data
records_retrieved = retrieve_and_record_data(args)
# Update the state with the new count of retrieved records
total_records_retrieved += records_retrieved
LOGGER.info(
f"Total records retrieved after fetching: {total_records_retrieved}"
)
state["total_records_retrieved (wikipedia)"] = total_records_retrieved
save_state(state)
# Add and commit changes
shared.add_and_commit(PATHS["repo"], "Added and committed Wikipedia data")
# Push changes
shared.push_changes(PATHS["repo"])
if __name__ == "__main__":
try:
main()
except shared.QuantifyingException as e:
if e.exit_code == 0:
LOGGER.info(e.message)
else:
LOGGER.error(e.message)
sys.exit(e.exit_code)
except SystemExit as e:
LOGGER.error(f"System exit with code: {e.code}")
sys.exit(e.code)
except KeyboardInterrupt:
LOGGER.info("(130) Halted via KeyboardInterrupt.")
sys.exit(130)
except Exception:
LOGGER.exception(f"(1) Unhandled exception: {traceback.format_exc()}")
sys.exit(1)