mirror of
https://github.com/JoeanAmier/XHS-Downloader.git
synced 2026-03-22 06:57:16 +08:00
优化项目代码
This commit is contained in:
@@ -6,7 +6,6 @@ from rich.text import Text
|
||||
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
|
||||
@@ -15,11 +14,9 @@ from textual.widgets import Footer
|
||||
from textual.widgets import Header
|
||||
from textual.widgets import Input
|
||||
from textual.widgets import Label
|
||||
from textual.widgets import ProgressBar
|
||||
from textual.widgets import RichLog
|
||||
|
||||
from source.application import XHS
|
||||
from source.module import ROOT
|
||||
from source.module import (
|
||||
VERSION_MAJOR,
|
||||
VERSION_MINOR,
|
||||
@@ -28,32 +25,20 @@ from source.module import (
|
||||
MASTER,
|
||||
ERROR,
|
||||
WARNING,
|
||||
INFO,
|
||||
LICENCE,
|
||||
REPOSITORY,
|
||||
RELEASES,
|
||||
GENERAL,
|
||||
USERSCRIPT,
|
||||
)
|
||||
from source.translator import (English, Chinese)
|
||||
from source.translator import (
|
||||
English,
|
||||
Chinese,
|
||||
)
|
||||
|
||||
__all__ = ["Index"]
|
||||
|
||||
|
||||
def show_state(function):
|
||||
async def inner(self, *args, **kwargs):
|
||||
self.close_disclaimer()
|
||||
self.bar.update(total=100, progress=100)
|
||||
result = await function(self, *args, **kwargs)
|
||||
self.bar.update(total=None)
|
||||
self.tip.write(Text(">" * 50, style=GENERAL))
|
||||
return result
|
||||
|
||||
return inner
|
||||
|
||||
|
||||
class Index(Screen):
|
||||
CSS_PATH = ROOT.joinpath("static/XHS-Downloader.tcss")
|
||||
BINDINGS = [
|
||||
Binding(key="q", action="quit", description="退出程序/Quit"),
|
||||
Binding(key="u", action="check_update", description="检查更新/Update"),
|
||||
@@ -63,12 +48,10 @@ class Index(Screen):
|
||||
|
||||
def __init__(self, app: XHS, language: Chinese | English):
|
||||
super().__init__()
|
||||
self.app_ = app
|
||||
self.xhs = app
|
||||
self.prompt = language
|
||||
self.url = None
|
||||
self.tip = None
|
||||
self.bar = None
|
||||
self.disclaimer = True
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header()
|
||||
@@ -95,9 +78,7 @@ class Index(Screen):
|
||||
Button(self.prompt.reset_button, id="reset"),
|
||||
),
|
||||
)
|
||||
with Center():
|
||||
yield ProgressBar(total=None, show_percentage=False, show_eta=False)
|
||||
yield RichLog(markup=True)
|
||||
yield RichLog(markup=True, wrap=True)
|
||||
yield Footer()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
@@ -105,17 +86,15 @@ class Index(Screen):
|
||||
VERSION_MINOR}{" Beta" if VERSION_BETA else ""}"
|
||||
self.url = self.query_one(Input)
|
||||
self.tip = self.query_one(RichLog)
|
||||
self.bar = self.query_one(ProgressBar)
|
||||
self.tip.write(Text("\n".join(self.prompt.disclaimer), style=MASTER))
|
||||
|
||||
def close_disclaimer(self):
|
||||
if self.disclaimer:
|
||||
self.tip.clear()
|
||||
self.disclaimer = False
|
||||
|
||||
@on(Button.Pressed, "#deal")
|
||||
def deal_button(self):
|
||||
create_task(self.deal())
|
||||
async def deal_button(self):
|
||||
if self.url.value:
|
||||
await create_task(self.deal())
|
||||
else:
|
||||
self.tip.write(Text(self.prompt.invalid_link, style=WARNING))
|
||||
self.tip.write(Text(">" * 50, style=GENERAL))
|
||||
|
||||
@on(Button.Pressed, "#reset")
|
||||
def reset_button(self):
|
||||
@@ -125,52 +104,13 @@ class Index(Screen):
|
||||
def paste_button(self):
|
||||
self.query_one(Input).value = paste()
|
||||
|
||||
@show_state
|
||||
async def deal(self):
|
||||
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)):
|
||||
await self.app.push_screen("loading")
|
||||
if any(await self.xhs.extract(self.url.value, True, log=self.tip)):
|
||||
self.url.value = ""
|
||||
else:
|
||||
self.tip.write(Text(self.prompt.download_failure, style=ERROR))
|
||||
|
||||
@show_state
|
||||
async def action_check_update(self):
|
||||
self.tip.write(
|
||||
Text(
|
||||
self.prompt.check_update_notification,
|
||||
style=WARNING))
|
||||
try:
|
||||
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:
|
||||
self.tip.write(
|
||||
Text(
|
||||
self.prompt.official_version_update(
|
||||
latest_major,
|
||||
latest_minor),
|
||||
style=WARNING))
|
||||
self.tip.write(RELEASES)
|
||||
elif latest_minor == VERSION_MINOR and VERSION_BETA:
|
||||
self.tip.write(
|
||||
Text(
|
||||
self.prompt.development_version_update,
|
||||
style=WARNING))
|
||||
self.tip.write(RELEASES)
|
||||
elif VERSION_BETA:
|
||||
self.tip.write(
|
||||
Text(
|
||||
self.prompt.latest_development_version,
|
||||
style=WARNING))
|
||||
else:
|
||||
self.tip.write(
|
||||
Text(
|
||||
self.prompt.latest_official_version,
|
||||
style=INFO))
|
||||
except ValueError:
|
||||
self.tip.write(Text(self.prompt.check_update_failure, style=ERROR))
|
||||
self.app.pop_screen()
|
||||
|
||||
@staticmethod
|
||||
def action_user_script():
|
||||
|
||||
Reference in New Issue
Block a user