-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
127 lines (106 loc) · 3.81 KB
/
Copy pathmain.py
File metadata and controls
127 lines (106 loc) · 3.81 KB
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
import datetime
from collections import namedtuple
import requests
from flask import Flask
from pytz import timezone
app = Flask(__name__)
endpoint = "https://ctz.solidwallet.io/api/v3"
data = {
"jsonrpc": "2.0",
"method": "icx_call",
"params": {
"to": "cx0000000000000000000000000000000000000000",
"dataType": "call",
"data": {
"method": "getIISSInfo"
}
},
"id": 1
}
TermData = namedtuple("Counter", "current_height, target_height, height_left,"
"current_datetime, est_datetime,"
"hours_left, minutes_left, seconds_left")
dt_fmt = "%Y-%m-%d %H:%M:%S"
def get_data() -> dict:
return requests.post(endpoint, json=data).json()
def parse_data(response: dict) -> TermData:
current_height = int(response["result"]["blockHeight"], 16)
target_height = int(response["result"]["nextPRepTerm"], 16)
height_left = target_height - current_height
t_diff = datetime.timedelta(seconds=height_left*2)
curr_time = datetime.datetime.now(timezone("Asia/Seoul"))
est_time = curr_time + t_diff
hours, remainders = divmod(t_diff.total_seconds(), 3600)
minutes, seconds = divmod(remainders, 60)
return TermData(
current_height=current_height, target_height=target_height, height_left=height_left,
current_datetime=curr_time.strftime(dt_fmt), est_datetime=est_time.strftime(dt_fmt),
hours_left=int(hours), minutes_left=int(minutes), seconds_left=int(seconds)
)
def make_result(parsed_data: TermData):
content0 = f"Current Height: {parsed_data.current_height}\n" \
f"Target Height: {parsed_data.target_height} \n" \
f"Heights left: {parsed_data.height_left} \n"
content1 = f"... Current Time: {parsed_data.current_datetime} \n" \
f"... Estimated Time: {parsed_data.est_datetime} \n"
footer = f"... {parsed_data.hours_left} hours {parsed_data.minutes_left} minutes {parsed_data.seconds_left} seconds left."
result_str = {
"response_type": "in_channel",
"blocks": [
{
"type": "section",
"text": {
"type": "plain_text",
"text": ":hourglass_flowing_sand: *P-Rep Timer* :iconrocket:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": content0
},
"accessory": {
"type": "image",
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/plants.png",
"alt_text": "plants"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": content1
}
},
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/notificationsWarningIcon.png",
"alt_text": "notifications warning icon"
},
{
"type": "mrkdwn",
"text": f"*{footer}*"
}
]
}
]
}
return result_str
@app.route('/consensus-zzang', methods=["POST"])
def doomsday_counter():
raw_data = get_data()
parsed_data = parse_data(raw_data)
result = make_result(parsed_data)
return result
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)