Skip to content

Commit

Permalink
优化了代码逻辑。
Browse files Browse the repository at this point in the history
  • Loading branch information
LxHTT committed Apr 3, 2023
1 parent e81fc7f commit c9c93b9
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 127 deletions.
6 changes: 4 additions & 2 deletions MCSL2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from re import search
from subprocess import check_output, STDOUT
from json import dumps
from json import dumps, loads
from os import getcwd
from shutil import copy
from sys import argv, exit
Expand All @@ -12,9 +12,11 @@
QGraphicsDropShadowEffect,
QMainWindow,
)
from requests import get

from MCSL2_Libs import MCSL2_Icon as _ # noqa: F401
from MCSL2_Libs import MCSL2_JavaDetector
from MCSL2_Libs.MCSL2_DownloadURLParser import FetchDownloadURLThreadFactory
from MCSL2_Libs.MCSL2_MainWindow import * # noqa: F403
from MCSL2_Libs.MCSL2_Utils import *

Expand Down Expand Up @@ -730,7 +732,7 @@ def initDownloadLayout(self, layout, subWidgetNames, pixMap):
CallMCSL2Dialog(Tip, isNeededTwoButtons=0)
return
elif subWidgetNames == -1:
Tip = "可能解析api内容失败\n\n请检查网络或自己的节点设置"
Tip = "可能解析API内容失败\n\n请检查网络或自己的节点设置"
CallMCSL2Dialog(Tip, isNeededTwoButtons=0)
return

Expand Down
6 changes: 5 additions & 1 deletion MCSL2_Libs/MCSL2_Aria2Controller.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# import aria2p
from aria2p.client import Client

class Aria2Controller():
pass
# MCSL2_Aria2Client = Client(host="http://localhost", port=60800, secret="")
114 changes: 114 additions & 0 deletions MCSL2_Libs/MCSL2_DownloadURLParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from typing import Dict, Callable
from PyQt5.QtCore import pyqtSignal, QThread
from requests import get
from json import loads


def Singleton(cls):
Instances = {}

def GetInstance(*args, **kwargs):
if cls not in Instances:
Instances[cls] = cls(*args, **kwargs)
return Instances[cls]

return GetInstance


class DownloadURLParser:
def __init__(self):
pass

def ParseDownloaderAPIUrl(DownloadSource):
UrlPrefix = "https://raw.iqiq.io/LxHTT/MCSLDownloaderAPI/master/"
SourceSuffix = ["SharePoint", "Gitee", "luoxisCloud", "GHProxy", "GitHub"]
TypeSuffix = [
"/JavaDownloadInfo.json",
"/SpigotDownloadInfo.json",
"/PaperDownloadInfo.json",
"/BungeeCordDownloadInfo.json",
"/OfficialCoreDownloadInfo.json",
]
rv = {}
for i in range(len(TypeSuffix)):
DownloadAPIUrl = UrlPrefix + SourceSuffix[DownloadSource] + TypeSuffix[i]
SubWidgetNames, DownloadUrls, FileNames, FileFormats = DownloadURLParser.DecodeDownloadJsons(DownloadAPIUrl)
rv.update({
i: dict(zip(("SubWidgetNames", "DownloadUrls", "FileNames", "FileFormats"),
(SubWidgetNames, DownloadUrls, FileNames, FileFormats)))
})
return {DownloadSource: rv}

def DecodeDownloadJsons(RefreshUrl):
SubWidgetNames = []
DownloadUrls = []
FileFormats = []
FileNames = []
try:
DownloadJson = get(RefreshUrl).text
except:
return -2, -2, -2, -2
try:
PyDownloadList = loads(DownloadJson)["MCSLDownloadList"]
for i in PyDownloadList:
SubWidgetName = i["name"]
SubWidgetNames.insert(0, SubWidgetName)
DownloadUrl = i["url"]
DownloadUrls.insert(0, DownloadUrl)
FileFormat = i["format"]
FileFormats.insert(0, FileFormat)
FileName = i["filename"]
FileNames.insert(0, FileName)
return SubWidgetNames, DownloadUrls, FileNames, FileFormats
except:
return -1, -1, -1, -1


class FetchDownloadURLThread(QThread):
"""
用于获取网页内容的线程
结束时发射fetchSignal信号,参数为url和data组成的元组
"""
fetchSignal = pyqtSignal(dict)

def __init__(self, DownloadSource, FinishSlot: Callable = ...):
super().__init__()
self._id = None
self.DownloadSource = DownloadSource
self.Data = None
if FinishSlot is not ...:
self.fetchSignal.connect(FinishSlot)

def getURL(self):
return self.url

def run(self):
self.fetchSignal.emit(DownloadURLParser.ParseDownloaderAPIUrl(self.DownloadSource))

def getData(self):
return self.Data


@Singleton
class FetchDownloadURLThreadFactory:
singletonThread: Dict[int, FetchDownloadURLThread] = {}

@classmethod
def create(cls,
downloadSrc: int,
_singleton=False,
finishSlot=...) -> FetchDownloadURLThread:

# print({k: v.isRunning() for k, v in cls.singletonThread.items()})
if _singleton:

if downloadSrc in cls.singletonThread and cls.singletonThread[downloadSrc].isRunning():
# print("线程已存在,返回已存在的线程")

return cls.singletonThread[downloadSrc]
else:
thread = FetchDownloadURLThread(downloadSrc, finishSlot)
cls.singletonThread[downloadSrc] = thread
return thread
else:
return FetchDownloadURLThread(downloadSrc, finishSlot)
129 changes: 10 additions & 119 deletions MCSL2_Libs/MCSL2_Utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from json import load, loads, dump
from json import load, dump
from os import mkdir, listdir
from os import path as ospath
from typing import Callable, Any, Dict
from typing import Callable, Any

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QDialog
from requests import get


from MCSL2_Libs.MCSL2_AskDialog import Ui_MCSL2_AskDialog
from MCSL2_Libs.MCSL2_Dialog import Ui_MCSL2_Dialog
Expand Down Expand Up @@ -55,74 +55,13 @@ def InitMCSL():
pass


def ParseDownloaderAPIUrl(DownloadSource, DownloadType):
UrlPrefix = "https://raw.iqiq.io/LxHTT/MCSLDownloaderAPI/master/"
SourceSuffix = ["SharePoint", "Gitee", "luoxisCloud", "GHProxy", "GitHub"]
TypeSuffix = [
"/JavaDownloadInfo.json",
"/SpigotDownloadInfo.json",
"/PaperDownloadInfo.json",
"/BungeeCordDownloadInfo.json",
"/OfficialCoreDownloadInfo.json",
]
DownloadAPIUrl = UrlPrefix + SourceSuffix[DownloadSource] + TypeSuffix[DownloadType]
DecodeDownloadJsonsSS = DecodeDownloadJsons(DownloadAPIUrl)
SubWidgetNames = DecodeDownloadJsonsSS[0]
DownloadUrls = DecodeDownloadJsonsSS[1]
FileNames = DecodeDownloadJsonsSS[2]
FileFormats = DecodeDownloadJsonsSS[3]
return SubWidgetNames, DownloadUrls, FileNames, FileFormats


def ParseDownloaderAPIUrl1(DownloadSource):
UrlPrefix = "https://raw.iqiq.io/LxHTT/MCSLDownloaderAPI/master/"
SourceSuffix = ["SharePoint", "Gitee", "luoxisCloud", "GHProxy", "GitHub"]
TypeSuffix = [
"/JavaDownloadInfo.json",
"/SpigotDownloadInfo.json",
"/PaperDownloadInfo.json",
"/BungeeCordDownloadInfo.json",
"/OfficialCoreDownloadInfo.json",
]
rv = {}
for i in range(len(TypeSuffix)):
DownloadAPIUrl = UrlPrefix + SourceSuffix[DownloadSource] + TypeSuffix[i]
SubWidgetNames, DownloadUrls, FileNames, FileFormats = DecodeDownloadJsons(DownloadAPIUrl)
rv.update({
i: dict(zip(("SubWidgetNames", "DownloadUrls", "FileNames", "FileFormats"),
(SubWidgetNames, DownloadUrls, FileNames, FileFormats)))
})
return {DownloadSource: rv}


def DecodeDownloadJsons(RefreshUrl):
SubWidgetNames = []
DownloadUrls = []
FileFormats = []
FileNames = []
try:
DownloadJson = get(RefreshUrl).text
except:
# Tip = "无法连接MCSLAPI,\n\n请检查网络或系统代理设置"
# CallMCSL2Dialog(Tip, isNeededTwoButtons=0)
return -2, -2, -2, -2
try:
PyDownloadList = loads(DownloadJson)["MCSLDownloadList"]
for i in PyDownloadList:
SubWidgetName = i["name"]
SubWidgetNames.insert(0, SubWidgetName)
DownloadUrl = i["url"]
DownloadUrls.insert(0, DownloadUrl)
FileFormat = i["format"]
FileFormats.insert(0, FileFormat)
FileName = i["filename"]
FileNames.insert(0, FileName)
return SubWidgetNames, DownloadUrls, FileNames, FileFormats
except:
# print(DownloadJson)
# Tip = "可能解析api内容失败\n\n请检查网络或自己的节点设置"
# CallMCSL2Dialog(Tip, isNeededTwoButtons=0)
return -1, -1, -1, -1









def ReadJsonFile(FileName):
Expand Down Expand Up @@ -230,51 +169,3 @@ def Create(cls, FileName, Data, FinishSlot: Callable[[], Any] = ...) -> JsonSave
return JsonSaveThread(FileName, Data, FinishSlot)


class FetchDownloadURLThread(QThread):
"""
用于获取网页内容的线程
结束时发射fetchSignal信号,参数为url和data组成的元组
"""
fetchSignal = pyqtSignal(dict)

def __init__(self, DownloadSource, FinishSlot: Callable = ...):
super().__init__()
self._id = None
self.DownloadSource = DownloadSource
self.Data = None
if FinishSlot is not ...:
self.fetchSignal.connect(FinishSlot)

def getURL(self):
return self.url

def run(self):
self.fetchSignal.emit(ParseDownloaderAPIUrl1(self.DownloadSource))

def getData(self):
return self.Data


@Singleton
class FetchDownloadURLThreadFactory:
singletonThread: Dict[int, FetchDownloadURLThread] = {}

@classmethod
def create(cls,
downloadSrc: int,
_singleton=False,
finishSlot=...) -> FetchDownloadURLThread:

# print({k: v.isRunning() for k, v in cls.singletonThread.items()})
if _singleton:

if downloadSrc in cls.singletonThread and cls.singletonThread[downloadSrc].isRunning():
# print("线程已存在,返回已存在的线程")

return cls.singletonThread[downloadSrc]
else:
thread = FetchDownloadURLThread(downloadSrc, finishSlot)
cls.singletonThread[downloadSrc] = thread
return thread
else:
return FetchDownloadURLThread(downloadSrc, finishSlot)
6 changes: 2 additions & 4 deletions Tools/Aria2/aria2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ enable-rpc=true
rpc-allow-origin-all=true
rpc-listen-all=true
event-poll=select
rpc-listen-port=6800
rpc-listen-port=60800

force-save=false
dht-file-path=dht.dat
dht-file-path6=dht6.dat
force-save=false
Binary file modified Tools/Aria2/aria2c.exe
Binary file not shown.
Binary file removed Tools/Aria2/dht.dat
Binary file not shown.
Binary file removed Tools/Aria2/dht6.dat
Binary file not shown.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PyQt5>=5.15.4
pyqt5-tools>=5.15.4.3.2
requests>=2.28.2
requests>=2.28.2

0 comments on commit c9c93b9

Please sign in to comment.