Skip to content

Commit

Permalink
remove delay, check data, time zone TZ, issue #7351 (#7745)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-couery authored Jan 23, 2025
1 parent 0089297 commit 00fdf9f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions parsers/ENERCAL.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from datetime import datetime, timedelta
from datetime import datetime
from logging import Logger, getLogger
from zoneinfo import ZoneInfo

Expand All @@ -9,6 +9,8 @@
from electricitymap.contrib.lib.models.events import ProductionMix
from electricitymap.contrib.lib.types import ZoneKey

from .lib.exceptions import ParserException

TZ = ZoneInfo("Pacific/Noumea")

SOURCE = "enercal.nc"
Expand All @@ -24,14 +26,13 @@ def fetch_production(
) -> list:
session = session or Session()

sixteen_weeks_ago = datetime.now(tz=TZ) - timedelta(weeks=16)
if target_datetime and target_datetime.tzinfo is None:
target_datetime = target_datetime.replace(tzinfo=TZ)

target_datetime = (
target_datetime
if target_datetime and target_datetime > sixteen_weeks_ago
else sixteen_weeks_ago
)
if target_datetime is None:
target_datetime = datetime.now(tz=TZ)

# No data since 2024-06-30, but the API is still working
data = session.get(
"https://www.enercal.nc/wp-content/themes/enercal/ajax-e-co2.php",
params={"date_day": target_datetime.strftime("%Y-%m-%d")},
Expand All @@ -44,6 +45,13 @@ def fetch_production(
time = json.loads(data["time"][0])
prettyTime = json.loads(data["prettyTime"][0])

if not time:
raise ParserException(
"ENERCAL.py",
f"This parser cannot retrieve data for: {target_datetime}",
ZoneKey("NC"),
)

# Reformat the data to add key value pairs instead of using index values.
production_data = []
for i in range(len(time)):
Expand Down

0 comments on commit 00fdf9f

Please sign in to comment.