Skip to content

Commit

Permalink
refactor(ni): swap arrow with datetime (#6616)
Browse files Browse the repository at this point in the history
  • Loading branch information
amv213 committed Apr 2, 2024
1 parent 8717b70 commit 4559ba4
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions parsers/NI.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env python3

from collections import defaultdict
from datetime import datetime
from datetime import datetime, time, timedelta
from logging import Logger, getLogger
from typing import Any
from zoneinfo import ZoneInfo

import arrow
from requests import Session

from electricitymap.contrib.lib.models.event_lists import (
Expand All @@ -17,7 +15,7 @@
from electricitymap.contrib.lib.types import ZoneKey
from parsers.lib.exceptions import ParserException

TIMEZONE = "America/Managua"
TIMEZONE = ZoneInfo("America/Managua")

MAP_URL = "http://www.cndc.org.ni/graficos/MapaSIN/index.php"
SUMMARY_URL = "http://www.cndc.org.ni/graficos/graficaGeneracion_Tipo_TReal0000.php"
Expand Down Expand Up @@ -92,14 +90,13 @@ def extract_text(full_text: str, start_text: str, end_text: str | None = None):
return full_text[start:end]


def get_time_from_system_map(text: str):
# date format is: "'Actualizado: 07/07/2017 01:00:50 PM'"

datetime_text = extract_text(text, "Actualizado: ", "'")
datetime_arrow = arrow.get(datetime_text, "DD/MM/YYYY hh:mm:ss A")
datetime_datetime = arrow.get(datetime_arrow.datetime, TIMEZONE).datetime

return datetime_datetime
def get_time_from_system_map(text: str) -> datetime:
# date format is: "'Información en Tiempo Real al 02/04/2024 05:57:40 AM'"
datetime_text = extract_text(text, "en Tiempo Real al ", "'")
# time is referring to local (NI) time
return datetime.strptime(datetime_text, "%d/%m/%Y %I:%M:%S %p").replace(
tzinfo=TIMEZONE
)


def get_production_from_map(requests_obj) -> tuple:
Expand Down Expand Up @@ -170,8 +167,7 @@ def get_production_from_summary(requests_obj) -> tuple:
datetime_text = extract_text(gentype_html, "Consultado a las ", "'")
hour = extract_text(datetime_text, "", " horas")
d = extract_text(datetime_text, "del dia ")
datetime_arrow = arrow.get(d + " " + hour, "DD/MM/YYYY HH")
datetime_datetime = arrow.get(datetime_arrow.datetime, TIMEZONE).datetime
dt = datetime.strptime(d + " " + hour, "%d/%m/%Y %H").replace(tzinfo=TIMEZONE)

gen_type_text = extract_text(gentype_html, "Tipo de Generación", "center:")
gen_type_text = extract_text(gen_type_text, "[")
Expand Down Expand Up @@ -204,7 +200,7 @@ def get_production_from_summary(requests_obj) -> tuple:

production = {k: sum(v) for k, v in production.items()}

return production, datetime_datetime
return production, dt


def fetch_production(
Expand Down Expand Up @@ -312,10 +308,10 @@ def fetch_price(
response.encoding = "utf-8"
prices_html = response.text

now_local_time = arrow.utcnow().to(TIMEZONE)
midnight_local_time = (
arrow.utcnow().to(TIMEZONE).replace(hour=0, minute=0, second=0, microsecond=0)
)
now_local_time = datetime.now(TIMEZONE)
midnight_local_time = datetime.combine(
now_local_time, time(), tzinfo=TIMEZONE
) # truncate to day

hours_text = prices_html.split("<br />")

Expand All @@ -328,15 +324,15 @@ def fetch_price(
hour = int(extract_text(hour_data, "Hora ", ":"))
price = float(extract_text(hour_data, "&nbsp; ").replace(",", "."))

price_date = midnight_local_time.replace(hour=hour)
price_date = midnight_local_time + timedelta(hours=hour)
if price_date > now_local_time:
# data for previous day is also included
price_date = price_date.replace(days=-1)
price_date = price_date - timedelta(days=1)

price_list = PriceList(logger)
price_list.append(
zoneKey=ZoneKey(zone_key),
datetime=price_date.datetime,
datetime=price_date,
price=price,
currency="USD",
source="cndc.org.ni",
Expand All @@ -352,8 +348,8 @@ def fetch_price(

print('fetch_exchange("NI", "HN") ->')
print(fetch_exchange("NI", "HN"))
print('fetch_exchange("NI", "CR") ->')
print(fetch_exchange("NI", "CR"))
# print('fetch_exchange("NI", "CR") ->')
# print(fetch_exchange("NI", "CR"))

print('fetch_price("NI") ->')
print(fetch_price("NI"))

0 comments on commit 4559ba4

Please sign in to comment.