mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
优化项目代码
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
from webbrowser import open
|
||||
|
||||
# from asyncio import sleep
|
||||
from pyperclip import paste
|
||||
from rich.text import Text
|
||||
from textual.app import App
|
||||
from textual import on
|
||||
from textual.app import ComposeResult
|
||||
from textual.binding import Binding
|
||||
from textual.containers import Center
|
||||
from textual.containers import HorizontalScroll
|
||||
from textual.containers import ScrollableContainer
|
||||
from textual.screen import Screen
|
||||
from textual.widgets import Button
|
||||
from textual.widgets import Footer
|
||||
from textual.widgets import Header
|
||||
@@ -17,12 +19,11 @@ from textual.widgets import ProgressBar
|
||||
from textual.widgets import RichLog
|
||||
|
||||
from source.application import XHS
|
||||
from source.module import Settings
|
||||
from source.module import ROOT
|
||||
from source.module import (
|
||||
VERSION_MAJOR,
|
||||
VERSION_MINOR,
|
||||
VERSION_BETA,
|
||||
ROOT,
|
||||
PROMPT,
|
||||
MASTER,
|
||||
ERROR,
|
||||
@@ -34,11 +35,9 @@ from source.module import (
|
||||
GENERAL,
|
||||
USERSCRIPT,
|
||||
)
|
||||
from source.translator import Chinese
|
||||
from source.translator import LANGUAGE
|
||||
from .setting import Setting
|
||||
from source.translator import (English, Chinese)
|
||||
|
||||
__all__ = ["XHSDownloader"]
|
||||
__all__ = ["Index"]
|
||||
|
||||
|
||||
def show_state(function):
|
||||
@@ -53,7 +52,7 @@ def show_state(function):
|
||||
return inner
|
||||
|
||||
|
||||
class XHSDownloader(App):
|
||||
class Index(Screen):
|
||||
CSS_PATH = ROOT.joinpath(
|
||||
"static/css/index.tcss")
|
||||
BINDINGS = [
|
||||
@@ -61,28 +60,18 @@ class XHSDownloader(App):
|
||||
# ("d", "toggle_dark", "切换主题"),
|
||||
Binding(key="u", action="check_update", description="检查更新"),
|
||||
Binding(key="m", action="user_script", description="获取脚本"),
|
||||
# Binding(key="l", action="choose_language", description="切换语言"),
|
||||
# Binding(key="s", action="settings", description="程序设置"),
|
||||
Binding(key="s", action="settings", description="程序设置"),
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, app: XHS, language: Chinese | English):
|
||||
super().__init__()
|
||||
settings = Settings(ROOT).run()
|
||||
self.prompt = LANGUAGE.get(settings["language"], Chinese)
|
||||
self.APP = XHS(**settings, language_object=self.prompt)
|
||||
self.app_ = app
|
||||
self.prompt = language
|
||||
self.url = None
|
||||
self.tip = None
|
||||
self.bar = None
|
||||
self.setting = None
|
||||
self.disclaimer = True
|
||||
|
||||
async def __aenter__(self):
|
||||
await self.APP.__aenter__()
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc_value, traceback):
|
||||
await self.APP.__aexit__(exc_type, exc_value, traceback)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header()
|
||||
yield ScrollableContainer(Label(Text(f"{self.prompt.open_source_protocol}{LICENCE}", style=MASTER)),
|
||||
@@ -96,7 +85,6 @@ class XHSDownloader(App):
|
||||
HorizontalScroll(Button(self.prompt.download_button, id="deal"),
|
||||
Button(self.prompt.paste_button, id="paste"),
|
||||
Button(self.prompt.reset_button, id="reset"), ),
|
||||
id="index",
|
||||
)
|
||||
with Center():
|
||||
yield ProgressBar(total=None, show_percentage=False, show_eta=False)
|
||||
@@ -106,8 +94,6 @@ class XHSDownloader(App):
|
||||
def on_mount(self) -> None:
|
||||
self.title = f"XHS-Downloader V{VERSION_MAJOR}.{
|
||||
VERSION_MINOR}{" Beta" if VERSION_BETA else ""}"
|
||||
|
||||
def on_ready(self) -> None:
|
||||
self.url = self.query_one(Input)
|
||||
self.tip = self.query_one(RichLog)
|
||||
self.bar = self.query_one(ProgressBar)
|
||||
@@ -118,20 +104,26 @@ class XHSDownloader(App):
|
||||
self.tip.clear()
|
||||
self.disclaimer = False
|
||||
|
||||
async def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
if event.button.id == "deal":
|
||||
await self.deal()
|
||||
elif event.button.id == "reset":
|
||||
self.query_one(Input).value = ""
|
||||
elif event.button.id == "paste":
|
||||
self.query_one(Input).value = paste()
|
||||
@on(Button.Pressed, "#deal")
|
||||
async def deal_button(self):
|
||||
await self.deal()
|
||||
|
||||
@on(Button.Pressed, "#reset")
|
||||
def reset_button(self):
|
||||
self.query_one(Input).value = ""
|
||||
|
||||
@on(Button.Pressed, "#paste")
|
||||
def paste_button(self):
|
||||
self.query_one(Input).value = paste()
|
||||
|
||||
@show_state
|
||||
async def deal(self):
|
||||
# TODO: 处理过程中,进度条异常卡顿,待排查!
|
||||
# await sleep(2)
|
||||
if not self.url.value:
|
||||
self.tip.write(Text(self.prompt.invalid_link, style=WARNING))
|
||||
return
|
||||
if any(await self.APP.extract(self.url.value, True, log=self.tip)):
|
||||
if any(await self.app_.extract(self.url.value, True, log=self.tip)):
|
||||
self.url.value = ""
|
||||
else:
|
||||
self.tip.write(Text(self.prompt.download_failure, style=ERROR))
|
||||
@@ -143,7 +135,7 @@ class XHSDownloader(App):
|
||||
self.prompt.check_update_notification,
|
||||
style=WARNING))
|
||||
try:
|
||||
url = await self.APP.html.request_url(RELEASES, False, self.tip)
|
||||
url = await self.app_.html.request_url(RELEASES, False, self.tip)
|
||||
latest_major, latest_minor = map(
|
||||
int, url.split("/")[-1].split(".", 1))
|
||||
if latest_major > VERSION_MAJOR or latest_minor > VERSION_MINOR:
|
||||
@@ -176,14 +168,3 @@ class XHSDownloader(App):
|
||||
@staticmethod
|
||||
def action_user_script():
|
||||
open(USERSCRIPT)
|
||||
|
||||
def action_choose_language(self):
|
||||
pass
|
||||
|
||||
def action_settings(self):
|
||||
if self.setting:
|
||||
self.setting.remove()
|
||||
self.setting = None
|
||||
else:
|
||||
self.setting = Setting()
|
||||
self.query_one("#index").mount(self.setting)
|
||||
|
||||
Reference in New Issue
Block a user