新增读取剪贴板按钮

This commit is contained in:
JoeamAmier
2023-11-23 23:09:18 +08:00
parent e767f04151
commit b9dd05652d
3 changed files with 17 additions and 5 deletions

View File

@@ -19,18 +19,22 @@ class Settings:
def run(self): def run(self):
return self.read() if self.file.is_file() else self.create() return self.read() if self.file.is_file() else self.create()
def read(self): def read(self) -> dict:
with self.file.open("r", encoding="utf-8") as f: with self.file.open("r", encoding="utf-8") as f:
return load(f) return load(f)
def create(self): def create(self) -> dict:
with self.file.open("w", encoding="utf-8") as f: with self.file.open("w", encoding="utf-8") as f:
dump(self.default, f, indent=2) dump(self.default, f, indent=2)
return self.default return self.default
def update(self, data: dict):
with self.file.open("w", encoding="utf-8") as f:
dump(data, f, indent=2, ensure_ascii=False)
class Batch: class Batch:
file = Path("./xhs.txt") file = Path("../xhs.txt")
def read_txt(self) -> list: def read_txt(self) -> list:
if self.file.is_file(): if self.file.is_file():

View File

@@ -1,6 +1,7 @@
from pathlib import Path from pathlib import Path
from re import compile from re import compile
from pyperclip import paste
from textual.app import App from textual.app import App
from textual.app import ComposeResult from textual.app import ComposeResult
from textual.binding import Binding from textual.binding import Binding
@@ -102,9 +103,10 @@ class XHS:
class XHSDownloader(App): class XHSDownloader(App):
VERSION = 1.5 VERSION = 1.6
Beta = True Beta = True
CSS_PATH = "static/XHS-Downloader.tcss" CSS_PATH = Path(__file__).resolve().parent.parent.joinpath(
"static/XHS-Downloader.tcss")
BINDINGS = [ BINDINGS = [
Binding(key="q", action="quit", description="退出程序"), Binding(key="q", action="quit", description="退出程序"),
("d", "toggle_dark", "切换主题"), ("d", "toggle_dark", "切换主题"),
@@ -118,6 +120,7 @@ class XHSDownloader(App):
Input(placeholder="URL"), Input(placeholder="URL"),
HorizontalScroll(Button("下载无水印图片/视频", id="solo"), HorizontalScroll(Button("下载无水印图片/视频", id="solo"),
Button("读取 xhs.txt 文件并批量下载作品", id="batch"), Button("读取 xhs.txt 文件并批量下载作品", id="batch"),
Button("读取剪贴板", id="paste"),
Button("清空输入框", id="reset"), )) Button("清空输入框", id="reset"), ))
yield Log(auto_scroll=True) yield Log(auto_scroll=True)
yield Footer() yield Footer()
@@ -132,6 +135,8 @@ class XHSDownloader(App):
self.batch() self.batch()
elif event.button.id == "reset": elif event.button.id == "reset":
self.query_one(Input).value = "" self.query_one(Input).value = ""
elif event.button.id == "paste":
self.query_one(Input).value = paste()
def solo(self): def solo(self):
url = self.query_one(Input).value url = self.query_one(Input).value

View File

@@ -16,6 +16,9 @@ Button#solo {
Button#batch { Button#batch {
tint: green 35%; tint: green 35%;
} }
Button#paste {
tint: green 35%;
}
Button#reset { Button#reset {
tint: red 35%; tint: red 35%;
} }