-
Notifications
You must be signed in to change notification settings - Fork 0
/
oec.py
66 lines (51 loc) · 1.76 KB
/
oec.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
import os
import requests
import pandas as pd
from typing import Optional
class OEC:
def __init__(self) -> None:
pass
def get_members(self, payload:dict):
"""
payload must be a dict like:
payload = {
'cube': 'trade_i_baci_a_92',
'level': 'Year'
}
"""
base_url = 'https://oec.world/olap-proxy/members?'
r = requests.get(base_url, params = payload)
df = pd.DataFrame(r.json()['data'])
df.sort_values('ID').reset_index(drop=True)
df.columns = df.columns.map(lambda x: x.replace(' ', '_').lower())
return df
def get_data(self, auth:bool, cube:str, drilldown:list, measure:list,token:Optional[str], cut=None):
"""
usage example:
cut = {
'Year': '2020',
'Trade Flow': '2'
}
drilldown = ['Year', 'Subnat Geography', 'Country', 'Product']
measure = ['Trade Value']
cube='trade_i_baci_a_92'
token = 'my_token'
oec.get_data(au)
"""
base_url = 'https://oec.world/olap-proxy/data.jsonrecords?'
if cut == None:
payload = {}
else:
payload = cut.copy()
drilldown = ', '.join(drilldown)
measure = ', '.join(measure)
payload['cube'] = cube
payload['drilldowns'] = drilldown
payload['measures'] = measure
base_url = 'https://oec.world/olap-proxy/data.jsonrecords?'
if auth:
payload['token'] = token if token else os.environ['OEC_TOKEN']
r = requests.get(base_url, params = payload)
df = pd.DataFrame(r.json()['data'])
df.columns = df.columns.map(lambda x: x.replace(' ', '_').lower())
return df