-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
210 lines (173 loc) · 8.06 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# -*- coding: utf-8 -*-
import ctypes
import json
import os
import sys
import winreg
import win32api
# Form implementation generated from reading ui file 'wyy.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QCheckBox, QWidget
from networkx.drawing import layout
from sympy import true
import bot
import wyylog
from dingLog import DingLog
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 507)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 801, 361))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
#添加list列表
self.logList = QtWidgets.QListWidget(self.centralwidget)
self.logList.setGeometry(QtCore.QRect(0, 370, 801, 141))
self.logList.setObjectName("logList")
self.verticalLayout.addWidget(self.logList)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(290, 370, 171, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.wyyMethod)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
# 添加设置页面
self.settingsWidget = QtWidgets.QWidget(MainWindow)
self.settingsWidget.setObjectName("settingsWidget")
self.settingsWidget.setVisible(False)
self.settingsLayout = QtWidgets.QVBoxLayout(self.settingsWidget)
self.settingsLayout.setObjectName("settingsLayout")
# 添加输入框
self.setting1_label = QtWidgets.QLabel(self.settingsWidget)
self.setting1_label.setText("Cookie_MUSIC_U:")
self.settingsLayout.addWidget(self.setting1_label)
self.setting1 = QtWidgets.QLineEdit(self.settingsWidget)
self.setting1.setObjectName("setting1")
self.settingsLayout.addWidget(self.setting1)
self.setting2_label = QtWidgets.QLabel(self.settingsWidget)
self.setting2_label.setText("Cookie___csrf")
self.settingsLayout.addWidget(self.setting2_label)
self.setting2 = QtWidgets.QLineEdit(self.settingsWidget)
self.setting2.setObjectName("setting2")
self.settingsLayout.addWidget(self.setting2)
self.setting3_label = QtWidgets.QLabel(self.settingsWidget)
self.setting3_label.setText("BOT_URL:")
self.settingsLayout.addWidget(self.setting3_label)
self.setting3 = QtWidgets.QLineEdit(self.settingsWidget)
self.setting3.setObjectName("setting3")
self.settingsLayout.addWidget(self.setting3)
# 添加保存按钮
self.saveButton = QtWidgets.QPushButton(self.settingsWidget)
self.saveButton.setObjectName("saveButton")
self.saveButton.setText("保存")
self.saveButton.clicked.connect(self.saveSettings)
self.settingsLayout.addWidget(self.saveButton)
# 将设置页面添加到主窗口
self.verticalLayout.addWidget(self.settingsWidget)
self.retranslateUi(MainWindow)
self.settingsButton = QtWidgets.QPushButton(self.centralwidget)
self.settingsButton.setObjectName("settingsButton")
self.settingsButton.setText("必要设置")
self.settingsButton.clicked.connect(self.showSettingsWidget)
# 创建一个 QCheckBox 用于控制开机自启动
self.autostart_checkbox = QCheckBox("开机自启")
self.autostart_checkbox.setChecked(self.is_autostart_enabled())
self.autostart_checkbox.stateChanged.connect(self.toggle_autostart)
self.verticalLayout.addWidget(self.settingsButton)
self.verticalLayout.addWidget(self.autostart_checkbox)
self.loadSettings()
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "wyy合伙人"))
self.pushButton.setText(_translate("MainWindow", "开始签到"))
def is_autostart_enabled(self):
"""检查是否已经设置了开机自启动"""
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run")
value, _ = winreg.QueryValueEx(key, "wyy")
winreg.CloseKey(key)
return os.path.abspath(sys.argv[0]) == value
except (FileNotFoundError, OSError):
return False
def toggle_autostart(self, state):
"""切换开机自启动状态"""
try:
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", 0,
winreg.KEY_WRITE)
if state == Qt.Checked:
self.logList.addItem("打开开机自启")
winreg.SetValueEx(key, "wyy", 0, winreg.REG_SZ, os.path.abspath(sys.executable))
else:
self.logList.addItem("关闭开机自启")
winreg.DeleteValue(key, "wyy")
winreg.CloseKey(key)
except (FileNotFoundError, OSError):
pass
def showSettingsWidget(self):
self.settingsWidget.setVisible(True)
def hideSettingsWidget(self):
self.settingsWidget.setVisible(False)
def wyyMethod(self):
try:
myContext = bot.Context()
dingLog = DingLog(myContext.getUserData("BOT_URL"))
log = wyylog.LogManager("wyy1.log", "wyy2.log", self.logList)
if bot.Bot(myContext, dingLog, log).run():
dingLog.end("✅ 执行成功")
log.write_to_log_file("✅ 执行成功", "info")
else:
dingLog.end("❌ 执行失败", True)
log.write_to_log_file("❌ 执行失败", "error")
except Exception as e:
self.logList.addItem(str(e))
self.logList.scrollToBottom()
def saveSettings(self):
setting1 = self.setting1.text().strip()
setting2 = self.setting2.text().strip()
setting3 = self.setting3.text().strip()
settings = {
"Cookie_MUSIC_U": setting1,
"Cookie___csrf": setting2,
"BOT_URL": setting3
}
with open("setting.json", "w") as f:
json.dump(settings, f)
QMessageBox.information(self.settingsWidget, "保存成功", "设置已成功保存至 setting.json 文件。")
self.hideSettingsWidget()
def loadSettings(self):
try:
with open("setting.json", "r") as f:
settings = json.load(f)
self.setting1.setText(settings["Cookie_MUSIC_U"])
self.setting2.setText(settings["Cookie___csrf"])
self.setting3.setText(settings["BOT_URL"])
except (FileNotFoundError, KeyError):
pass
if __name__ == "__main__":
# 固定的,PyQt5程序都需要QApplication对象。sys.argv是命令行参数列表,确保程序可以双击运行
app = QApplication(sys.argv)
# 初始化
mainWindow = QMainWindow()
myWin = Ui_MainWindow()
# 将窗口控件显示在屏幕上
myWin.setupUi(mainWindow)
mainWindow.show()
myWin.wyyMethod()
# 程序运行,sys.exit方法确保程序完整退出。
sys.exit(app.exec_())