-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounter_sender-ikus-pesc-ru.py
119 lines (105 loc) · 3.83 KB
/
counter_sender-ikus-pesc-ru.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
# https://ikus.pesc.ru/
# -*- coding: utf-8 -*-
import requests
import sys
import json
class IKUS_API:
def __init__(self, timeout=15.0):
"""
"""
self.timeout = timeout
self.account_page = ''
self.cookie = ''
self.access = ''
self.auth = ''
self.sess = requests.Session()
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Captcha': 'none',
'Content-Type': 'application/json; charset=UTF-8',
'Accept-Language': 'en-GB,en;q=0.9,ru-RU;q=0.8,ru;q=0.7,en-US;q=0.6',
'customer': 'ikus-spb',
'Origin': 'https://ikus.pesc.ru',
'Referer': 'https://ikus.pesc.ru/indications/individual/accounts?id=2434337',
}
self.sendformdata = {}
def login(self, **kwargs):
"""
:param userid:
:return:
"""
r = self.sess.get(f"{data['domain']}",
headers=self.headers,
timeout=self.timeout)
assert r.ok
cookies=r.headers['Set-Cookie'].split(';')[0]
self.headers['Cookie']=cookies
formdata = {
"type": "PHONE",
"login": kwargs.get('username'),
"password": kwargs.get('password')
}
json_data = json.dumps(formdata)
r = self.sess.post(f"{data['domain']}/api/v7/users/auth",
allow_redirects=False,
data=json_data,
headers=self.headers,
timeout=self.timeout)
response_json = json.loads(r.content)
auth_token = response_json.get('auth')
self.access = response_json.get('access')
self.auth = auth_token
self.headers['Authorization']='Bearer ' +auth_token
assert r.ok, 'Ошибка авторизации'
r = self.sess.get(f"{data['domain']}/api/v6/accounts/2434337/meters/info",
headers=self.headers,
timeout=self.timeout)
assert r.ok, 'Ошибка авторизации'
def send_values(self,**kwargs):
body = [
{
"scaleId": 1,
"value": int(kwargs.get('energo_counter_value'))
}
]
#отправляем показания
r = self.sess.post(f"{data['domain']}/api/v6/accounts/2434337/meters/26833261/reading",
data=json.dumps(body),
headers=self.headers,
timeout=self.timeout)
if r.status_code == 200:
print('Показания успешно переданы')
else:
print(r.content)
print(r.status_code)
def logout(self):
body = {
"access": self.access,
"auth": self.auth,
}
r = self.sess.delete(f"{data['domain']}/api/v6/users/auth",
headers=self.headers,
data=json.dumps(body),
timeout=self.timeout)
assert r.ok
if r.status_code == 200:
print('Выход выполнен')
if __name__ == '__main__':
data = {
'username': '',
'password': '',
'energo_counter_value':'',
}
api = IKUS_API()
try:
data['domain'] = 'https://ikus.pesc.ru'
data['username']=str(sys.argv[1])
data['password']=str(sys.argv[2])
data['energo_counter_value']=str(sys.argv[3])
#call function
api.login(**data)
api.send_values(**data)
finally:
api.logout()
print('close connection')