-
Notifications
You must be signed in to change notification settings - Fork 10
/
query_apiv2.py
125 lines (94 loc) · 3.88 KB
/
query_apiv2.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
"""
Only tested in Python 3.
You may need to install the 'requests' Python3 module.
Be sure to fill in your username, password, org name and email before running
"""
from os import path
import requests
from requests.auth import HTTPBasicAuth
# account details
USERNAME = 'YOUR USERNAME HERE'
PASSWORD = 'YOUR PASSWORD HERE'
EMAIL = '[email protected]'
ORG = 'some org name'
# request details
BA = 'CAISO_NORTH' # identify grid region
# starttime and endtime are optional, if ommited will return the latest value
START = '2020-03-01T00:00:00-0000' # UTC offset of 0 (PDT is -7, PST -8)
END = '2020-03-01T00:45:00-0000'
def register(username, password, email, org):
url = 'https://api2.watttime.org/register'
params = {'username': username,
'password': password,
'email': email,
'org': org}
rsp = requests.post(url, json=params)
print(rsp.text)
def login(username, password):
url = 'https://api2.watttime.org/login'
try:
rsp = requests.get(url, auth=HTTPBasicAuth(username, password))
except BaseException as e:
print('There was an error making your login request: {}'.format(e))
return None
try:
token = rsp.json()['token']
except BaseException:
print('There was an error logging in. The message returned from the '
'api is {}'.format(rsp.text))
return None
return token
def data(token, ba, starttime, endtime):
url = 'https://api2.watttime.org/data'
headers = {'Authorization': 'Bearer {}'.format(token)}
params = {'ba': ba, 'starttime': starttime, 'endtime': endtime}
rsp = requests.get(url, headers=headers, params=params)
# print(rsp.text) # uncomment to see raw response
return rsp.json()
def index(token, ba):
url = 'https://api2.watttime.org/index'
headers = {'Authorization': 'Bearer {}'.format(token)}
params = {'ba': ba}
rsp = requests.get(url, headers=headers, params=params)
# print(rsp.text) # uncomment to see raw response
return rsp.json()
def forecast(token, ba, starttime=None, endtime=None):
url = 'https://api2.watttime.org/forecast'
headers = {'Authorization': 'Bearer {}'.format(token)}
params = {'ba': ba}
if starttime:
params.update({'starttime': starttime, 'endtime': endtime})
rsp = requests.get(url, headers=headers, params=params)
# print(rsp.text) # uncomment to see raw response
return rsp.json()
def historical(token, ba):
url = 'https://api2.watttime.org/historical'
headers = {'Authorization': 'Bearer {}'.format(token)}
params = {'ba': ba}
rsp = requests.get(url, headers=headers, params=params)
cur_dir = path.dirname(path.realpath(__file__))
file_path = path.join(cur_dir, '{}_historical.zip'.format(ba))
with open(file_path, 'wb') as fp:
fp.write(rsp.content)
print('Wrote historical data for {} to {}'.format(ba, file_path))
# Only register once!!
# register(USERNAME, PASSWORD, EMAIL, ORG)
token = login(USERNAME, PASSWORD)
if not token:
print('You will need to fix your login credentials (username and password '
'at the start of this file) before you can query other endpoints. '
'Make sure that you have registered at least once by uncommenting '
'the register(username, password, email, org) line near the bottom '
'of this file.')
exit()
realtime_index = index(token, BA)
print(realtime_index)
print('Please note: the following endpoints require a WattTime subscription')
historical_moer = data(token, BA, START, END)
print(historical_moer)
forecast_moer = forecast(token, BA)
print(forecast_moer)
forecast_moer = forecast(token, BA, START, END)
print(forecast_moer)
historical(token, BA) # Writes zip file to current ditectory