-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathhomework_device.py
More file actions
239 lines (199 loc) · 5.34 KB
/
homework_device.py
File metadata and controls
239 lines (199 loc) · 5.34 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# coding: utf-8
import requests
from time import sleep
# --- config start
SERVER = 'https://sleepy.example.com' # 部署地址,末尾不带 `/`
SECRET = '11111111-4444-5555-1111-444444444444'
PROXY: str = '' # 代理地址 (<http/socks5>://host:port), 设置为空字符串禁用
# --- config end
# --- modifies
_print_ = print
def print(*args, **kwargs):
'''
modified `print()` function
replaced secret to `[SECRET]`
original: `_print_()`
'''
new_args = []
for i in args:
new_args.append(str(i).replace(str(SECRET), '[SECRET]'))
_print_(*new_args, **kwargs)
def get(url: str, **kwargs):
'''
modified `requests.get()`
'''
retries = 5
while True:
try:
if PROXY:
return requests.get(
url=url,
proxies={
'all': PROXY
},
**kwargs
)
else:
return requests.get(
url=url,
**kwargs
)
except Exception as e:
retries -= 1
if retries:
print(f'ing - Request error: {e}, retrying... ({retries} left)')
sleep(0.5)
continue
else:
raise
def post(url: str, Json: dict, **kwargs):
'''
modified `requests.post()`
'''
retries = 5
while True:
try:
if PROXY:
return requests.post(
url=url,
json=Json,
proxies={
'all': PROXY
},
headers={
'Content-Type': 'application/json'
},
**kwargs
)
else:
return requests.post(
url=url,
json=Json,
headers={
'Content-Type': 'application/json'
},
**kwargs
)
except Exception as e:
retries -= 1
if retries:
print(f'ing - Request error: {e}, retrying... ({retries} left)')
sleep(0.5)
continue
else:
raise
# --- functions
# - public
def query():
'''
/query using GET
- check status now
'''
resp = get(f'{SERVER}/query')
print(f'[/query] Response: {resp.status_code} - {resp.text}')
def status_list():
'''
/status_list using GET
- see status list
'''
resp = get(f'{SERVER}/status_list')
print(f'[/status_list] Response: {resp.status_code} - {resp.text}')
def metrics():
'''
/metrics using GET
- see metrics data
'''
resp = get(f'{SERVER}/metrics')
print(f'[/metrics] Response: {resp.status_code} - {resp.text}')
# - status
def status(stat: int):
'''
/set using GET
- set status manually
'''
resp = get(f'{SERVER}/set?secret={SECRET}&status={stat}')
print(f'[/set] Response: {resp.status_code} - {resp.json()}')
# - device
def device_set(id: str, show_name: str, status: str, using: bool = True):
'''
/api/device/set using POST
- set device status
'''
resp = post(f'{SERVER}/api/device/set', {
'secret': SECRET,
'id': id,
'show_name': show_name,
'using': using,
'status': status
})
print(f'[/api/device/set] Response: {resp.status_code} - {resp.json()}')
def device_remove(id: str):
'''
/device/remove using GET
- remove a device with it's status
'''
resp = get(f'{SERVER}/device/remove?secret={SECRET}&id={id}')
print(f'[/device/remove] Response: {resp.status_code} - {resp.json()}')
def device_clear():
'''
/device/clear using GET
- remove **all** devices with their statuses
'''
resp = get(f'{SERVER}/device/clear?secret={SECRET}')
print(f'[/device/clear] Response: {resp.status_code} - {resp.json()}')
def private_mode(private: bool):
'''
/device/private using GET
- open / close private mode *(don't show device status)*
'''
resp = get(f'{SERVER}/device/private?secret={SECRET}&private={private}')
print(f'[/device/private] Response: {resp.status_code} - {resp.json()}')
# - custom
def left(
num: int = 0,
id: str = 'homework-left',
show_name: str = 'Homework left'
):
'''
set how much homework left
'''
if num:
device_set(
id=id,
show_name=show_name,
status=f'{num}'
)
else:
device_remove(id=id)
def writing(
name: str = '',
id: str = 'homework-writing',
show_name: str = 'Homework writing'
):
'''
set what homework you're writing
'''
if name:
device_set(
id=id,
show_name=show_name,
status=name
)
else:
device_remove(id=id)
# --- main loop
if __name__ == '__main__':
try:
while True:
i = input('in < ')
try:
o = eval(i)
if not o is None:
print(f'out > {o}')
except Exception as e:
print(f'err - {e}')
except KeyboardInterrupt:
print('Exiting: ^C')
except Exception as e:
print(f'Exiting: {e}')
exit()