-
Notifications
You must be signed in to change notification settings - Fork 1
/
pik.py
84 lines (74 loc) · 3.44 KB
/
pik.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
import requests
import json
import os
import time
from helpers import hash_vals, dump_data, load_data, compare
class PIKData(object):
def __init__(self, login, password, data_dir='data'):
self.sess = requests.Session()
self.login = login
self.password = password
self.data_dir = data_dir
self.data_uri = 'https://api.pik.ru/v1/opportunity/log?opportunity_id={}'
self.auth_uri = 'https://api.pik.ru/v1/auth'
self.appointment_uri = 'https://api.pik.ru/v1/office/appointment'
self.token_path = os.path.join(self.data_dir, 'token.json')
self.token = self.get_token(login, password)
# Добавляем куку и заголовок с токеном
self.sess.headers = {
'TOKEN': self.token,
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0',
'Referer': 'https://pik.ru',
'Origin': 'https://pik.ru'
}
cookie_obj = requests.cookies.create_cookie(name='PIK_TOKEN',
value=self.token)
self.sess.cookies.set_cookie(cookie_obj)
# Получаем айдишники квартиры и договора
self.get_flat_data()
# Получаем этапы выполнения договора
self.progress = self.get_progress()
# Получаем данные раздела сопровождения
self.appointment = self.get_appointment()
# Получаем поле статуса этапа "Получение ключей"
self.keys_status = self.progress[5]
def get_token(self, login=None, password=None):
if not login:
login = self.login
if not password:
password = self.password
old_token = load_data(self.token_path)
expiration = old_token.get('expires_in', old_token.get('expiresIn', time.time()))
diff = expiration - time.time()
if diff <= 518400:
print('Обновление токена')
try:
token = self.refresh_token(login, password)
except:
print('Обновление токена не удалось')
token = old_token['token']
else:
token = old_token['token']
return token
def refresh_token(self, login, password):
request = {"login": login, "password": password}
response = self.sess.post(url=self.auth_uri, json=request)
dump_data(response.json(), self.token_path)
token = response.json()['token']
return token
def get_flat_data(self):
page = self.sess.get('https://www.pik.ru/client/property').content.decode()
data_raw = page.split('<script id="__NEXT_DATA__" type="application/json">')[-1].split('</script>')[0]
data = json.loads(data_raw)['props']['initialState']['dealsService']['deals']
for e in data:
if data[e]['isFlat'] == True:
self.oid = data[e]['deal']['id']
self.flat_id = data[e]['guid']
self.flat_data = data[e]
self.flat_data.pop('flats')
def get_progress(self):
response = self.sess.get(url=self.data_uri.format(self.oid)).json()
return response
def get_appointment(self):
response = self.sess.post(url=self.appointment_uri, json={'flat_id': self.flat_id}).json()
return response