Skip to content

Commit

Permalink
PE parser: add unit tests and remove usage of arrow module (#6481)
Browse files Browse the repository at this point in the history
* PE parser: added snapshot test

* PE parser: using different dates for snapshot test mock files, in order to have full data

* PE parser: reduced size of snapshot test mock files

* PE parser: added test test_api_requests_are_sent_with_correct_dates

* PE parser: remove usage of arrow, part 1

* PE parser: remove usage of arrow, part 2

* PE parser: simplify test test_api_requests_are_sent_with_correct_dates

* PE parser: use urlencode in test test_api_requests_are_sent_with_correct_dates

* PE parser: simplify test test_api_requests_are_sent_with_correct_dates

* PE parser: added test test_result_list_elements_do_not_contain_storage_key

* PE parser: minor change to improve code readability

---------

Co-authored-by: Viktor Andersson <30777521+VIKTORVAV99@users.noreply.github.com>
  • Loading branch information
gianantoniopini and VIKTORVAV99 committed Feb 16, 2024
1 parent 5b5bfc2 commit 8854fdd
Show file tree
Hide file tree
Showing 5 changed files with 7,597 additions and 14 deletions.
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

0 comments on commit 8854fdd

Please sign in to comment.