-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlake_levels.py
319 lines (268 loc) · 8.61 KB
/
lake_levels.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
"""
Returns a JSON of USA lake levels on record.
Can be searched by state (although not all states have lake level records).
Source: https://lakelevels.info
"""
import argparse
import requests
import os
import re
import json
from bs4 import BeautifulSoup
from prompt_toolkit import prompt
from prompt_toolkit.completion import FuzzyWordCompleter
HEADER = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0"
}
def main():
# Checks for CLI Args
ALL, RESET, SKIP_ASK = cli_args()
# Gets HTML content
if ALL:
html = requests.get("https://www.lakelevels.info/", headers=HEADER)
state = "All"
else:
# Resets defaults at CLI arg.
if RESET:
if os.path.isfile("config.json"):
os.remove("config.json")
print("Defaults removed!")
else:
print(f"Defaults could not be reset: config.json missing!")
# Checks for default settings
if not os.path.isfile("config.json"):
state, url = pick_state(SKIP_ASK)
else:
with open("config.json", "r") as f:
state, url = (json.load(f)).popitem()
html = requests.get(url, headers=HEADER)
if html.status_code != 200:
print(f"Status: {html.status_code} — Try code again\n")
print(f"Error: {html.content}")
quit()
# Calls function to scrape the Beautiful Soup parsed webpage
table_scraper(BeautifulSoup(html.content, "html.parser"), state)
def cli_args():
"""Usage ex.: python.py lake_levels.py -a"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-a",
"--all",
action="store_true",
default=False,
help="Get lake level info for all states on record",
)
parser.add_argument(
"-r",
"--reset",
action="store_true",
default=False,
help="Reset default settings",
)
parser.add_argument(
"-s",
"--skip_ask",
action="store_true",
default=False,
help="Skip asking user whether to save defaults",
)
args = parser.parse_args()
return (
args.all,
args.reset,
args.skip_ask,
)
def pick_state(SKIP_ASK):
"""Returns request URL via user input for US state"""
STATES = [
"Alabama",
"Arizona",
"Arkansas",
"California",
"Colorado",
"Florida",
"Georgia",
"Illinois",
"Indiana",
"Kansas",
"Kentucky",
"Louisiana",
"Mississippi",
"Missouri",
"Montana",
"Nevada",
"New Mexico",
"New York",
"North Carolina",
"Oklahoma",
"Oregon",
"South Carolina",
"Tennessee",
"Texas",
"Utah",
"Virginia",
"West Virginia",
"Wisconsin",
]
word_completer = FuzzyWordCompleter(STATES)
# Get state from CLI user input
while True:
user_input = prompt(f"Enter state:\n", completer=word_completer)
if user_input in STATES:
state_name = user_input.replace(" ", "-")
if not SKIP_ASK:
save_defaults(user_input, state_name)
return user_input, f"https://www.lakelevels.info/USA/{state_name}"
else:
print(f"{user_input} not found.")
def save_defaults(user_input, state_name):
"""Creates JSON of user settings (i.e. the state & the request URL)"""
while True:
ask = (
input("Would you like to save this state as your default? (y/n) ")
.strip()
.lower()
)
if ask == "y":
user_state = {}
user_state[user_input] = f"https://www.lakelevels.info/USA/{state_name}"
# Write the above dict. to a JSON
with open("config.json", "w") as f:
json.dump(user_state, f, indent=4)
print("Default settings JSON created!")
return
elif ask == "n":
return
else:
print("Invalid entry, please enter y or n.")
def table_scraper(soup, state):
"""
Scrapes the lake level table & writes formatted info. to CSV;
Calls another func. to write the info to JSON
"""
# Keeps line breaks by replacing <br>
br_tags = soup.find_all("br")
for br_tag in br_tags:
br_tag.replace_with(" ")
# Beautiful Soup finds table & rows
table = soup.find("table", {"style": "margin-top:15px;"})
table_headers = table.find_all("th", {"bgcolor": "#CCCCCC"})
rows = table.find_all("tr", {"bgcolor": "#ffffff"})
data = {}
with open(f"{state} Lake Data.csv", "w") as f:
# Write the headers to the file
header_list = [a_header.text.strip() for a_header in table_headers]
f.write(", ".join(header_list).replace("\n", ""))
# Write the data for each row to the file
for row in rows:
columns = row.find_all("td")
column_list = [
column.text.strip()
.replace("\n", "")
.replace("\xa0", "")
.replace(" ", " ")
.replace(",", "")
for column in columns
]
# Removes weird & huge whitespace b/w Date & Time
column_list[-1] = " ".join(column_list[-1].split())
f.write("\n" + ", ".join(column_list))
# Make JSON
make_json(data, state, header_list, column_list)
return
def make_json(data, state, header_list, all_columns):
# Extract the lake name from the first column
lake_name = all_columns[0].replace("({})".format(state), "").strip()
lake = {}
for i, head in enumerate(header_list[1:]):
lake[head] = all_columns[i + 1]
# Adds state to data dict. if not preexisting
if state not in data and state != "All":
data[state] = {}
# Add the current iter. lake to the dict.
if state != "All":
data[state][lake_name] = lake
else:
# RegEx pattern for state abbrev. (i.e. w/n parantheses)
pattern = r"\((.*?)\)"
state_list = []
## Split the table into rows and loop through each row
for row in "\n".join(all_columns).split("\n"):
# Extract the values in parentheses from the first column
match = re.search(pattern, row)
if match:
# Split on whitespace & add ea. abbrev. to the list
state_list += match.group(1).split()
for state in state_list:
state_fullname = state_abbreviations(state)
if state_fullname not in data:
data[state_fullname] = {}
data[state_fullname][lake_name] = lake
if data: # Just in case data's empty (altho. unlikely)
with open("lakes.json", "w") as f:
json.dump(data, f, indent=4)
return
def state_abbreviations(abbreviation):
"""Quickly converts state abbreviations to the state's full name"""
state_abbreviations = {
"AK": "Alaska",
"AL": "Alabama",
"AR": "Arkansas",
"AS": "American Samoa",
"AZ": "Arizona",
"CA": "California",
"CO": "Colorado",
"CT": "Connecticut",
"DC": "District of Columbia",
"DE": "Delaware",
"FL": "Florida",
"GA": "Georgia",
"GU": "Guam",
"HI": "Hawaii",
"IA": "Iowa",
"ID": "Idaho",
"IL": "Illinois",
"IN": "Indiana",
"KS": "Kansas",
"KY": "Kentucky",
"LA": "Louisiana",
"MA": "Massachusetts",
"MD": "Maryland",
"ME": "Maine",
"MI": "Michigan",
"MN": "Minnesota",
"MO": "Missouri",
"MP": "Northern Mariana Islands",
"MS": "Mississippi",
"MT": "Montana",
"NC": "North Carolina",
"ND": "North Dakota",
"NE": "Nebraska",
"NH": "New Hampshire",
"NJ": "New Jersey",
"NM": "New Mexico",
"NV": "Nevada",
"NY": "New York",
"OH": "Ohio",
"OK": "Oklahoma",
"OR": "Oregon",
"PA": "Pennsylvania",
"PR": "Puerto Rico",
"RI": "Rhode Island",
"SC": "South Carolina",
"SD": "South Dakota",
"TN": "Tennessee",
"TT": "Trust Territories",
"TX": "Texas",
"UT": "Utah",
"VA": "Virginia",
"VI": "Virgin Islands",
"VT": "Vermont",
"WA": "Washington",
"WI": "Wisconsin",
"WV": "West Virginia",
"WY": "Wyoming",
}
return state_abbreviations[abbreviation]
if __name__ == "__main__":
main()