-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (120 loc) · 3.51 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 5 12:25:26 2022
@author: Nathan Woodburn
"""
#change this KEY
apikey = 'API KEY'
#Network port is 12039 for mainnet and 14039
networkport = '12039'
# Delay to check for new names in Bob (in minutes)
checknames = 10
# Delay to check for expired names using webhook block (in minutes)
checkexpiration = 1
# Blocks before exipation to alert user
expblock = 2000
# Linux command to run on name expiry alert. This is where you can use
# any email, sms or other method to alert the user
script = "./alert.sh"
# Import needed stuff
import requests
import time
import os
import sys
# Get current OS
import platform
osname = platform.system()
# Get cli args
# Runs program once if cli passed
cli = False
# You can pass the api key and settings through cli args
i = 0
for arg in sys.argv:
if (i == 1):
cli = True
apikey = arg
if (i == 2):
networkport = arg
if (i == 3):
expblock = int(arg)
i+=1
def hsd_running():
'''
Check whether the process is running
Returns
-------
bool
Whether process exists
'''
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
result = sock.connect_ex(('127.0.0.1',int(networkport)))
sock.close()
if result == 0:
return True
else:
return False
def getnextexp():
if not hsd_running():
print("HSD not running")
return
global nextexp
global nextexpblock
r = requests.get('http://x:'+apikey+'@127.0.0.1:'+networkport+'/wallet')
wallets = r.json()
names = []
for wallet in wallets:
r = requests.get('http://x:'+apikey+'@127.0.0.1:'+networkport+'/wallet/'+wallet+'/name?own=true')
names.extend(r.json())
for name in names:
if ("renewalPeriodEnd" in name["stats"]):
if(name["stats"]["renewalPeriodEnd"] < nextexpblock or nextexpblock == 0):
nextexpblock = name["stats"]["renewalPeriodEnd"]
nextexp = name["name"]
def checkexp():
r = requests.get('https://api.handshakeapi.com/hsd')
api = r.json()
block = api["chain"]["height"]
global nextexpblock
global nextexp
print(nextexp,"will expire in",nextexpblock-block,"blocks")
print("Current block",block)
if block > nextexpblock - expblock and not cli:
global osname
if (osname == "Windows"):
# from win10toast import ToastNotifier
time = (nextexpblock-block)/144
from plyer import notification
notification.notify(
title = "HNS Alarm",
message = nextexp+" will expire in "+str(nextexpblock-block)+" blocks (~"+str(round(time))+" days)!\nPlease renew it in Bob/HSD",
timeout = 10
)
return
if (osname == "Linux"):
os.system(script)
return
print("Sorry we don\'t currently support",osname)
nextexp = ''
nextexpblock = 0
getnextexp()
print("Next name to expire",nextexp)
print("Expiry block",nextexpblock)
if (nextexpblock != 0):
checkexp()
timenew = -1
timeexp = -1
# Time between loops
timeloop = 1
while not cli:
timenew += timeloop
timeexp += timeloop
if timenew >= checknames*60:
getnextexp()
print(nextexp)
print(nextexpblock)
if timeexp >= checkexpiration*60:
if (nextexpblock != 0):
checkexp()
time.sleep(timeloop)