-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8e3f1b
commit fb9e2c6
Showing
5 changed files
with
210 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
openpyxl | ||
pandas | ||
pytz | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |