Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework MD #670 #671

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions warn/scrapers/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ def scrape(
# Set the cache
cache = Cache(cache_dir)

# In November 2024 Maryland began throwing out many failed connection messages. These two things helped.
request_headers = {"User-Agent": "BigLocalNews.org"}
request_verify = False

# Get the page
url = "https://www.dllr.state.md.us/employment/warn.shtml"
r = utils.get_url(url)
r = utils.get_url(url, headers=request_headers, verify=request_verify)
r.encoding = "utf-8"
html = r.text

Expand All @@ -56,17 +60,41 @@ def scrape(
html_list = []
html_list.append(html) # Save the source HTML for parsing also

old_pages = [
"warn2023.shtml",
"warn2022.shtml",
"warn2021.shtml",
"warn2020.shtml",
"warn2019.shtml",
"warn2018.shtml",
"warn2017.shtml",
"warn2016.shtml",
"warn2015.shtml",
"warn2014.shtml",
"warn2013.shtml",
"warn2012.shtml",
"warn2011.shtml",
"warn2010.shtml",
]

for href in href_list:
# Request the HTML
url = f"https://www.dllr.state.md.us/employment/{href}"
r = utils.get_url(url)
r.encoding = "utf-8"
html = r.text

# Save it to the cache
cache.write(f"md/{href}.html", html)

sleep(naptime) # Try to stop blocked connections by being less aggressive
filename = cache_dir / f"md/{href}.html"

if href not in old_pages:
sleep(naptime) # Try to stop blocked connections by being less aggressive
r = utils.get_url(url, headers=request_headers, verify=request_verify)
r.encoding = "utf-8"
html = r.text

# Save it to the cache
cache.write(filename, html)
else:
r = utils.fetch_if_not_cached(
filename, url, headers=request_headers, verify=request_verify
)
html = cache.read(filename)

# Add it to the list
html_list.append(html)
Expand Down