Skip to content

Commit

Permalink
remove arrow NG.py (#6696)
Browse files Browse the repository at this point in the history
* remove arrow NG.py

* Update parsers/NG.py

Co-authored-by: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com>

---------

Co-authored-by: jiajunln <jiajunln@umich.edu>
Co-authored-by: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com>
  • Loading branch information
3 people committed May 2, 2024
1 parent 6421e87 commit c974e8b
Showing 1 changed file with 11 additions and 9 deletions.
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

0 comments on commit c974e8b

Please sign in to comment.