From 4b45c6f9a200f3ee5727784906e571c1a1365f7a Mon Sep 17 00:00:00 2001 From: tonquer Date: Thu, 25 Feb 2021 17:42:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/qt/qtmain.py | 1 - src/qt/user/qtlogin.py | 81 +++++++++++++++++++- src/server/req.py | 9 +++ src/server/server.py | 28 +++++-- src/server/user_handler.py | 37 +++++++++- ui/login.py | 107 +++++++++++++++++++-------- ui/login.ui | 147 +++++++++++++++++++++++++++++-------- ui/test.py | 15 +++- ui/test.ui | 36 ++++++++- 9 files changed, 382 insertions(+), 79 deletions(-) diff --git a/src/qt/qtmain.py b/src/qt/qtmain.py index 92ffc61..f4539e1 100644 --- a/src/qt/qtmain.py +++ b/src/qt/qtmain.py @@ -120,7 +120,6 @@ def closeEvent(self, a0: QtGui.QCloseEvent) -> None: self.settingForm.ExitSaveSetting(self.size(), self.bookInfoForm.size(), userId, passwd) def Init(self): - # self.ClearExpiredCache() waifu2x.Set(config.Waifu2xThread, config.Encode, getattr(config, "Model"+str(config.Model))) stat = waifu2x.Init() if stat < 0: diff --git a/src/qt/user/qtlogin.py b/src/qt/user/qtlogin.py index ba6e6a6..87994ad 100644 --- a/src/qt/user/qtlogin.py +++ b/src/qt/user/qtlogin.py @@ -1,12 +1,14 @@ import weakref +from PySide2.QtCore import Qt from PySide2.QtGui import QPixmap from conf import config from src.qt.util.qttask import QtTask +from src.server import Server, req from src.user.user import User from src.util.status import Status -from ui.login import Ui_Login +from ui.login import Ui_Login, QLabel from PySide2 import QtWidgets @@ -21,6 +23,9 @@ def __init__(self, owner): self.buttonGroup.addButton(self.selectIp1) self.selectIp1.setChecked(True) + self.speedTest = [] + self.speedIndex = 0 + def Login(self): self.SetSelectIp() userId = self.userIdEdit.text() @@ -75,8 +80,9 @@ def InitBack(self, msg): for index, ip in enumerate(User().addresss, 2): selectIp = QtWidgets.QRadioButton(self) selectIp.setObjectName("selectIp"+str(index)) - self.horizontalLayout_4.addWidget(selectIp) self.buttonGroup.addButton(selectIp) + count = self.gridLayout_4.rowCount() + self.gridLayout_4.addWidget(selectIp, count, 0, 1, 1) selectIp.setText("分流" + str(index)) self.update() else: @@ -90,3 +96,74 @@ def SetSelectIp(self): User().address = "" else: User().address = User().addresss[index-1] + + def SpeedTest(self): + self.testSpeedButton.setEnabled(False) + self.loginButton.setEnabled(False) + self.registerButton.setEnabled(False) + self.speedIndex = 0 + self.speedTest = [] + self.speedTest = [("", False), ("", True)] + for address in User().addresss: + self.speedTest.append((address, False)) + self.speedTest.append((address, True)) + for i in range(len(self.speedTest)): + row = i // 2 + 1 + col = i % 2 + 1 + item = self.gridLayout_4.itemAtPosition(row, col) + if item: + item.widget().setText("") + + self.StartSpeedTest() + + def StartSpeedTest(self): + if len(self.speedTest) <= self.speedIndex: + self.testSpeedButton.setEnabled(True) + self.loginButton.setEnabled(True) + self.registerButton.setEnabled(True) + return + + address, httpProxy = self.speedTest[self.speedIndex] + if httpProxy and not config.HttpProxy: + row = self.speedIndex // 2 + 1 + col = self.speedIndex % 2 + 1 + item = self.gridLayout_4.itemAtPosition(row, col) + if item: + item.widget().setText("无代理") + else: + label = QLabel("无代理") + label.setAlignment(Qt.AlignCenter) + self.gridLayout_4.addWidget(label, row, col, 1, 1) + self.speedIndex += 1 + self.StartSpeedTest() + return + + request = req.SpeedTestReq() + # token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MDEyMjg3YzYxYWFlODJmZDJjMGQzNTUiLCJlbWFpbCI6InRvbnF1ZXIyIiwicm9sZSI6Im1lbWJlciIsIm5hbWUiOiJ0b25xdWVyMiIsInZlcnNpb24iOiIyLjIuMS4zLjMuNCIsImJ1aWxkVmVyc2lvbiI6IjQ1IiwicGxhdGZvcm0iOiJhbmRyb2lkIiwiaWF0IjoxNjE0MjQxODY1LCJleHAiOjE2MTQ4NDY2NjV9.ZUmRP319zREBHk3ax_dJh-qeUDFLmOg_RQBPAMWN8II" + testIp = address + index = self.speedIndex + if httpProxy: + request.proxy = {"http": config.HttpProxy, "https": config.HttpProxy} + else: + request.proxy = "" + QtTask().AddHttpTask(lambda x: Server().TestSpeed(request, x, testIp), self.SpeedTestBack, index) + return + + def SpeedTestBack(self, data, backParam): + if not data: + data = "超时" + else: + data = data + + row = self.speedIndex // 2 + 1 + col = self.speedIndex % 2 + 1 + item = self.gridLayout_4.itemAtPosition(row, col) + if item: + item.widget().setText(data) + else: + label = QLabel(data) + label.setAlignment(Qt.AlignCenter) + self.gridLayout_4.addWidget(label, row, col, 1, 1) + self.speedIndex += 1 + self.StartSpeedTest() + return diff --git a/src/server/req.py b/src/server/req.py index 7ccec30..2bba7f9 100644 --- a/src/server/req.py +++ b/src/server/req.py @@ -206,3 +206,12 @@ def __init__(self, bookId="", content=""): method = "POST" super(self.__class__, self).__init__(url, ToolUtil.GetHeader(url, method), {"content": content}, method) + + +# 下载图片 +class SpeedTestReq(ServerReq): + def __init__(self): + url = "https://storage1.picacomic.com/static/7e7d1320-9717-4702-883d-2899975283b2.jpg" + method = "Download" + super(self.__class__, self).__init__(url, ToolUtil.GetHeader(url, method), + {}, method) diff --git a/src/server/server.py b/src/server/server.py index 4d30ce4..b005594 100644 --- a/src/server/server.py +++ b/src/server/server.py @@ -160,14 +160,15 @@ def Download(self, request, token="", bakParams="", cacheAndLoadPath="", loadPat def _Download(self, task): try: - for cachePath in [task.cacheAndLoadPath, task.loadPath]: - if cachePath and task.bakParam: - data = ToolUtil.LoadCachePicture(cachePath) - if data: - from src.qt.util.qttask import QtTask - QtTask().downloadBack.emit(task.bakParam, len(data), data) - QtTask().downloadBack.emit(task.bakParam, 0, b"") - return + if not isinstance(task.req, req.SpeedTestReq): + for cachePath in [task.cacheAndLoadPath, task.loadPath]: + if cachePath and task.bakParam: + data = ToolUtil.LoadCachePicture(cachePath) + if data: + from src.qt.util.qttask import QtTask + QtTask().downloadBack.emit(task.bakParam, len(data), data) + QtTask().downloadBack.emit(task.bakParam, 0, b"") + return request = task.req if request.params == None: request.params = {} @@ -183,3 +184,14 @@ def _Download(self, task): self.handler.get(task.req.__class__)(task) if task.res: task.res.close() + + def TestSpeed(self, request, bakParams="", testAddress=""): + bakAddress = self.address + self.address = testAddress + if testAddress: + self.imageServer = "storage.wikawika.xyz" + self.__DealHeaders(request, "") + self.address = bakAddress + task = Task(request, bakParams) + task.timeout = 2 + self._downloadQueue.put(task) diff --git a/src/server/user_handler.py b/src/server/user_handler.py index 2e9fa9c..830b379 100644 --- a/src/server/user_handler.py +++ b/src/server/user_handler.py @@ -6,7 +6,7 @@ from conf import config from src.qt.util.qttask import QtTask from .server import handler -from src.server import req, Status, Log +from src.server import req, Status, Log, ToolUtil @handler(req.InitReq) @@ -228,3 +228,38 @@ class GetKeywordsHandler(object): def __call__(self, backData): if backData.bakParam: QtTask().taskBack.emit(backData.bakParam, backData.res.raw.text) + + +@handler(req.SpeedTestReq) +class SpeedTestReq(object): + def __call__(self, backData): + if backData.status != Status.Ok: + if backData.bakParam: + QtTask().taskBack.emit(backData.bakParam, "") + else: + r = backData.res + try: + if r.status_code != 200: + if backData.bakParam: + QtTask().taskBack.emit(backData.bakParam, "") + return + + fileSize = int(r.headers.get('Content-Length', 0)) + getSize = 0 + now = time.time() + consume = 1 + for chunk in r.iter_content(chunk_size=1024): + getSize += len(chunk) + consume = time.time() - now + if consume >= 2: + break + + downloadSize = getSize / consume + speed = ToolUtil.GetDownloadSize(downloadSize) + if backData.bakParam: + QtTask().taskBack.emit(backData.bakParam, speed) + + except Exception as es: + Log.Error(es) + if backData.bakParam: + QtTask().taskBack.emit(backData.bakParam, "") diff --git a/ui/login.py b/ui/login.py index 23622af..57838ec 100644 --- a/ui/login.py +++ b/ui/login.py @@ -17,7 +17,7 @@ class Ui_Login(object): def setupUi(self, Login): if not Login.objectName(): Login.setObjectName(u"Login") - Login.resize(455, 205) + Login.resize(455, 237) self.gridLayout = QGridLayout(Login) self.gridLayout.setObjectName(u"gridLayout") self.horizontalSpacer_2 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) @@ -28,6 +28,20 @@ def setupUi(self, Login): self.verticalLayout.setObjectName(u"verticalLayout") self.gridLayout_2 = QGridLayout() self.gridLayout_2.setObjectName(u"gridLayout_2") + self.line = QFrame(Login) + self.line.setObjectName(u"line") + self.line.setFrameShape(QFrame.HLine) + self.line.setFrameShadow(QFrame.Sunken) + + self.gridLayout_2.addWidget(self.line, 2, 0, 1, 1) + + self.line_2 = QFrame(Login) + self.line_2.setObjectName(u"line_2") + self.line_2.setFrameShape(QFrame.HLine) + self.line_2.setFrameShadow(QFrame.Sunken) + + self.gridLayout_2.addWidget(self.line_2, 7, 0, 1, 1) + self.horizontalLayout_3 = QHBoxLayout() self.horizontalLayout_3.setObjectName(u"horizontalLayout_3") self.loginButton = QPushButton(Login) @@ -44,27 +58,15 @@ def setupUi(self, Login): self.horizontalLayout_3.addWidget(self.registerButton) + self.testSpeedButton = QPushButton(Login) + self.testSpeedButton.setObjectName(u"testSpeedButton") + self.testSpeedButton.setMinimumSize(QSize(100, 30)) + self.testSpeedButton.setMaximumSize(QSize(100, 16777215)) - self.gridLayout_2.addLayout(self.horizontalLayout_3, 3, 0, 1, 1) + self.horizontalLayout_3.addWidget(self.testSpeedButton) - self.horizontalLayout_2 = QHBoxLayout() - self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") - self.label_2 = QLabel(Login) - self.label_2.setObjectName(u"label_2") - self.label_2.setMinimumSize(QSize(50, 30)) - self.label_2.setAlignment(Qt.AlignCenter) - self.horizontalLayout_2.addWidget(self.label_2) - - self.passwdEdit = QLineEdit(Login) - self.passwdEdit.setObjectName(u"passwdEdit") - self.passwdEdit.setMinimumSize(QSize(300, 30)) - self.passwdEdit.setEchoMode(QLineEdit.Password) - - self.horizontalLayout_2.addWidget(self.passwdEdit) - - - self.gridLayout_2.addLayout(self.horizontalLayout_2, 2, 0, 1, 1) + self.gridLayout_2.addLayout(self.horizontalLayout_3, 6, 0, 1, 1) self.horizontalLayout = QHBoxLayout() self.horizontalLayout.setObjectName(u"horizontalLayout") @@ -82,27 +84,68 @@ def setupUi(self, Login): self.horizontalLayout.addWidget(self.userIdEdit) - self.gridLayout_2.addLayout(self.horizontalLayout, 1, 0, 1, 1) + self.gridLayout_2.addLayout(self.horizontalLayout, 3, 0, 1, 1) - self.verticalLayout_2 = QVBoxLayout() - self.verticalLayout_2.setObjectName(u"verticalLayout_2") self.label_3 = QLabel(Login) self.label_3.setObjectName(u"label_3") + self.label_3.setAlignment(Qt.AlignCenter) + + self.gridLayout_2.addWidget(self.label_3, 1, 0, 1, 1) + + self.verticalLayout_2 = QVBoxLayout() + self.verticalLayout_2.setObjectName(u"verticalLayout_2") + + self.gridLayout_2.addLayout(self.verticalLayout_2, 0, 0, 1, 1) + + self.horizontalLayout_2 = QHBoxLayout() + self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") + self.label_2 = QLabel(Login) + self.label_2.setObjectName(u"label_2") + self.label_2.setMinimumSize(QSize(50, 30)) + self.label_2.setAlignment(Qt.AlignCenter) + + self.horizontalLayout_2.addWidget(self.label_2) - self.verticalLayout_2.addWidget(self.label_3) + self.passwdEdit = QLineEdit(Login) + self.passwdEdit.setObjectName(u"passwdEdit") + self.passwdEdit.setMinimumSize(QSize(300, 30)) + self.passwdEdit.setEchoMode(QLineEdit.Password) + + self.horizontalLayout_2.addWidget(self.passwdEdit) + + + self.gridLayout_2.addLayout(self.horizontalLayout_2, 4, 0, 1, 1) + + self.gridLayout_4 = QGridLayout() + self.gridLayout_4.setObjectName(u"gridLayout_4") + self.label_5 = QLabel(Login) + self.label_5.setObjectName(u"label_5") + self.label_5.setAlignment(Qt.AlignCenter) + + self.gridLayout_4.addWidget(self.label_5, 0, 1, 1, 1) - self.horizontalLayout_4 = QHBoxLayout() - self.horizontalLayout_4.setObjectName(u"horizontalLayout_4") self.selectIp1 = QRadioButton(Login) self.selectIp1.setObjectName(u"selectIp1") + self.selectIp1.setLayoutDirection(Qt.LeftToRight) + self.selectIp1.setAutoFillBackground(False) - self.horizontalLayout_4.addWidget(self.selectIp1) + self.gridLayout_4.addWidget(self.selectIp1, 1, 0, 1, 1) + self.label_6 = QLabel(Login) + self.label_6.setObjectName(u"label_6") + self.label_6.setAlignment(Qt.AlignCenter) - self.verticalLayout_2.addLayout(self.horizontalLayout_4) + self.gridLayout_4.addWidget(self.label_6, 0, 2, 1, 1) - self.gridLayout_2.addLayout(self.verticalLayout_2, 0, 0, 1, 1) + self.gridLayout_2.addLayout(self.gridLayout_4, 8, 0, 1, 1) + + self.line_3 = QFrame(Login) + self.line_3.setObjectName(u"line_3") + self.line_3.setFrameShape(QFrame.HLine) + self.line_3.setFrameShadow(QFrame.Sunken) + + self.gridLayout_2.addWidget(self.line_3, 5, 0, 1, 1) self.verticalLayout.addLayout(self.gridLayout_2) @@ -130,6 +173,7 @@ def setupUi(self, Login): self.retranslateUi(Login) self.loginButton.clicked.connect(Login.Login) self.registerButton.clicked.connect(Login.OpenRegister) + self.testSpeedButton.clicked.connect(Login.SpeedTest) QMetaObject.connectSlotsByName(Login) # setupUi @@ -141,11 +185,14 @@ def retranslateUi(self, Login): self.loginButton.setShortcut(QCoreApplication.translate("Login", u"Return", None)) #endif // QT_CONFIG(shortcut) self.registerButton.setText(QCoreApplication.translate("Login", u"\u6ce8\u518c", None)) - self.label_2.setText(QCoreApplication.translate("Login", u"\u5bc6\u7801", None)) - self.passwdEdit.setText("") + self.testSpeedButton.setText(QCoreApplication.translate("Login", u"\u6d4b\u901f", None)) self.label.setText(QCoreApplication.translate("Login", u"\u5e10\u53f7", None)) self.userIdEdit.setText("") self.label_3.setText(QCoreApplication.translate("Login", u"\u5982\u679c\u4e0d\u80fd\u8fde\u63a5\u548c\u770b\u56fe\uff0c\u8bf7\u5c1d\u8bd5\u5176\u4ed6\u5206\u6d41", None)) + self.label_2.setText(QCoreApplication.translate("Login", u"\u5bc6\u7801", None)) + self.passwdEdit.setText("") + self.label_5.setText(QCoreApplication.translate("Login", u"\u76f4\u8fde", None)) self.selectIp1.setText(QCoreApplication.translate("Login", u"\u5206\u6d411", None)) + self.label_6.setText(QCoreApplication.translate("Login", u"\u4ee3\u7406", None)) # retranslateUi diff --git a/ui/login.ui b/ui/login.ui index 419f731..5fd6167 100644 --- a/ui/login.ui +++ b/ui/login.ui @@ -7,7 +7,7 @@ 0 0 455 - 205 + 237 @@ -31,7 +31,21 @@ - + + + + Qt::Horizontal + + + + + + + Qt::Horizontal + + + + @@ -74,12 +88,31 @@ + + + + + 100 + 30 + + + + + 100 + 16777215 + + + + 测速 + + + - - + + - + 50 @@ -87,7 +120,7 @@ - 密码 + 帐号 Qt::AlignCenter @@ -95,7 +128,7 @@ - + 300 @@ -105,17 +138,27 @@ - - QLineEdit::Password - - + + + 如果不能连接和看图,请尝试其他分流 + + + Qt::AlignCenter + + + + + + + + - + 50 @@ -123,7 +166,7 @@ - 帐号 + 密码 Qt::AlignCenter @@ -131,7 +174,7 @@ - + 300 @@ -141,32 +184,57 @@ + + QLineEdit::Password + - - - - + + + + - 如果不能连接和看图,请尝试其他分流 + 直连 + + + Qt::AlignCenter - - - - - - 分流1 - - - - + + + + Qt::LeftToRight + + + false + + + 分流1 + + + + + + + 代理 + + + Qt::AlignCenter + + + + + + Qt::Horizontal + + + @@ -229,7 +297,7 @@ 198 - 176 + 134 203 @@ -245,7 +313,7 @@ 354 - 176 + 134 145 @@ -253,9 +321,26 @@ + + testSpeedButton + clicked() + Login + SpeedTest() + + + 92 + 170 + + + 20 + 222 + + + Login() OpenRegister() + SpeedTest() diff --git a/ui/test.py b/ui/test.py index d6be7d8..1be893a 100644 --- a/ui/test.py +++ b/ui/test.py @@ -17,15 +17,23 @@ class Ui_Test(object): def setupUi(self, Test): if not Test.objectName(): Test.setObjectName(u"Test") - Test.resize(400, 286) + Test.resize(411, 301) Test.setAcceptDrops(False) self.gridLayout = QGridLayout(Test) self.gridLayout.setObjectName(u"gridLayout") self.gridLayout.setContentsMargins(0, 0, 0, 0) - self.graphicsView = QGraphicsView(Test) + self.frame = QFrame(Test) + self.frame.setObjectName(u"frame") + self.frame.setFrameShape(QFrame.StyledPanel) + self.frame.setFrameShadow(QFrame.Raised) + self.graphicsView = QGraphicsView(self.frame) self.graphicsView.setObjectName(u"graphicsView") + self.graphicsView.setGeometry(QRect(10, 50, 256, 192)) + self.label = QLabel(self.frame) + self.label.setObjectName(u"label") + self.label.setGeometry(QRect(250, 140, 54, 12)) - self.gridLayout.addWidget(self.graphicsView, 0, 0, 1, 1) + self.gridLayout.addWidget(self.frame, 0, 0, 1, 1) self.retranslateUi(Test) @@ -35,5 +43,6 @@ def setupUi(self, Test): def retranslateUi(self, Test): Test.setWindowTitle(QCoreApplication.translate("Test", u"Form", None)) + self.label.setText(QCoreApplication.translate("Test", u"TextLabel", None)) # retranslateUi diff --git a/ui/test.ui b/ui/test.ui index 686b682..b0a2bee 100644 --- a/ui/test.ui +++ b/ui/test.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 286 + 411 + 301 @@ -30,7 +30,37 @@ 0 - + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + 10 + 50 + 256 + 192 + + + + + + + 250 + 140 + 54 + 12 + + + + TextLabel + + +