From fb9e2c6f9d62a77d722919746a047f038d20dcac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mu=C3=B1oz=20Novoa?= <49635897+davidmunoznovoa@users.noreply.github.com> Date: Sat, 16 Jul 2022 23:00:37 +0200 Subject: [PATCH] Add MIBGAS downloader --- README.md | 147 ++++++++++++++++++++---------- guess_omie_price/mibgas_getter.py | 35 +++++++ guess_omie_price/utils.py | 38 +++++++- requirements.txt | 1 + spec/mibgas_better_spec.py | 38 ++++++++ 5 files changed, 210 insertions(+), 49 deletions(-) create mode 100644 guess_omie_price/mibgas_getter.py create mode 100644 spec/mibgas_better_spec.py diff --git a/README.md b/README.md index a99fd20..7c5193f 100644 --- a/README.md +++ b/README.md @@ -24,30 +24,30 @@ print(*prices, sep='\n') # Resultado ``` -{'local_timestamp': '2022-04-13 01:00:00', 'utc_timestamp': '2022-04-12 23:00:00', 'value': 242.75} -{'local_timestamp': '2022-04-13 02:00:00', 'utc_timestamp': '2022-04-13 00:00:00', 'value': 229.77} -{'local_timestamp': '2022-04-13 03:00:00', 'utc_timestamp': '2022-04-13 01:00:00', 'value': 220.0} -{'local_timestamp': '2022-04-13 04:00:00', 'utc_timestamp': '2022-04-13 02:00:00', 'value': 201.85} -{'local_timestamp': '2022-04-13 05:00:00', 'utc_timestamp': '2022-04-13 03:00:00', 'value': 190.9} -{'local_timestamp': '2022-04-13 06:00:00', 'utc_timestamp': '2022-04-13 04:00:00', 'value': 212.32} -{'local_timestamp': '2022-04-13 07:00:00', 'utc_timestamp': '2022-04-13 05:00:00', 'value': 248.74} -{'local_timestamp': '2022-04-13 08:00:00', 'utc_timestamp': '2022-04-13 06:00:00', 'value': 257.02} -{'local_timestamp': '2022-04-13 09:00:00', 'utc_timestamp': '2022-04-13 07:00:00', 'value': 260.0} -{'local_timestamp': '2022-04-13 10:00:00', 'utc_timestamp': '2022-04-13 08:00:00', 'value': 254.25} -{'local_timestamp': '2022-04-13 11:00:00', 'utc_timestamp': '2022-04-13 09:00:00', 'value': 242.75} -{'local_timestamp': '2022-04-13 12:00:00', 'utc_timestamp': '2022-04-13 10:00:00', 'value': 234.99} -{'local_timestamp': '2022-04-13 13:00:00', 'utc_timestamp': '2022-04-13 11:00:00', 'value': 227.67} -{'local_timestamp': '2022-04-13 14:00:00', 'utc_timestamp': '2022-04-13 12:00:00', 'value': 208.0} -{'local_timestamp': '2022-04-13 15:00:00', 'utc_timestamp': '2022-04-13 13:00:00', 'value': 201.18} -{'local_timestamp': '2022-04-13 16:00:00', 'utc_timestamp': '2022-04-13 14:00:00', 'value': 196.0} -{'local_timestamp': '2022-04-13 17:00:00', 'utc_timestamp': '2022-04-13 15:00:00', 'value': 200.47} -{'local_timestamp': '2022-04-13 18:00:00', 'utc_timestamp': '2022-04-13 16:00:00', 'value': 221.2} -{'local_timestamp': '2022-04-13 19:00:00', 'utc_timestamp': '2022-04-13 17:00:00', 'value': 219.28} -{'local_timestamp': '2022-04-13 20:00:00', 'utc_timestamp': '2022-04-13 18:00:00', 'value': 242.36} -{'local_timestamp': '2022-04-13 21:00:00', 'utc_timestamp': '2022-04-13 19:00:00', 'value': 254.0} -{'local_timestamp': '2022-04-13 22:00:00', 'utc_timestamp': '2022-04-13 20:00:00', 'value': 259.0} -{'local_timestamp': '2022-04-13 23:00:00', 'utc_timestamp': '2022-04-13 21:00:00', 'value': 250.0} -{'local_timestamp': '2022-04-14 00:00:00', 'utc_timestamp': '2022-04-13 22:00:00', 'value': 231.94} +{'local_timestamp': '2022-04-13 01:00:00', 'utc_timestamp': '2022-04-12 23:00:00', 'value': 242.75, 'zone': 'es'} +{'local_timestamp': '2022-04-13 02:00:00', 'utc_timestamp': '2022-04-13 00:00:00', 'value': 229.77, 'zone': 'es'} +{'local_timestamp': '2022-04-13 03:00:00', 'utc_timestamp': '2022-04-13 01:00:00', 'value': 220.0, 'zone': 'es'} +{'local_timestamp': '2022-04-13 04:00:00', 'utc_timestamp': '2022-04-13 02:00:00', 'value': 201.85, 'zone': 'es'} +{'local_timestamp': '2022-04-13 05:00:00', 'utc_timestamp': '2022-04-13 03:00:00', 'value': 190.9, 'zone': 'es'} +{'local_timestamp': '2022-04-13 06:00:00', 'utc_timestamp': '2022-04-13 04:00:00', 'value': 212.32, 'zone': 'es'} +{'local_timestamp': '2022-04-13 07:00:00', 'utc_timestamp': '2022-04-13 05:00:00', 'value': 248.74, 'zone': 'es'} +{'local_timestamp': '2022-04-13 08:00:00', 'utc_timestamp': '2022-04-13 06:00:00', 'value': 257.02, 'zone': 'es'} +{'local_timestamp': '2022-04-13 09:00:00', 'utc_timestamp': '2022-04-13 07:00:00', 'value': 260.0, 'zone': 'es'} +{'local_timestamp': '2022-04-13 10:00:00', 'utc_timestamp': '2022-04-13 08:00:00', 'value': 254.25, 'zone': 'es'} +{'local_timestamp': '2022-04-13 11:00:00', 'utc_timestamp': '2022-04-13 09:00:00', 'value': 242.75, 'zone': 'es'} +{'local_timestamp': '2022-04-13 12:00:00', 'utc_timestamp': '2022-04-13 10:00:00', 'value': 234.99, 'zone': 'es'} +{'local_timestamp': '2022-04-13 13:00:00', 'utc_timestamp': '2022-04-13 11:00:00', 'value': 227.67, 'zone': 'es'} +{'local_timestamp': '2022-04-13 14:00:00', 'utc_timestamp': '2022-04-13 12:00:00', 'value': 208.0, 'zone': 'es'} +{'local_timestamp': '2022-04-13 15:00:00', 'utc_timestamp': '2022-04-13 13:00:00', 'value': 201.18, 'zone': 'es'} +{'local_timestamp': '2022-04-13 16:00:00', 'utc_timestamp': '2022-04-13 14:00:00', 'value': 196.0, 'zone': 'es'} +{'local_timestamp': '2022-04-13 17:00:00', 'utc_timestamp': '2022-04-13 15:00:00', 'value': 200.47, 'zone': 'es'} +{'local_timestamp': '2022-04-13 18:00:00', 'utc_timestamp': '2022-04-13 16:00:00', 'value': 221.2, 'zone': 'es'} +{'local_timestamp': '2022-04-13 19:00:00', 'utc_timestamp': '2022-04-13 17:00:00', 'value': 219.28, 'zone': 'es'} +{'local_timestamp': '2022-04-13 20:00:00', 'utc_timestamp': '2022-04-13 18:00:00', 'value': 242.36, 'zone': 'es'} +{'local_timestamp': '2022-04-13 21:00:00', 'utc_timestamp': '2022-04-13 19:00:00', 'value': 254.0, 'zone': 'es'} +{'local_timestamp': '2022-04-13 22:00:00', 'utc_timestamp': '2022-04-13 20:00:00', 'value': 259.0, 'zone': 'es'} +{'local_timestamp': '2022-04-13 23:00:00', 'utc_timestamp': '2022-04-13 21:00:00', 'value': 250.0, 'zone': 'es'} +{'local_timestamp': '2022-04-14 00:00:00', 'utc_timestamp': '2022-04-13 22:00:00', 'value': 231.94, 'zone': 'es'} ``` - Descargar `Precio horario de ajuste a los consumidores en el mercado` @@ -64,28 +64,79 @@ print(*prices, sep='\n') # Resultado ``` -{'local_timestamp': '2022-06-15 01:00:00', 'utc_timestamp': '2022-06-14 23:00:00', 'value': 65.22} -{'local_timestamp': '2022-06-15 02:00:00', 'utc_timestamp': '2022-06-15 00:00:00', 'value': 71.27} -{'local_timestamp': '2022-06-15 03:00:00', 'utc_timestamp': '2022-06-15 01:00:00', 'value': 75.24} -{'local_timestamp': '2022-06-15 04:00:00', 'utc_timestamp': '2022-06-15 02:00:00', 'value': 79.33} -{'local_timestamp': '2022-06-15 05:00:00', 'utc_timestamp': '2022-06-15 03:00:00', 'value': 79.33} -{'local_timestamp': '2022-06-15 06:00:00', 'utc_timestamp': '2022-06-15 04:00:00', 'value': 80.71} -{'local_timestamp': '2022-06-15 07:00:00', 'utc_timestamp': '2022-06-15 05:00:00', 'value': 79.0} -{'local_timestamp': '2022-06-15 08:00:00', 'utc_timestamp': '2022-06-15 06:00:00', 'value': 70.84} -{'local_timestamp': '2022-06-15 09:00:00', 'utc_timestamp': '2022-06-15 07:00:00', 'value': 61.46} -{'local_timestamp': '2022-06-15 10:00:00', 'utc_timestamp': '2022-06-15 08:00:00', 'value': 56.1} -{'local_timestamp': '2022-06-15 11:00:00', 'utc_timestamp': '2022-06-15 09:00:00', 'value': 52.13} -{'local_timestamp': '2022-06-15 12:00:00', 'utc_timestamp': '2022-06-15 10:00:00', 'value': 48.45} -{'local_timestamp': '2022-06-15 13:00:00', 'utc_timestamp': '2022-06-15 11:00:00', 'value': 46.2} -{'local_timestamp': '2022-06-15 14:00:00', 'utc_timestamp': '2022-06-15 12:00:00', 'value': 45.5} -{'local_timestamp': '2022-06-15 15:00:00', 'utc_timestamp': '2022-06-15 13:00:00', 'value': 44.08} -{'local_timestamp': '2022-06-15 16:00:00', 'utc_timestamp': '2022-06-15 14:00:00', 'value': 44.82} -{'local_timestamp': '2022-06-15 17:00:00', 'utc_timestamp': '2022-06-15 15:00:00', 'value': 46.32} -{'local_timestamp': '2022-06-15 18:00:00', 'utc_timestamp': '2022-06-15 16:00:00', 'value': 48.12} -{'local_timestamp': '2022-06-15 19:00:00', 'utc_timestamp': '2022-06-15 17:00:00', 'value': 50.7} -{'local_timestamp': '2022-06-15 20:00:00', 'utc_timestamp': '2022-06-15 18:00:00', 'value': 53.55} -{'local_timestamp': '2022-06-15 21:00:00', 'utc_timestamp': '2022-06-15 19:00:00', 'value': 55.4} -{'local_timestamp': '2022-06-15 22:00:00', 'utc_timestamp': '2022-06-15 20:00:00', 'value': 56.44} -{'local_timestamp': '2022-06-15 23:00:00', 'utc_timestamp': '2022-06-15 21:00:00', 'value': 57.79} -{'local_timestamp': '2022-06-16 00:00:00', 'utc_timestamp': '2022-06-15 22:00:00', 'value': 64.2} +{'local_timestamp': '2022-06-15 01:00:00', 'utc_timestamp': '2022-06-14 23:00:00', 'value': 65.22, 'zone': 'es'} +{'local_timestamp': '2022-06-15 02:00:00', 'utc_timestamp': '2022-06-15 00:00:00', 'value': 71.27, 'zone': 'es'} +{'local_timestamp': '2022-06-15 03:00:00', 'utc_timestamp': '2022-06-15 01:00:00', 'value': 75.24, 'zone': 'es'} +{'local_timestamp': '2022-06-15 04:00:00', 'utc_timestamp': '2022-06-15 02:00:00', 'value': 79.33, 'zone': 'es'} +{'local_timestamp': '2022-06-15 05:00:00', 'utc_timestamp': '2022-06-15 03:00:00', 'value': 79.33, 'zone': 'es'} +{'local_timestamp': '2022-06-15 06:00:00', 'utc_timestamp': '2022-06-15 04:00:00', 'value': 80.71, 'zone': 'es'} +{'local_timestamp': '2022-06-15 07:00:00', 'utc_timestamp': '2022-06-15 05:00:00', 'value': 79.0, 'zone': 'es'} +{'local_timestamp': '2022-06-15 08:00:00', 'utc_timestamp': '2022-06-15 06:00:00', 'value': 70.84, 'zone': 'es'} +{'local_timestamp': '2022-06-15 09:00:00', 'utc_timestamp': '2022-06-15 07:00:00', 'value': 61.46, 'zone': 'es'} +{'local_timestamp': '2022-06-15 10:00:00', 'utc_timestamp': '2022-06-15 08:00:00', 'value': 56.1, 'zone': 'es'} +{'local_timestamp': '2022-06-15 11:00:00', 'utc_timestamp': '2022-06-15 09:00:00', 'value': 52.13, 'zone': 'es'} +{'local_timestamp': '2022-06-15 12:00:00', 'utc_timestamp': '2022-06-15 10:00:00', 'value': 48.45, 'zone': 'es'} +{'local_timestamp': '2022-06-15 13:00:00', 'utc_timestamp': '2022-06-15 11:00:00', 'value': 46.2, 'zone': 'es'} +{'local_timestamp': '2022-06-15 14:00:00', 'utc_timestamp': '2022-06-15 12:00:00', 'value': 45.5, 'zone': 'es'} +{'local_timestamp': '2022-06-15 15:00:00', 'utc_timestamp': '2022-06-15 13:00:00', 'value': 44.08, 'zone': 'es'} +{'local_timestamp': '2022-06-15 16:00:00', 'utc_timestamp': '2022-06-15 14:00:00', 'value': 44.82, 'zone': 'es'} +{'local_timestamp': '2022-06-15 17:00:00', 'utc_timestamp': '2022-06-15 15:00:00', 'value': 46.32, 'zone': 'es'} +{'local_timestamp': '2022-06-15 18:00:00', 'utc_timestamp': '2022-06-15 16:00:00', 'value': 48.12, 'zone': 'es'} +{'local_timestamp': '2022-06-15 19:00:00', 'utc_timestamp': '2022-06-15 17:00:00', 'value': 50.7, 'zone': 'es'} +{'local_timestamp': '2022-06-15 20:00:00', 'utc_timestamp': '2022-06-15 18:00:00', 'value': 53.55, 'zone': 'es'} +{'local_timestamp': '2022-06-15 21:00:00', 'utc_timestamp': '2022-06-15 19:00:00', 'value': 55.4, 'zone': 'es'} +{'local_timestamp': '2022-06-15 22:00:00', 'utc_timestamp': '2022-06-15 20:00:00', 'value': 56.44, 'zone': 'es'} +{'local_timestamp': '2022-06-15 23:00:00', 'utc_timestamp': '2022-06-15 21:00:00', 'value': 57.79, 'zone': 'es'} +{'local_timestamp': '2022-06-16 00:00:00', 'utc_timestamp': '2022-06-15 22:00:00', 'value': 64.2, 'zone': 'es'} +``` + +- Descargar `Precio marginal de compra de MIBGAS` + +```python +from datetime import datetime +from guess_omie_price.mibgas_getter import download_pmc_on_date + +d = datetime(2022, 6, 15) +prices = download_pmc_on_date(d) + +print(*prices, sep='\n') +``` + +# Resultado +``` +# Prices are ordered descending +{'value': 52.7, 'local_timestamp': '2022-01-01 00:00:00', 'utc_timestamp': '2021-12-31 23:00:00', 'zone': 'es'} +{'value': 56.25, 'local_timestamp': '2022-01-02 00:00:00', 'utc_timestamp': '2022-01-01 23:00:00', 'zone': 'es'} +{'value': 79.0, 'local_timestamp': '2022-01-03 00:00:00', 'utc_timestamp': '2022-01-02 23:00:00', 'zone': 'es'} +{'value': 81.72, 'local_timestamp': '2022-01-04 00:00:00', 'utc_timestamp': '2022-01-03 23:00:00', 'zone': 'es'} +{'value': 91.54, 'local_timestamp': '2022-01-05 00:00:00', 'utc_timestamp': '2022-01-04 23:00:00', 'zone': 'es'} +{'value': 95.8, 'local_timestamp': '2022-01-06 00:00:00', 'utc_timestamp': '2022-01-05 23:00:00', 'zone': 'es'} +{'value': 94.67, 'local_timestamp': '2022-01-07 00:00:00', 'utc_timestamp': '2022-01-06 23:00:00', 'zone': 'es'} +{'value': 89.06, 'local_timestamp': '2022-01-08 00:00:00', 'utc_timestamp': '2022-01-07 23:00:00', 'zone': 'es'} +{'value': 88.17, 'local_timestamp': '2022-01-09 00:00:00', 'utc_timestamp': '2022-01-08 23:00:00', 'zone': 'es'} +{'value': 89.43, 'local_timestamp': '2022-01-10 00:00:00', 'utc_timestamp': '2022-01-09 23:00:00', 'zone': 'es'} +{'value': 87.09, 'local_timestamp': '2022-01-11 00:00:00', 'utc_timestamp': '2022-01-10 23:00:00', 'zone': 'es'} +{'value': 82.43, 'local_timestamp': '2022-01-12 00:00:00', 'utc_timestamp': '2022-01-11 23:00:00', 'zone': 'es'} +{'value': 79.02, 'local_timestamp': '2022-01-13 00:00:00', 'utc_timestamp': '2022-01-12 23:00:00', 'zone': 'es'} +{'value': 92.5, 'local_timestamp': '2022-01-14 00:00:00', 'utc_timestamp': '2022-01-13 23:00:00', 'zone': 'es'} +{'value': 91.33, 'local_timestamp': '2022-01-15 00:00:00', 'utc_timestamp': '2022-01-14 23:00:00', 'zone': 'es'} +{'value': 93.55, 'local_timestamp': '2022-01-16 00:00:00', 'utc_timestamp': '2022-01-15 23:00:00', 'zone': 'es'} +{'value': 88.72, 'local_timestamp': '2022-01-17 00:00:00', 'utc_timestamp': '2022-01-16 23:00:00', 'zone': 'es'} +{'value': 82.57, 'local_timestamp': '2022-01-18 00:00:00', 'utc_timestamp': '2022-01-17 23:00:00', 'zone': 'es'} +{'value': 78.53, 'local_timestamp': '2022-01-19 00:00:00', 'utc_timestamp': '2022-01-18 23:00:00', 'zone': 'es'} +{'value': 74.89, 'local_timestamp': '2022-01-20 00:00:00', 'utc_timestamp': '2022-01-19 23:00:00', 'zone': 'es'} +{'value': 76.11, 'local_timestamp': '2022-01-21 00:00:00', 'utc_timestamp': '2022-01-20 23:00:00', 'zone': 'es'} +{'value': 82.38, 'local_timestamp': '2022-01-22 00:00:00', 'utc_timestamp': '2022-01-21 23:00:00', 'zone': 'es'} +{'value': 85.75, 'local_timestamp': '2022-01-23 00:00:00', 'utc_timestamp': '2022-01-22 23:00:00', 'zone': 'es'} +{'value': 89.19, 'local_timestamp': '2022-01-24 00:00:00', 'utc_timestamp': '2022-01-23 23:00:00', 'zone': 'es'} +{'value': 91.24, 'local_timestamp': '2022-01-25 00:00:00', 'utc_timestamp': '2022-01-24 23:00:00', 'zone': 'es'} +{'value': 93.3, 'local_timestamp': '2022-01-26 00:00:00', 'utc_timestamp': '2022-01-25 23:00:00', 'zone': 'es'} +{'value': 93.03, 'local_timestamp': '2022-01-27 00:00:00', 'utc_timestamp': '2022-01-26 23:00:00', 'zone': 'es'} +{'value': 93.53, 'local_timestamp': '2022-01-28 00:00:00', 'utc_timestamp': '2022-01-27 23:00:00', 'zone': 'es'} +{'value': 94.99, 'local_timestamp': '2022-01-29 00:00:00', 'utc_timestamp': '2022-01-28 23:00:00', 'zone': 'es'} +{'value': 96.47, 'local_timestamp': '2022-01-30 00:00:00', 'utc_timestamp': '2022-01-29 23:00:00', 'zone': 'es'} +{'value': 89.38, 'local_timestamp': '2022-01-31 00:00:00', 'utc_timestamp': '2022-01-30 23:00:00', 'zone': 'es'} +{'value': 81.67, 'local_timestamp': '2022-02-01 00:00:00', 'utc_timestamp': '2022-01-31 23:00:00', 'zone': 'es'} +... +# Note: File continues till last publish price for specified year ``` \ No newline at end of file diff --git a/guess_omie_price/mibgas_getter.py b/guess_omie_price/mibgas_getter.py new file mode 100644 index 0000000..75c2884 --- /dev/null +++ b/guess_omie_price/mibgas_getter.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +from guess_omie_price.utils import download_mibgas_component_on_date +import logging +import zipfile + + +def download_pmc_on_date(today, zone='es'): + """ + Downloads PMC data from MIBGAS + :param today: datetime + :param zone: str, must be in ('es', 'pt') + :return: list of dict + """ + logger = logging.getLogger('MIBGAS') + logger.info(f'Descargando precios PMC para la fecha {today}.') + + year = today.year + + if year < 2019: + sheet_number = 6 + else: + sheet_number = 7 + + try: + base_url = f'https://www.mibgas.es/es/file-access/' + file_url = f'MIBGAS_Data_{year}.xlsx?path=AGNO_{year}/XLS' + + return download_mibgas_component_on_date(base_url, file_url, sheet_number, zone=zone) + + except IndexError: + logger.info(f'No se han podido descargar los precios PMC en fecha {today}.') + return [] + except zipfile.BadZipFile: + logger.info(f'No se han podido descargar los precios PMC en fecha {today}.') + return [] diff --git a/guess_omie_price/utils.py b/guess_omie_price/utils.py index 33c7ecf..1ce2aab 100644 --- a/guess_omie_price/utils.py +++ b/guess_omie_price/utils.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from datetime import timedelta from guess_omie_price.headers import ZONE_MAP -from io import StringIO +from io import StringIO, BytesIO from pytz import timezone import pandas as pd import requests @@ -40,8 +40,44 @@ def download_omie_component_on_date(today, base_url, file_url, file_header, zone register['local_timestamp'] = local_timestamp.strftime('%Y-%m-%d %H:%M:%S') register['utc_timestamp'] = utc_timestamp.strftime('%Y-%m-%d %H:%M:%S') register['value'] = val + register['zone'] = zone.lower() res.append(register) current_utc_timestamp += timedelta(hours=1) return res + + +def download_mibgas_component_on_date(base_url, file_url, sheet_number, zone): + """ + Downloads component data from MIBGAS + :param base_url: str + :param file_url: str + :param sheet_number: int + :param zone: str, must be in ('es', 'pt') + :return: list of dict + """ + url = base_url + file_url + r = requests.get(url) + io = BytesIO(r.content) + + column = pd.read_excel(io, sheet_name=sheet_number, usecols=[0, 1, 5], engine='openpyxl') + column.rename(columns={'Marginal purchase price\n[EUR/MWh]': 'value'}, inplace=True) + + data = column.T.to_dict().values() + + res = [] + for register in data: + if register['Zone'] != zone.upper(): + continue + local_timestamp = LOCAL_TZ.localize(register['Date'], '%Y-%m-%d %H:%M:%S') + utc_timestamp = UTC_TZ.normalize(local_timestamp.astimezone(UTC_TZ)) + register.pop('Date') + register.pop('Zone') + register['local_timestamp'] = local_timestamp.strftime('%Y-%m-%d %H:%M:%S') + register['utc_timestamp'] = utc_timestamp.strftime('%Y-%m-%d %H:%M:%S') + register['zone'] = zone.lower() + + res = [register] + res + + return res diff --git a/requirements.txt b/requirements.txt index a61f842..c3b91bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +openpyxl pandas pytz requests \ No newline at end of file diff --git a/spec/mibgas_better_spec.py b/spec/mibgas_better_spec.py new file mode 100644 index 0000000..47e0aaf --- /dev/null +++ b/spec/mibgas_better_spec.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +from datetime import datetime, timedelta +from guess_omie_price.mibgas_getter import download_pmc_on_date +from mamba import context, description, it + +with description('PMC'): + with context('PMC getter'): + with it('Downloads prices for a valid datetime'): + d = datetime(2022, 7, 16) + prices = download_pmc_on_date(d) + + res_prices = [x['value'] for x in prices] + + expected_price = 106.66 + assert res_prices[197-1] == expected_price # 2022-07-16 is the year number 197 on 2022 + + with it('Does not fail execution for an invalid datetime'): + d = datetime.today() + timedelta(days=365) # next year must not be published yet + prices = download_pmc_on_date(d) + assert len(prices) == 0 + assert prices == [] + + with it('Downloads prices for specified zone (Spain by default)'): + d = datetime(2022, 7, 16) + prices = download_pmc_on_date(d) + + res_prices = [x['value'] for x in prices] + + expected_price = 106.66 + assert res_prices[197-1] == expected_price # 2022-07-16 is the year number 197 on 2022 + + d = datetime(2022, 7, 16) + prices = download_pmc_on_date(d, 'pt') + + res_prices = [x['value'] for x in prices] + + expected_price = 108.20 + assert res_prices[197-1] == expected_price # 2022-07-16 is the year number 197 on 2022