diff --git a/main.py b/main.py index 19b65ee..643159b 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ from textual.widgets import Footer from textual.widgets import Header from textual.widgets import Input from textual.widgets import Label +from textual.widgets import Log from textual.widgets import Pretty from source import Batch @@ -14,9 +15,6 @@ from source import Settings from source import XHS -# from textual.widgets import Log - - def example(): """通过代码设置参数,适合二次开发""" # 测试链接 @@ -67,37 +65,45 @@ class XHSDownloader(App): Binding(key="q", action="quit", description="退出程序"), ("d", "toggle_dark", "切换主题"), ] - xhs = XHS(**Settings().run()) + APP = XHS(**Settings().run()) + Batch = Batch() def compose(self) -> ComposeResult: yield Header() yield Label("请输入小红书图文/视频作品链接:") - yield Input(id="url", placeholder="URL") + yield Input(placeholder="URL") yield HorizontalScroll(Button("下载无水印图片/视频", id="solo", variant="success"), Button("读取文件并开始批量下载作品", id="batch", variant="success"), - Button("清空输入", id="reset", variant="error"), ) - # yield Log(auto_scroll=True) - yield Pretty({"采集结果": "未采集到任何数据!"}) + Button("清空输入框", id="reset", variant="error"), ) + yield Log(auto_scroll=True) + yield Pretty( + {"运行结果": "输入小红书图文/视频作品链接,点击“下载无水印图片/视频”按钮即可获取作品数据并下载作品文件!"}) yield Footer() def on_mount(self) -> None: self.title = "小红书作品采集工具" def on_button_pressed(self, event: Button.Pressed) -> None: - if event.button.id in ("solo", "batch"): + if event.button.id == "solo": self.solo() elif event.button.id == "batch": self.batch() elif event.button.id == "reset": - self.query_one("#url").value = "" + self.query_one(Input).value = "" def solo(self): - result = self.xhs.extract(self.query_one("#url").value, download=True) + url = self.query_one(Input).value + result = self.APP.extract(url, download=True) # self.query_one(Log).write_line() self.query_one(Pretty).update(result) def batch(self): - pass + urls = self.Batch.read_txt() + if not urls: + self.query_one(Log).write_line("未检测到 xhs.txt 文件 或者 该文件为空!") + for url in urls: + self.query_one(Log).write_line(f"当前作品链接: {url}") + self.APP.extract(url) if __name__ == '__main__':