-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREData.py
95 lines (65 loc) · 2.57 KB
/
REData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Imports
"""
import pandas as pd
import numpy as np
import requests
import inspect
import operator
import itertools
from functools import reduce
from API_structure import *
"""
Wrapper
"""
class REData:
## Calling the API
def check_errs(self, r_json):
if 'errors' in r_json.keys():
for err in r_json['errors']:
err_title = err['title']
err_detail = err['detail']
raise ValueError(f'{err_title}\n{err_detail}\n')
def make_request(self, start_date, end_date, time_trunc, geo_trunc=None, geo_limit=None, geo_ids=None):
*_, params = inspect.getargvalues(inspect.currentframe())
r = requests.get(self.url, params=params)
return r
## JSON Parsing
col_2_s = staticmethod(lambda col: pd.DataFrame(col['attributes']['values']).set_index('datetime')['value'])
def get_from_dict(self, data_dict, map_list):
return reduce(operator.getitem, map_list, data_dict)
def json_2_nested_lists(self, obj_in, route):
if len(route) == 0:
yield obj_in
elif isinstance(obj_in, list):
for item in obj_in:
obj_out = self.json_2_nested_lists(item, route)
yield from obj_out
else:
obj_out = self.get_from_dict(obj_in, route[0])
yield from self.json_2_nested_lists(obj_out, route[1:])
def json_2_df(self, r_json):
nested_lists = self.json_2_nested_lists(r_json, self.JSON_route)
cols_flatlist = list(itertools.chain(*nested_lists))
df = pd.DataFrame()
for col in cols_flatlist:
s_data = self.col_2_s(col)
s_data.name = col['type']
df[s_data.name] = s_data
df.index = pd.to_datetime(df.index)
return df
## User Functions
def update_stream(self, category, widget):
assert (category, widget) in list(JSON_routes.keys()), f'The widget \'{widget}\' is not allowed for category \'{category}\''
self.JSON_route = JSON_routes[(category, widget)]
self.url = f'https://apidatos.ree.es/en/datos/{category}/{widget}'
def query_REData(self, *args, **kwargs):
r = self.make_request(*args, **kwargs)
r_json = r.json()
self.check_errs(r_json)
df = self.json_2_df(r_json)
return df
## Initialisation
def __init__(self, category, widget):
self.update_stream(category, widget)
self.JSON_route = JSON_routes[(category, widget)]