新增批量下载功能

This commit is contained in:
JoeamAmier 2023-09-04 19:24:17 +08:00
parent e8ee1736ce
commit 7b7d9700d7
4 changed files with 71 additions and 7 deletions

View File

@ -50,6 +50,8 @@ print(xhs.extract(error_demo)) # 获取数据失败时返回空字典
print(xhs.extract(image_demo, download=download))
print(xhs.extract(video_demo, download=download))
</pre>
<h1>⛓ 批量下载</h1>
<p>在程序当前文件夹创建一个 <code>xhs.txt</code> 文本文件,然后将待处理的作品链接输入文件,每行输入一个作品链接,编辑完成后保存文件,然后运行程序,程序会自动读取 <code>xhs.txt</code> 文件内容,并批量下载每个作品的文件,下载完成后需要手动删除 <code>xhs.txt</code> 文件。</p>
<h1>⚙️ 配置文件</h1>
<p>根目录下的 <code>settings.json</code> 文件,可以自定义部分运行参数。</p>
<table>

60
main.py
View File

@ -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()

View File

@ -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 []

View File

@ -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: