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

PE parser: add unit tests and remove usage of arrow module #6481

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions parsers/PE.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env python3

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

import arrow
import dateutil
from requests import Session

from .lib.validation import validate

tz = "America/Lima"
API_ENDPOINT = "https://www.coes.org.pe/Portal/portalinformacion/generacion"

TIMEZONE = ZoneInfo("America/Lima")

MAP_GENERATION = {
"DIESEL": "oil",
Expand All @@ -25,8 +26,8 @@


def parse_date(item):
return arrow.get(item["Nombre"], "YYYY/MM/DD hh:mm:ss").replace(
tzinfo=dateutil.tz.gettz(tz)
return datetime.strptime(item["Nombre"], "%Y/%m/%d %H:%M:%S").replace(
tzinfo=TIMEZONE
)


Expand All @@ -41,22 +42,24 @@ def fetch_production(
raise NotImplementedError("This parser is not yet able to parse past dates")

r = session or Session()
url = "https://www.coes.org.pe/Portal/portalinformacion/generacion"

current_date = arrow.now(tz=tz)
current_date = datetime.now(tz=TIMEZONE)

today = current_date.format("DD/MM/YYYY")
yesterday = current_date.shift(days=-1).format("DD/MM/YYYY")
end_date = current_date.shift(days=+1).format("DD/MM/YYYY")
date_format = "%d/%m/%Y"
today = current_date.strftime(date_format)
yesterday = (current_date + timedelta(days=-1)).strftime(date_format)
end_date = (current_date + timedelta(days=+1)).strftime(date_format)

# To guarantee a full 24 hours of data we must make 2 requests.

response_today = r.post(
url, data={"fechaInicial": today, "fechaFinal": end_date, "indicador": 0}
API_ENDPOINT,
data={"fechaInicial": today, "fechaFinal": end_date, "indicador": 0},
)

response_yesterday = r.post(
url, data={"fechaInicial": yesterday, "fechaFinal": today, "indicador": 0}
API_ENDPOINT,
data={"fechaInicial": yesterday, "fechaFinal": today, "indicador": 0},
)

data_today = response_today.json()["GraficoTipoCombustible"]["Series"]
Expand Down Expand Up @@ -86,7 +89,7 @@ def fetch_production(
data.append(
{
"zoneKey": zone_key,
"datetime": dt.datetime,
"datetime": dt,
"production": {},
"source": "coes.org.pe",
}
Expand Down
Loading