forked from Merack/fosuNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
186 lines (163 loc) · 5.85 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
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
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# @Author: Merack
# @Email: [email protected]
import os
import sys
import schoolNet
import configparser
import config as cfg
class Menu():
def __init__(self):
self.config = configparser.ConfigParser()
if not os.path.exists(cfg.config_path + cfg.config_name):
self.config["account"] = {
"studentID": '',
"password": '',
"service": ''
}
with open(cfg.config_path + cfg.config_name, 'w', encoding='utf-8') as f:
self.config.write(f)
if cfg.debug:
self.config.read(cfg.config_path + cfg.dev_config_name, encoding='utf-8')
else:
self.config.read(cfg.config_path + cfg.config_name, encoding='utf-8')
self.studentID = self.config['account']['studentID']
self.password = self.config['account']['password']
self.service = self.config['account']['service']
self.cookie = None
self.serviceList = ['电信', '移动', '联通']
self.choices = {
"1": self.offline,
"2": self.login,
"3": self.showDevices,
"4": self.offlineLocal,
"5": self.offlineALL,
"6": self.changeInfo,
"q": self.quit
}
def displayMenu(self):
print("""
Operation Menu:
1. 下线设备
2. 登陆校园网
3. 列出在线设备
4. 下线本设备
5. 下线所有设备
6. 修改配置文件
q. 退出程序
""")
def run(self):
self.check()
self.cookie = schoolNet.serviceLogin(self.studentID, self.password)
# self.test()
while True:
os.system('cls')
self.displayMenu()
try:
choice = input("Enter an option: ")
except Exception as e:
print("Please input a valid option!")
continue
choice = str(choice).strip()
action = self.choices.get(choice)
if action:
action()
else:
print("{0} is not a valid choice".format(choice))
def test(self):
print(self.studentID, self.password, self.service, self.cookie)
input()
def check(self):
"""
检查配置文件中是否有值
如果配置为空, 则调用信息修改函数
:return:
"""
if not self.studentID or not self.password or not self.service:
print("未在配置文件 " + os.path.abspath(cfg.config_path + cfg.config_name) + " 中检测到账号/密码/运营商,")
self.changeInfo()
def check_login(self):
re = schoolNet.serviceLogin(self.studentID, self.password)
if re == -1:
print("登陆失败, 您输入的信息不正确, 请在菜单中选择'修改配置文件'重新输入")
print("或者前往软件目录下config文件夹中修改config.ini")
return False
else:
return True
def quit(self):
print("\nThank you for using this script!\n")
sys.exit(0)
def offline(self):
# print(self.check_login())
if self.check_login():
re = schoolNet.offline(self.studentID, self.cookie)
if re != -1:
print(re)
os.system('pause')
else:
os.system('pause')
def offlineALL(self):
if self.check_login():
choice = input("您确定要下线所有设备吗(y/n)")
if choice == 'yes' or choice == 'y':
re = schoolNet.offline(self.studentID, self.cookie, True)
if re != -1:
print(re)
os.system('pause')
def offlineLocal(self):
if self.check_login():
re = schoolNet.offline(self.studentID, self.cookie, False, True)
if re != -1:
print(re)
os.system('pause')
def login(self):
re = schoolNet.login(self.studentID, self.password, self.service)
print(re[1])
if re[0] == -1:
print("\ntips: 如果你想修改账号信息, 可以在菜单中选择'修改配置文件'重新输入账号信息")
print("或者前往软件目录下config文件夹中修改config.ini来进行修改")
os.system('pause')
def showDevices(self):
if self.check_login():
schoolNet.showDevices(None, self.cookie)
os.system('pause')
def getService(self):
"""
让用户选择是哪个运营商
:return:
"""
try:
index = int(input("请选择运营商id: "))
service = self.serviceList[index]
except Exception as e:
print("输入非法, 请输入正确的数值")
return self.getService()
else:
return service
def changeInfo(self):
print("请手动输入信息~")
self.studentID = input("学号: ")
self.password = input("密码: ")
print("""
运营商列表:
0. 电信
1. 移动
2. 联通
""")
self.service = self.getService()
self.cookie = schoolNet.serviceLogin(self.studentID, self.password)
self.config['account']['studentID'] = self.studentID
self.config['account']['password'] = self.password
self.config['account']['service'] = self.service
if cfg.debug:
with open(cfg.config_path + cfg.dev_config_name, 'w', encoding='utf-8') as f:
self.config.write(f)
else:
with open(cfg.config_path + cfg.config_name, 'w', encoding='utf-8') as f:
self.config.write(f)
print("配置文件写入成功, 路径:" + os.path.abspath(cfg.config_path + cfg.config_name))
print("按任意键进入菜单")
input()
if __name__ == '__main__':
Menu().run()