-
Notifications
You must be signed in to change notification settings - Fork 0
/
esm_watchlist_export.py
205 lines (166 loc) · 6.38 KB
/
esm_watchlist_export.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- coding: utf-8 -*-
import base64
import json
import re
import sys
from config import ESMConfig
from httpclient import HTTPClient
from exceptions import ESMError
class ESM(object):
def __init__(self, esmcfg, api_version='2'):
"""Initialize the ESM class.
Args:
esmcfg: Instance of ESMConfig object OR Dict with these keys:
{'esmhost': '<ESM-IP>',
'esmuser': <ESM-USERNAME>,
'esmpass': <ESM-PASSWORD>}
api_version (str): '1' or '2'. Defaults to '2'.
Returns:
Instance of ESM
Raises:
ValueError: esmcfg required.
ValueError: esmhost, esmuser, esmpass required in esmcfg.
ValueError: api_version must be '1' or '2'.
"""
if api_version is not '2':# or not '2':
raise ValueError('api_version is "1" or "2".')
try:
esmuser = esmcfg['esmuser']
except KeyError:
raise ValueError('Invalid esmcreds: Missing esmuser.')
try:
esmpass = esmcfg['esmpass']
except KeyError:
raise ValueError('Invalid esmcfg: Missing esmpass.')
try:
esmhost = esmcfg['esmhost']
except KeyError:
raise ValueError('Invalid esmcfg: Missing esmhost.')
self.session = HTTPClient()
self.base_url = self._set_base_url(esmhost, api_version)
self.int_url = 'https://{}/ess'.format(esmhost)
self.login_url = 'https://{}/rs/esm/login'.format(esmhost)
self._setup_auth(esmuser, esmpass)
self.logged_in = False
def _set_base_url(self, host, api_version):
"""ESM public URL setter"""
if str(api_version) == '2':
return 'https://{}/rs/esm/v2/'.format(host)
elif str(api_version) == '1':
return 'https://{}/rs/esm/'.format(host)
else:
raise ValueError('API Version must be 1 or 2.')
def _setup_auth(self, user, passwd):
"""Interface to auth setup functions"""
self._auth_data = {}
self._auth_data['username'] = base64.b64encode(
user.encode('utf-8')).decode()
self._auth_data['password'] = base64.b64encode(
passwd.encode('utf-8')).decode()
self._auth_data['locale'] = 'en_US'
self._auth_data['os'] = 'Win32'
def login(self):
method = 'login'
cb = self._set_header
self.post(method, data=self._auth_data, callback=cb, raw=True)
def logout(self):
"""
"""
method = 'logout'
url = self._set_url(method)
self.session.delete(url)
def _set_header(self, resp):
"""Adds field to http header.
Args:
Requests response object.
"""
self.session.add_header({'X-Xsrf-Token':
resp.headers.get('Xsrf-Token')})
self.logged_in = True
def _set_url(self, method):
if method.isupper():
url = self._int_url
data = self._format_priv_params(method, **data)
elif method is 'login':
url = self.login_url
else:
url = ''.join([self.base_url, method])
return url
def post(self, method, data=None, callback=None, raw=False):
url = self._set_url(method)
resp = self.session.post(url, data=data)
if raw:
data = resp
else:
data = self._unpack_resp(resp)
if callback:
return callback(data)
return data
def _unpack_resp(self, response):
"""Unpack data from response.
Args:
response: requests response object
"""
data = None
try:
if isinstance(response.json(), list):
data = response.json()
elif isinstance(response.json(), dict):
try:
data = response.json()['value']
except KeyError:
try:
response.json()['return']
except KeyError:
data = response.json()
return data
except json.decoder.JSONDecodeError:
return
def watchlist_fields(self):
method = 'sysGetWatchlistFields'
return self.post(method)
def watchlist_summary(self):
method = 'sysGetWatchlists?hidden=false&dynamic=false&writeOnly=false&indexedOnly=false'
return self.post(method)
def get_watchlist_details(self, w_id):
method = 'sysGetWatchlistDetails'
data = {'id': w_id}
return self.post(method, data=data)
def export_watchlist(self, w_id):
wl_data = []
file_size = 0
bytes_read = 0
wl_details = self.get_watchlist_details(w_id)
filename = wl_details.get('valueFile').get('fileToken')
file_data = self._get_watchlist_vals(filename, bytes_read)
wl_data.append(file_data.get('data'))
bytes_read += file_data.get('bytesRead')
while bytes_read != file_data.get('fileSize'):
file_data = self._get_watchlist_vals(filename, bytes_read)
bytes_read += file_data.get('bytesRead')
wl_data.append(file_data.get('data'))
return wl_data
def _get_watchlist_vals(self, w_id, byte_pos):
method = 'sysGetWatchlistValues?pos={}&count=0'.format(byte_pos)
data = {"file": {"id": w_id}}
return self.post(method, data=data)
def format_filename(text):
return text.replace(' ', '_').lower() + '_watchlist.txt'
def main():
config = ESMConfig()
esm = ESM(config)
esm.login()
watchlists = esm.watchlist_summary()
for wl in watchlists:
wl_data = esm.export_watchlist(wl['id'])
wl_data = ''.join(wl_data)
filename = format_filename(wl['name'])
with open(filename, 'w') as f:
f.write(wl_data)
esm.logout()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logging.warning("Control-C Pressed, stopping...")
sys.exit()