python-esios 0.2.1__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-esios
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: A Python wrapper for the ESIOS API
5
5
  Home-page: https://github.com/datons/python-esios
6
6
  Author: Jesús López
@@ -26,8 +26,26 @@ This library provides a simple interface to download and preprocess the data fro
26
26
  pip install python-esios
27
27
  ```
28
28
 
29
+ ## Get token
30
+
31
+ Ask for a personal token to access the ESIOS API following the [instructions from REE](https://www.esios.ree.es/es/pagina/api).
32
+
29
33
  ## Usage
30
34
 
35
+ ### Register the token in Python
36
+
37
+ ```python
38
+ TOKEN = '343sdfewe342309gjarijgwoiret834383434524...'
39
+ TOKEN = '<YOUR_TOKEN>'
40
+ ```
41
+
42
+ Then, set the token in the environment variable `ESIOS_API_KEY`.
43
+
44
+ ```python
45
+ import os
46
+ os.environ['ESIOS_API_KEY'] = TOKEN
47
+ ```
48
+
31
49
  ### Instantiate the client
32
50
 
33
51
  ```python
@@ -43,5 +61,5 @@ endpoint = client.endpoint(name=?)
43
61
 
44
62
  In the tutorials below, you will learn how to download, preprocess, and visualize the data from the following endpoints:
45
63
 
46
- - [Indicators](https://github.com/datons/python-esios/blob/main/examples/20_Indicators/0_Steps.ipynb)
47
- - [Archives](https://github.com/datons/python-esios/blob/main/examples/30_Archives/0_Steps.ipynb)
64
+ - [Indicators](https://github.com/datons/python-esios/blob/main/examples/20_Indicators/0_Steps/B1_Download.ipynb)
65
+ - [Archives](https://github.com/datons/python-esios/blob/main/examples/30_Archives/0_Steps/B1_Download.ipynb)
@@ -10,8 +10,26 @@ This library provides a simple interface to download and preprocess the data fro
10
10
  pip install python-esios
11
11
  ```
12
12
 
13
+ ## Get token
14
+
15
+ Ask for a personal token to access the ESIOS API following the [instructions from REE](https://www.esios.ree.es/es/pagina/api).
16
+
13
17
  ## Usage
14
18
 
19
+ ### Register the token in Python
20
+
21
+ ```python
22
+ TOKEN = '343sdfewe342309gjarijgwoiret834383434524...'
23
+ TOKEN = '<YOUR_TOKEN>'
24
+ ```
25
+
26
+ Then, set the token in the environment variable `ESIOS_API_KEY`.
27
+
28
+ ```python
29
+ import os
30
+ os.environ['ESIOS_API_KEY'] = TOKEN
31
+ ```
32
+
15
33
  ### Instantiate the client
16
34
 
17
35
  ```python
@@ -27,5 +45,5 @@ endpoint = client.endpoint(name=?)
27
45
 
28
46
  In the tutorials below, you will learn how to download, preprocess, and visualize the data from the following endpoints:
29
47
 
30
- - [Indicators](https://github.com/datons/python-esios/blob/main/examples/20_Indicators/0_Steps.ipynb)
31
- - [Archives](https://github.com/datons/python-esios/blob/main/examples/30_Archives/0_Steps.ipynb)
48
+ - [Indicators](https://github.com/datons/python-esios/blob/main/examples/20_Indicators/0_Steps/B1_Download.ipynb)
49
+ - [Archives](https://github.com/datons/python-esios/blob/main/examples/30_Archives/0_Steps/B1_Download.ipynb)
@@ -3,6 +3,7 @@ import requests
3
3
  import warnings
4
4
  from .indicators import Indicators
5
5
  from .archives import Archives
6
+ from .offer_indicators import OfferIndicators
6
7
 
7
8
  class ESIOSClient:
8
9
  def __init__(self, api_key_esios=None, api_key_premium=None):
@@ -50,5 +51,7 @@ class ESIOSClient:
50
51
  return Indicators(self)
51
52
  elif name == 'archives':
52
53
  return Archives(self)
54
+ elif name == 'offer_indicators':
55
+ return OfferIndicators(self)
53
56
  else:
54
57
  raise ValueError(f"Unknown endpoint: {name}")
@@ -80,7 +80,8 @@ class Indicator:
80
80
  df = df[[col for col in df.columns if 'time' not in col]]
81
81
 
82
82
  if column_name in self.metadata and column_name != 'value':
83
- df.rename(columns={'value': self.metadata[column_name]}, inplace=True)
83
+ column_name = str(self.metadata[column_name])
84
+ df.rename(columns={'value': column_name}, inplace=True)
84
85
 
85
86
  return df
86
87
  else:
@@ -0,0 +1,85 @@
1
+ import pandas as pd
2
+ from datetime import datetime, timedelta
3
+ from bs4 import BeautifulSoup
4
+
5
+ class OfferIndicators:
6
+ def __init__(self, client):
7
+ self.client = client
8
+
9
+ def _html_to_text(self, html):
10
+ soup = BeautifulSoup(html, 'html.parser')
11
+ return '\n\n'.join([p.get_text() for p in soup.find_all('p')])
12
+
13
+ def list(self):
14
+ endpoint = "offer_indicators"
15
+ data = self.client._get(endpoint, self.client.public_headers)
16
+
17
+ indicators = data.get('indicators', [])
18
+ for indicator in indicators:
19
+ indicator['description'] = self._html_to_text(indicator['description'])
20
+
21
+ return pd.DataFrame(indicators)
22
+
23
+ def select(self, id):
24
+ return OfferIndicator(self.client, id)
25
+
26
+ class OfferIndicator:
27
+ def __init__(self, client, id):
28
+ self.client = client
29
+ self.id = id
30
+ self.metadata = self._get_metadata()
31
+
32
+ def _get_metadata(self):
33
+ endpoint = f"offer_indicators/{self.id}"
34
+ data = self.client._get(endpoint, self.client.public_headers).get('indicator', {})
35
+ data.pop('values')
36
+ return data
37
+
38
+ def historical(self, start=None, end=None, locale='es'):
39
+ params = {
40
+ 'start_date': start,
41
+ 'end_date': end,
42
+ 'locale': locale
43
+ }
44
+
45
+ # Remove None values from params
46
+ params = {k: v for k, v in params.items() if v is not None}
47
+
48
+ start_date = datetime.strptime(start, '%Y-%m-%d')
49
+ end_date = datetime.strptime(end, '%Y-%m-%d')
50
+ three_weeks = timedelta(days=3)
51
+
52
+ data_all = []
53
+
54
+ current_start = start_date
55
+ while current_start < end_date:
56
+ current_end = min(current_start + three_weeks, end_date)
57
+ chunk_params = params.copy()
58
+ chunk_params['start_date'] = current_start.strftime('%Y-%m-%d')
59
+ chunk_params['end_date'] = current_end.strftime('%Y-%m-%d')
60
+
61
+ endpoint = f"offer_indicators/{self.id}"
62
+ data = self.client._get(endpoint, self.client.public_headers, params=chunk_params)
63
+ data_all.extend(data.get('indicator', {}).get('values', []))
64
+
65
+ current_start = current_end + timedelta(days=1)
66
+
67
+ return self._to_dataframe(data_all)
68
+
69
+ def _to_dataframe(self, data):
70
+ if data:
71
+ df = pd.DataFrame(data)
72
+ if 'datetime' in df.columns:
73
+ df['datetime'] = pd.to_datetime(df['datetime'], utc=True)
74
+ df = df.set_index('datetime')
75
+ df.index = df.index.tz_convert('Europe/Madrid')
76
+
77
+ df = df[[col for col in df.columns if 'time' not in col]]
78
+
79
+ return df
80
+ else:
81
+ return pd.DataFrame()
82
+
83
+ def forecast(self):
84
+ # Implement forecast functionality similar to historical
85
+ pass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-esios
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: A Python wrapper for the ESIOS API
5
5
  Home-page: https://github.com/datons/python-esios
6
6
  Author: Jesús López
@@ -26,8 +26,26 @@ This library provides a simple interface to download and preprocess the data fro
26
26
  pip install python-esios
27
27
  ```
28
28
 
29
+ ## Get token
30
+
31
+ Ask for a personal token to access the ESIOS API following the [instructions from REE](https://www.esios.ree.es/es/pagina/api).
32
+
29
33
  ## Usage
30
34
 
35
+ ### Register the token in Python
36
+
37
+ ```python
38
+ TOKEN = '343sdfewe342309gjarijgwoiret834383434524...'
39
+ TOKEN = '<YOUR_TOKEN>'
40
+ ```
41
+
42
+ Then, set the token in the environment variable `ESIOS_API_KEY`.
43
+
44
+ ```python
45
+ import os
46
+ os.environ['ESIOS_API_KEY'] = TOKEN
47
+ ```
48
+
31
49
  ### Instantiate the client
32
50
 
33
51
  ```python
@@ -43,5 +61,5 @@ endpoint = client.endpoint(name=?)
43
61
 
44
62
  In the tutorials below, you will learn how to download, preprocess, and visualize the data from the following endpoints:
45
63
 
46
- - [Indicators](https://github.com/datons/python-esios/blob/main/examples/20_Indicators/0_Steps.ipynb)
47
- - [Archives](https://github.com/datons/python-esios/blob/main/examples/30_Archives/0_Steps.ipynb)
64
+ - [Indicators](https://github.com/datons/python-esios/blob/main/examples/20_Indicators/0_Steps/B1_Download.ipynb)
65
+ - [Archives](https://github.com/datons/python-esios/blob/main/examples/30_Archives/0_Steps/B1_Download.ipynb)
@@ -5,6 +5,7 @@ esios/__init__.py
5
5
  esios/archives.py
6
6
  esios/client.py
7
7
  esios/indicators.py
8
+ esios/offer_indicators.py
8
9
  python_esios.egg-info/PKG-INFO
9
10
  python_esios.egg-info/SOURCES.txt
10
11
  python_esios.egg-info/dependency_links.txt
@@ -7,7 +7,7 @@ long_description = (this_directory / "README.md").read_text(encoding='utf-8')
7
7
 
8
8
  setup(
9
9
  name='python-esios',
10
- version='0.2.1',
10
+ version='0.2.2',
11
11
  packages=find_packages(),
12
12
  install_requires=[
13
13
  'requests',
File without changes
File without changes