diff --git a/README.md b/README.md index d97644f..0f49094 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,8 @@ print(xhs.extract(error_demo)) # 获取数据失败时返回空字典 print(xhs.extract(image_demo, download=download)) print(xhs.extract(video_demo, download=download)) +

⛓ 批量下载

+

在程序当前文件夹创建一个 xhs.txt 文本文件,然后将待处理的作品链接输入文件,每行输入一个作品链接,编辑完成后保存文件,然后运行程序,程序会自动读取 xhs.txt 文件内容,并批量下载每个作品的文件,下载完成后需要手动删除 xhs.txt 文件。

⚙️ 配置文件

根目录下的 settings.json 文件,可以自定义部分运行参数。

diff --git a/main.py b/main.py index 3484602..face9ca 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,14 @@ +from textual.app import App +from textual.app import ComposeResult +from textual.binding import Binding +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 Static + +from source import Batch from source import Settings from source import XHS @@ -31,13 +42,52 @@ def example(): def main(): """读取并应用配置文件设置的参数,适合一般作品文件下载需求""" xhs = XHS(**Settings().run()) - while True: - if url := input("请输入小红书作品链接:"): - xhs.extract(url, download=True) - else: - break + if ids := Batch().read_txt(): + for i in ids: + xhs.extract(i, download=True) + else: + while True: + if url := input("请输入小红书作品链接:"): + xhs.extract(url, download=True) + else: + break + + +class RunMenu(Static): + + def compose(self) -> ComposeResult: + yield Button("获取数据", id="run", variant="success") + yield Button("清空输入", id="reset") + + +class XHSDownloader(App): + CSS_PATH = "static/XHS_Downloader.tcss" + BINDINGS = [ + Binding(key="q", action="quit", description="结束运行"), + Binding( + key="w", + action="repository", + description="获取源码", + ), + ] + + def compose(self) -> ComposeResult: + yield Label("请输入小红书图文/视频作品链接:") + yield Input(placeholder="URL") + yield RunMenu() + yield Header() + yield Footer() + + def on_mount(self) -> None: + self.title = "小红书作品采集工具" + + @staticmethod + def action_repository(): + yield Label("Github Repository") if __name__ == '__main__': # example() main() + # app = XHSDownloader() + # app.run() diff --git a/source/Settings.py b/source/Settings.py index b1583db..7f510ac 100644 --- a/source/Settings.py +++ b/source/Settings.py @@ -2,7 +2,7 @@ from json import dump from json import load from pathlib import Path -__all__ = ['Settings'] +__all__ = ['Settings', 'Batch'] class Settings: @@ -26,3 +26,13 @@ class Settings: with self.path.open("w", encoding="utf-8") as f: dump(self.default, f, indent=2) return self.default + + +class Batch: + file = Path("./xhs.txt") + + def read_txt(self) -> list: + if self.file.is_file(): + with self.file.open("r") as f: + return f.readlines() + return [] diff --git a/source/__init__.py b/source/__init__.py index a260403..bee2aef 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -4,10 +4,11 @@ from .Download import Download from .Explore import Explore from .Html import Html from .Image import Image +from .Settings import Batch from .Settings import Settings from .Video import Video -__all__ = ['XHS', 'Settings'] +__all__ = ['XHS', 'Settings', 'Batch'] class XHS: @@ -49,6 +50,7 @@ class XHS: def extract(self, url: str, download=False) -> dict: if not self.__check(url): + print(f"无效的作品链接: {url}") return {} html = self.html.get_html(url) if not html: