-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstate_console.py
86 lines (67 loc) · 2.08 KB
/
state_console.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
#!/usr/bin/python3
# coding:utf-8
"""
一个 python 命令行示例
依赖: requests
by @wyf9
修改:@rinlit-233-shiroko
"""
import requests
import json
from loguru import logger
import config as cf
# 请求重试次数
RETRY = 3
def get(url):
error_content = ''
for t1 in range(RETRY):
t = t1 + 1
try:
x = requests.get(url, proxies={'http': None, 'https': None})
return x.text
except Exception as e:
logger.warning(f'请求失败!{e}')
error_content += f'第{t}次请求失败:{e};'
logger.info(f'重试中... {t}/{RETRY}')
print(f'Retrying... {t}/{RETRY}')
if t >= RETRY:
logger.error(f'最大重试次数!')
print('Max retry limit!')
continue
return f'CONNECTION FAILED {error_content}'
def loadjson(url):
raw = get(url)
if raw == 'CONNECTION FAILED':
return "connection failed"
try:
return json.loads(raw)
except json.decoder.JSONDecodeError:
print('Error decoding json!\nRaw data:\n"""')
print(raw)
print('"""')
return f"json decode error: {raw}"
except Exception as e:
print('Error:', e)
return str(e)
def main():
print('\n---\nStatus now:')
stnow = loadjson(f'{cf.server}/query')
try:
print(f'success: [{stnow["success"]}], status: [{stnow["status"]}], info_name: [{stnow["info"]["name"]}],'
f' info_desc: [{stnow["info"]["desc"]}], info_color: [{stnow["info"]["color"]}]')
except KeyError:
print(f'RawData: {stnow}')
print('\n---\nSelect status:')
stlst = loadjson(f'{cf.server}/status_list')
for n in stlst:
print(f'{n["id"]} - {n["name"]} - {n["desc"]}')
st = input('\n> ')
ret = loadjson(f'{cf.server}/set/{cf.secret}/{st}')
try:
print(f'success: [{ret["success"]}], code: [{ret["code"]}], set_to: [{ret["set_to"]}]')
except:
print(f'RawData: {ret}')
input('\n---\nPress Enter to exit.')
return 0
if __name__ == "__main__":
main()