-
Notifications
You must be signed in to change notification settings - Fork 0
/
morton.py
84 lines (72 loc) · 2.8 KB
/
morton.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 config
import requests
from states import *
from datetime import datetime, timedelta
# DEPRECATED
class Morton:
host = 'https://webapi.morton.ru'
sms_path = "/objects/mortonnew"
old_path = '/objects/smssalesmorton'
sold_path = '/objects/mortonstat'
date = {
State.morton_today: 'today',
State.morton_yesterday: 'yesterday',
State.morton_holidays: 'weekend',
State.morton_month: 'month',
}
sales_strings = {
State.morton_today: "Продано в Мортон {} м²",
State.morton_yesterday: "Вчера продано в Мортон {} м²"
}
def forecast_request(self, state):
return 'Нет данных по потецниалу Мортон'
def sms_request(self, state):
data = self.__request(self.sms_path)
if data is None:
return 'Не могу подключиться к Мортон'
for i in data:
if i["tp"] == self.date[state]:
return self.format_sms(i["txt"])
return None
def format_sms(self, txt):
txt = txt.replace("Отчет за", 'Контрактация за')
txt = txt.replace("Отчёт за", 'Контрактация за')
return txt
def old_request(self, state):
data = self.__request(self.old_path)
if data is None:
return 'Не могу подключиться к Мортон'
for i in data:
if i["tp"] == self.date[state]:
return self.format_old(i["txt"])
return None
def format_old(self, txt):
return txt.replace("Отчет за", 'Контрактация за')
def sold_request(self, state):
data = self.__request(self.sold_path)
if data is None:
return 'Не могу подключиться к Мортон'
today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)
if state == State.morton_today:
return self.sales_strings[state].format(self.sum_sold(data, today.day))
elif state == State.morton_yesterday:
return self.sales_strings[state].format(self.sum_sold(data, yesterday.day))
else:
return "Нет информации"
def sum_sold(self, data, day):
result = 0.00
for i in data:
if int(i["day"]) == day:
result += float(i["area"])
return round(result, 2)
def __request(self, path):
payload = {'token': config.MORTON_TOKEN}
# header = {'Accept': "application"}
try:
r = requests.get(self.host + path, params=payload)
except requests.exceptions.HTTPError:
return None
if r.status_code != requests.codes.ok:
return None
return r.json()["data"]