-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremeha.py
121 lines (92 loc) · 4.53 KB
/
remeha.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
import requests
import csv
import re
from io import StringIO
import datetime
from typing import List, Dict
class Remeha:
_urls = {
'login_form': 'https://monitoring.remeha.nl/status/accounts/login/?next=/status/dashboard/',
'login': 'https://monitoring.remeha.nl/status/accounts/login/?next=/status/dashboard/',
'getdata': 'https://monitoring.remeha.nl/status/srv/handlers/modpython.py'
}
_headers = {
'authority': 'monitoring.remeha.nl',
'origin': 'https://monitoring.remeha.nl',
'Content-type': 'application/x-www-form-urlencoded',
'User-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
def __init__(self, username: str, password: str, ssl_verify: bool = True):
self._username = username
self._password = password
self._ssl_verify = ssl_verify
self._session = requests.Session()
def login(self):
self._get_csrftoken()
self._do_login()
def _get_csrftoken(self):
print(requests.certs.where())
response = self._session.get(self._urls['login_form'], verify=self._ssl_verify)
if not response:
raise(f'failure! {response}')
if 'csrftoken' in response.cookies:
self._csrftoken = response.cookies['csrftoken']
print(f'csrftoken: {self._csrftoken}')
self._cookies = response.cookies
def _do_login(self):
data = {
'csrfmiddlewaretoken': self._csrftoken,
'username': self._username,
'password': self._password,
'next': '/status/dashboard/'}
headers = self._headers
headers['referer'] = 'https://monitoring.remeha.nl/status/accounts/login/?next=/status/dashboard/'
response = self._session.post(url=self._urls['login'],
data=data,
headers=headers,
cookies=self._cookies,
verify=self._ssl_verify)
print('logging in')
if response.status_code != 200:
print(response)
print(response.text)
exit(1)
def get_data(self, mac_address: str, date_from: datetime = None, date_to: datetime = None) -> List[Dict[str,str]]:
if not date_to:
date_to = datetime.datetime.now()
if not date_from:
date_from = date_to - datetime.timedelta(days=1)
db = f'KIT-{mac_address}'
label = 'data'
headers = self._headers
headers['referer'] = 'https://monitoring.remeha.nl/html/graph.html?plot=points'
headers['x-requested-with'] = 'XMLHttpRequest'
headers['accept'] = '*/*'
data = {
'command': f'<cmd><name>download_points</name><params><table>v2</table><table>v3</table><table>v4</table><table>v5</table><table>v18</table><table>v188</table><table>v192</table><table>v235</table><table>v1</table><table>v252</table><table>v250</table><table>v14</table><table>v32</table><table>v6</table><table>v187</table><table>v241</table><table>v11</table><table>v12</table><table>v186</table><table>v185</table><table>v13</table><table>v184</table><table>v8</table><table>v9</table><table>v10</table><db>{db}</db><label>{label}</label><from>{str(int(date_from.timestamp()))}</from><to>{str(int(date_to.timestamp()))}</to></params></cmd>'}
response = self._session.post(url=self._urls['getdata'],
headers=headers,
data=data,
verify=self._ssl_verify)
if response.status_code == 200:
fields = None
original_fields = {}
s = StringIO(response.text)
reader = csv.reader(s, delimiter='\t')
if not reader:
print('Failed reading input')
result: List[Dict[str, any]] = []
for row in reader:
if not fields:
fields = []
regex = re.compile('[^a-zA-Z_0-9]')
for original_field in row:
field = original_field.split(',')[0]
field = re.sub('\[.*?\]', '', field)
field = field.lower().rstrip().replace(' ', '_')
field = regex.sub('', field)
fields.append(field)
original_fields[field] = original_field
else:
result.append({fields[index]: v for index, v in enumerate(row)})
return result, original_fields