Please enable Javascript to view the contents

得到电子书批量下载

 ·  ☕ 2 分钟

前面介绍Windows和MacOS系统下载得到App电子书的教程,但都是单个下载,比较麻烦。今天介绍一下如何批量下载得到电子书,适用于Windows、MacOS和Linux系统。

dedao-dl

Github 上有一个项目dedao-dl的命令行工具。

yann0917/dedao-dl: 得到 APP 课程下载工具,可在终端查看文章内容,可生成 PDF,音频文件,markdown 文稿,可下载电子书。

它是先获取要下载的电子书 id, 然后再通过id下载电子书。

安装 dedao-dl

1
go install github.com/yann0917/dedao-dl@latest

登录

1
dedao-dl login -q

扫描二维码登录。

获取电子书 id

1
dedao-dl ebook

下载电子书

1
dedao-dl dle id -t 3 # -t 下载格式, 1:mp3, 2:PDF文档, 3:markdown文档 (default 1)

批量下载

批量下载就是将所有的电子书id的下载命令,拼接在一起,拷贝到终端一次性执行。这样就可以一次性下载多本电子书。

获取电子书 id

将所有电子书的id重定向到文件中。

1
dedao-dl ebook > ebooks.txt

提取电子书 id,组合下载命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# main.py
import argparse
import os

def load_existing_books(exist_file):
    if not exist_file or not os.path.exists(exist_file):
        return set()
    
    existing_books = set()
    with open(exist_file, 'r', encoding='utf-8') as file:
        for line in file:
            parts = line.split('_')
            if len(parts) > 1:
                id = parts[0]
                ext = parts[-1].split('.')[-1].strip()
                existing_books.add((id, ext))
    return existing_books

def extract_ids_and_generate_commands(input_file, output_file, format_type, exist_file=None):
    existing_books = load_existing_books(exist_file)
    
    with open(input_file, 'r', encoding='utf-8') as infile:
        lines = infile.readlines()

    ids = []
    for line in lines:
        if line.startswith('|'):
            parts = line.split('|')
            if len(parts) > 2 and parts[2].strip().isdigit():
                ids.append(parts[2].strip())

    commands = []
    for id in ids:
        if format_type == 4:
            if not (id, 'pdf') in existing_books or not (id, 'epub') in existing_books:
                if (id, 'pdf') not in existing_books:
                    commands.append(f"dedao-dl dle {id} -t 2")
                if (id, 'epub') not in existing_books:
                    commands.append(f"dedao-dl dle {id} -t 3")
        else:
            ext = {1: 'html', 2: 'pdf', 3: 'epub'}.get(format_type, 'epub')
            if (id, ext) not in existing_books:
                commands.append(f"dedao-dl dle {id} -t {format_type}")

    with open(output_file, 'w', encoding='utf-8') as outfile:
        outfile.write('\n'.join(commands))

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Extract IDs and generate commands.")
    parser.add_argument('-i', '--input', type=str, required=True, help='Input file path')
    parser.add_argument('-o', '--output', type=str, required=True, help='Output file path')
    parser.add_argument('-t', '--type', type=int, default=3, choices=[1, 2, 3, 4], help='Format type: 1 for html, 2 for pdf, 3 for epub, 4 for both pdf and epub')
    parser.add_argument('-e', '--exist', type=str, help='Exist file path')
    args = parser.parse_args()

    extract_ids_and_generate_commands(args.input, args.output, args.type, args.exist)

执行脚本即可生成下载命令。

1
python3 main.py -i ebooks.txt -o output.txt

output.txt 文件内容如下:

1
2
3
4
5
6
7
8
dedao-dl dle 142815 -t 2
dedao-dl dle 142815 -t 3
dedao-dl dle 142574 -t 2
dedao-dl dle 142574 -t 3
dedao-dl dle 142572 -t 2
dedao-dl dle 142572 -t 3
dedao-dl dle 142817 -t 2
dedao-dl dle 142817 -t 3

拷贝到终端执行即可。

代码仓库

mzhren/dedao-dl-batch: 得到电子书批量下载,基于dedao-dl

分享

码中人
作者
码中人
Web Developer