Skip to content

Commit

Permalink
cached sun pos data used even if old
Browse files Browse the repository at this point in the history
  • Loading branch information
justjokiing committed Jan 7, 2025
1 parent 8867a9b commit 998d4ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "kshift"
version = "0.2.1"
version = "0.2.2"
authors = [
{ name="Seth Mackert", email="[email protected]" },
]
Expand Down
39 changes: 27 additions & 12 deletions src/kshift/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,24 @@ def web_sundata(self, sunstate):
with open(self.api_file, "w") as file:
json.dump(cache_data, file)

except (requests.RequestException, requests.ConnectionError, KeyError,
ValueError) as e:
print(f"Error fetching sun data: {e}")
raise
except requests.exceptions.ConnectionError as e:
print(
f"Connection error: {e}. Could not connect to {url}. Falling back to defaults."
)
except requests.exceptions.Timeout as e:
print(
f"Timeout error: {e}. The request to {url} took too long. Falling back to defaults."
)
except requests.exceptions.HTTPError as e:
print(
f"HTTP error: {e}. Invalid response from the server. Falling back to defaults."
)
except json.JSONDecodeError as e:
print(
f"JSON decoding error: {e}. Invalid response format. Falling back to defaults."
)
except Exception as e:
print(f"Unexpected error: {e}. Falling back to defaults.")

return self._select_sunstate(sunstate)

Expand All @@ -381,17 +395,18 @@ def get_sundata(self, sunstate):
cache_data["sunset"] = datetime.fromisoformat(
cache_data["sunset"])

if (cache_data["location"]
== f"{self.latitude},{self.longitude}"
and cache_data["sunrise"].date()
== datetime.today().date()
and cache_data["sunset"].date()
== datetime.today().date()):
if cache_data[
"location"] == f"{self.latitude},{self.longitude}":

self.sunrise = cache_data["sunrise"]
self.sunset = cache_data["sunset"]
self.sunrise = datetime.combine(datetime.now().date(),
cache_data["sunrise"].time())
self.sunset = datetime.combine(datetime.now().date(),
cache_data["sunset"].time())

if cache_data["sunrise"].date() == datetime.today().date(
) and cache_data["sunset"].date() == datetime.today().date():
return self._select_sunstate(sunstate)

except (ValueError, IndexError):
pass # Fall back to fetching data if the cache is corrupted

Expand Down

0 comments on commit 998d4ea

Please sign in to comment.