From e767f0415180db16b23fa899bdb9f66dca59ff68 Mon Sep 17 00:00:00 2001 From: JoeamAmier Date: Wed, 22 Nov 2023 21:11:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 66 ++-------------------------------------------- source/__init__.py | 62 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 65 deletions(-) diff --git a/main.py b/main.py index 94b1c97..622c109 100644 --- a/main.py +++ b/main.py @@ -1,18 +1,5 @@ -from textual.app import App -from textual.app import ComposeResult -from textual.binding import Binding -from textual.containers import HorizontalScroll -from textual.containers import ScrollableContainer -from textual.widgets import Button -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 source import Batch -from source import Settings from source import XHS +from source import XHSDownloader def example(): @@ -43,55 +30,6 @@ def example(): print(xhs.extract(video_demo, download=download)) -class XHSDownloader(App): - VERSION = 1.5 - Beta = True - CSS_PATH = "static/XHS-Downloader.tcss" - BINDINGS = [ - Binding(key="q", action="quit", description="退出程序"), - ("d", "toggle_dark", "切换主题"), - ] - APP = XHS(**Settings().run()) - Batch = Batch() - - def compose(self) -> ComposeResult: - yield Header() - yield ScrollableContainer(Label("请输入小红书图文/视频作品链接:"), - Input(placeholder="URL"), - HorizontalScroll(Button("下载无水印图片/视频", id="solo"), - Button("读取 xhs.txt 文件并批量下载作品", id="batch"), - Button("清空输入框", id="reset"), )) - yield Log(auto_scroll=True) - yield Footer() - - def on_mount(self) -> None: - self.title = f"小红书作品采集工具 V{self.VERSION}{" Beta" if self.Beta else ""}" - - def on_button_pressed(self, event: Button.Pressed) -> None: - if event.button.id == "solo": - self.solo() - elif event.button.id == "batch": - self.batch() - elif event.button.id == "reset": - self.query_one(Input).value = "" - - def solo(self): - url = self.query_one(Input).value - log = self.query_one(Log) - log.write_line(f"当前作品链接: {url}") - self.APP.extract(url, True, log) - - def batch(self): - urls = self.Batch.read_txt() - log = self.query_one(Log) - if not urls: - log.write_line("未检测到 xhs.txt 文件 或者 该文件为空!") - for url in urls: - log.write_line(f"当前作品链接: {url}") - self.APP.extract(url, True, log) - - if __name__ == '__main__': # example() - app = XHSDownloader() - app.run() + XHSDownloader().run() diff --git a/source/__init__.py b/source/__init__.py index 1ad5e3d..2ec1098 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -1,6 +1,18 @@ from pathlib import Path from re import compile +from textual.app import App +from textual.app import ComposeResult +from textual.binding import Binding +from textual.containers import HorizontalScroll +from textual.containers import ScrollableContainer +from textual.widgets import Button +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 .Download import Download from .Explore import Explore from .Html import Html @@ -9,7 +21,7 @@ from .Settings import Batch from .Settings import Settings from .Video import Video -__all__ = ['XHS', 'Settings', 'Batch'] +__all__ = ['XHS', 'XHSDownloader'] class XHS: @@ -87,3 +99,51 @@ class XHS: def __update_cookie(self, cookie: str) -> None: if cookie and isinstance(cookie, str): self.headers["Cookie"] = cookie + + +class XHSDownloader(App): + VERSION = 1.5 + Beta = True + CSS_PATH = "static/XHS-Downloader.tcss" + BINDINGS = [ + Binding(key="q", action="quit", description="退出程序"), + ("d", "toggle_dark", "切换主题"), + ] + APP = XHS(**Settings().run()) + Batch = Batch() + + def compose(self) -> ComposeResult: + yield Header() + yield ScrollableContainer(Label("请输入小红书图文/视频作品链接:"), + Input(placeholder="URL"), + HorizontalScroll(Button("下载无水印图片/视频", id="solo"), + Button("读取 xhs.txt 文件并批量下载作品", id="batch"), + Button("清空输入框", id="reset"), )) + yield Log(auto_scroll=True) + yield Footer() + + def on_mount(self) -> None: + self.title = f"小红书作品采集工具 V{self.VERSION}{" Beta" if self.Beta else ""}" + + def on_button_pressed(self, event: Button.Pressed) -> None: + if event.button.id == "solo": + self.solo() + elif event.button.id == "batch": + self.batch() + elif event.button.id == "reset": + self.query_one(Input).value = "" + + def solo(self): + url = self.query_one(Input).value + log = self.query_one(Log) + log.write_line(f"当前作品链接: {url}") + self.APP.extract(url, True, log) + + def batch(self): + urls = self.Batch.read_txt() + log = self.query_one(Log) + if not urls: + log.write_line("未检测到 xhs.txt 文件 或者 该文件为空!") + for url in urls: + log.write_line(f"当前作品链接: {url}") + self.APP.extract(url, True, log)