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

Add requests checking and retry to solar flare fetch #92

Merged
merged 3 commits into from
Mar 4, 2025
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
14 changes: 14 additions & 0 deletions get_solar_flare_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from urllib.parse import urljoin

import requests
from ska_helpers import retry

URL = "https://www.solen.info/solar/index.html"
IMAGE_SRC_PATTERN = r"<img src=\"(images/AR_CH_\d{8}\.png)\""
Expand All @@ -23,6 +24,12 @@ def get_options():
return parser


@retry.retry(
exceptions=requests.exceptions.RequestException,
delay=5,
tries=3,
mangle_alert_words=True,
)
def get_last_referenced_web_image(
url: str, img_src_pattern: str, cache_dir: str | Path
) -> Path:
Expand Down Expand Up @@ -55,6 +62,8 @@ def get_last_referenced_web_image(

# Fetch the web page and get the html
response = requests.get(url)
# Check that the request was successful
response.raise_for_status()
html = response.text

# get absolute url of the image that matches the supplied pattern
Expand All @@ -80,6 +89,9 @@ def get_last_referenced_web_image(

# Download the new image and save it to the cache directory
response = requests.get(img_url)
# Check that the request was successful
response.raise_for_status()

with open(cached_image_file, "wb") as f:
f.write(response.content)

Expand All @@ -102,6 +114,8 @@ def main(sys_args=None):
if __name__ == "__main__":
try:
main()
except requests.exceptions.HTTPError as e:
print(f"Failed to get page or image: {e}")
except Exception:
import traceback

Expand Down