Skip to content

Commit

Permalink
refactor(gb-ork): replace usage of 'arrow' library with Python built-…
Browse files Browse the repository at this point in the history
…in 'datetime' module. (electricitymaps#6831)

* refactor(gb-ork): replace usage of 'arrow' library with built-in 'datetime'.

* Fixup: remove unused import from GB-ORK parser.
  • Loading branch information
jayaddison authored Jun 23, 2024
1 parent d51fc19 commit 69836ee
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions parsers/GB_ORK.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

from datetime import datetime
from logging import Logger, getLogger
from zoneinfo import ZoneInfo

import arrow
import dateutil
from bs4 import BeautifulSoup
from requests import Response, Session

Expand All @@ -15,7 +14,7 @@
# There is a 2MW storage battery on the islands.
# https://www.oref.co.uk/orkneys-energy/innovations-2/

TZ = "Europe/London"
TIMEZONE = ZoneInfo("Europe/London")
DATETIME_LINK = "https://distribution.ssen.co.uk/anmorkneygraph/"
GENERATION_LINK = "https://distribution.ssen.co.uk/Sse_Components/Views/Controls/FormControls/Handlers/ActiveNetworkManagementHandler.ashx?action=graph&contentId=14973&_=1537467858726"

Expand Down Expand Up @@ -53,7 +52,7 @@ def get_json_data():
def get_datetime():
"""
Extracts data timestamp from html and checks it's less than 2 hours old.
Returns an arrow object.
Returns a Python datetime object.
"""
req: Response = get_session_with_legacy_adapter().get(DATETIME_LINK)
soup = BeautifulSoup(req.text, "html.parser")
Expand All @@ -62,18 +61,18 @@ def get_datetime():

last_updated = data_table.find("div", {"class": "button"}).contents
raw_dt = last_updated[-1].strip().split(" ", 1)[-1]
naive_dt = arrow.get(raw_dt, "DD MMMM YYYY HH:mm:ss")
aware_dt = naive_dt.replace(tzinfo=dateutil.tz.gettz(TZ))
naive_dt = datetime.strptime(raw_dt, "%d %B %Y %H:%M:%S")
aware_dt = naive_dt.replace(tzinfo=TIMEZONE)

current_time = arrow.now(TZ)
current_time = datetime.now(tz=TIMEZONE)
diff = current_time - aware_dt

if diff.total_seconds() > 7200:
raise ValueError(
f"Orkney data is too old to use, data is {int(diff.total_seconds() / 3600)} hours old."
)

return aware_dt.datetime
return aware_dt


def fetch_production(
Expand Down

0 comments on commit 69836ee

Please sign in to comment.