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

remove arrow NG.py #6696

Merged
merged 4 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions parsers/NG.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

import re
import urllib.parse
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from logging import Logger, getLogger
from zoneinfo import ZoneInfo

import arrow
import bs4
from requests import Session

Expand All @@ -27,6 +27,7 @@
"steam": "gas",
}
PATTERN = re.compile(r"\((.*)\)")
TIMEZONE = ZoneInfo("Africa/Lagos")


# The data is hourly, but it takes a few minutes after the turn of each hour
Expand All @@ -42,19 +43,20 @@ def fetch_production(
logger: Logger = getLogger(__name__),
) -> dict:
"""Requests the last known production mix (in MW) of a given zone."""
timestamp = (
arrow.get(target_datetime)
.to("Africa/Lagos")
.replace(minute=0, second=0, microsecond=0)

if target_datetime is None:
target_datetime = datetime.now(timezone.utc)
timestamp = target_datetime.replace(minute=0, second=0, microsecond=0).astimezone(
TIMEZONE
)

# GET the landing page (HTML) and scrape some form data from it.
session = session or Session()
response = session.get(API_URL_STRING)
soup = bs4.BeautifulSoup(response.text, "html.parser")
data = {tag["name"]: tag["value"] for tag in soup.find_all("input")}
data["ctl00$MainContent$txtReadingDate"] = timestamp.format("DD/MM/YYYY")
data["ctl00$MainContent$ddlTime"] = timestamp.format("HH:mm")
data["ctl00$MainContent$txtReadingDate"] = timestamp.strftime("%d/%m/%Y")
data["ctl00$MainContent$ddlTime"] = timestamp.strftime("%H:%M")

# Send a POST request for the desired grid data using parameters from the
# landing page form. The grid data is presented as an HTML table; we ignore
Expand All @@ -75,7 +77,7 @@ def fetch_production(
return validation.validate(
{
"zoneKey": zone_key,
"datetime": timestamp.datetime,
"datetime": timestamp,
"production": production_mix,
"source": API_URL.netloc,
},
Expand Down