novel-downloader 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- novel_downloader/__init__.py +14 -0
- novel_downloader/cli/__init__.py +14 -0
- novel_downloader/cli/clean.py +134 -0
- novel_downloader/cli/download.py +132 -0
- novel_downloader/cli/interactive.py +67 -0
- novel_downloader/cli/main.py +45 -0
- novel_downloader/cli/settings.py +177 -0
- novel_downloader/config/__init__.py +52 -0
- novel_downloader/config/adapter.py +153 -0
- novel_downloader/config/loader.py +177 -0
- novel_downloader/config/models.py +173 -0
- novel_downloader/config/site_rules.py +97 -0
- novel_downloader/core/__init__.py +25 -0
- novel_downloader/core/downloaders/__init__.py +22 -0
- novel_downloader/core/downloaders/base_async_downloader.py +157 -0
- novel_downloader/core/downloaders/base_downloader.py +187 -0
- novel_downloader/core/downloaders/common_asynb_downloader.py +207 -0
- novel_downloader/core/downloaders/common_downloader.py +191 -0
- novel_downloader/core/downloaders/qidian_downloader.py +208 -0
- novel_downloader/core/factory/__init__.py +33 -0
- novel_downloader/core/factory/downloader_factory.py +149 -0
- novel_downloader/core/factory/parser_factory.py +62 -0
- novel_downloader/core/factory/requester_factory.py +106 -0
- novel_downloader/core/factory/saver_factory.py +49 -0
- novel_downloader/core/interfaces/__init__.py +32 -0
- novel_downloader/core/interfaces/async_downloader_protocol.py +37 -0
- novel_downloader/core/interfaces/async_requester_protocol.py +68 -0
- novel_downloader/core/interfaces/downloader_protocol.py +37 -0
- novel_downloader/core/interfaces/parser_protocol.py +40 -0
- novel_downloader/core/interfaces/requester_protocol.py +65 -0
- novel_downloader/core/interfaces/saver_protocol.py +61 -0
- novel_downloader/core/parsers/__init__.py +28 -0
- novel_downloader/core/parsers/base_parser.py +96 -0
- novel_downloader/core/parsers/common_parser/__init__.py +14 -0
- novel_downloader/core/parsers/common_parser/helper.py +321 -0
- novel_downloader/core/parsers/common_parser/main_parser.py +86 -0
- novel_downloader/core/parsers/qidian_parser/__init__.py +20 -0
- novel_downloader/core/parsers/qidian_parser/browser/__init__.py +13 -0
- novel_downloader/core/parsers/qidian_parser/browser/chapter_encrypted.py +498 -0
- novel_downloader/core/parsers/qidian_parser/browser/chapter_normal.py +97 -0
- novel_downloader/core/parsers/qidian_parser/browser/chapter_router.py +70 -0
- novel_downloader/core/parsers/qidian_parser/browser/main_parser.py +110 -0
- novel_downloader/core/parsers/qidian_parser/session/__init__.py +13 -0
- novel_downloader/core/parsers/qidian_parser/session/chapter_encrypted.py +451 -0
- novel_downloader/core/parsers/qidian_parser/session/chapter_normal.py +119 -0
- novel_downloader/core/parsers/qidian_parser/session/chapter_router.py +67 -0
- novel_downloader/core/parsers/qidian_parser/session/main_parser.py +113 -0
- novel_downloader/core/parsers/qidian_parser/session/node_decryptor.py +164 -0
- novel_downloader/core/parsers/qidian_parser/shared/__init__.py +38 -0
- novel_downloader/core/parsers/qidian_parser/shared/book_info_parser.py +95 -0
- novel_downloader/core/parsers/qidian_parser/shared/helpers.py +133 -0
- novel_downloader/core/requesters/__init__.py +31 -0
- novel_downloader/core/requesters/base_async_session.py +297 -0
- novel_downloader/core/requesters/base_browser.py +210 -0
- novel_downloader/core/requesters/base_session.py +243 -0
- novel_downloader/core/requesters/common_requester/__init__.py +18 -0
- novel_downloader/core/requesters/common_requester/common_async_session.py +96 -0
- novel_downloader/core/requesters/common_requester/common_session.py +126 -0
- novel_downloader/core/requesters/qidian_requester/__init__.py +22 -0
- novel_downloader/core/requesters/qidian_requester/qidian_broswer.py +377 -0
- novel_downloader/core/requesters/qidian_requester/qidian_session.py +202 -0
- novel_downloader/core/savers/__init__.py +20 -0
- novel_downloader/core/savers/base_saver.py +169 -0
- novel_downloader/core/savers/common_saver/__init__.py +13 -0
- novel_downloader/core/savers/common_saver/common_epub.py +232 -0
- novel_downloader/core/savers/common_saver/common_txt.py +176 -0
- novel_downloader/core/savers/common_saver/main_saver.py +86 -0
- novel_downloader/core/savers/epub_utils/__init__.py +27 -0
- novel_downloader/core/savers/epub_utils/css_builder.py +68 -0
- novel_downloader/core/savers/epub_utils/initializer.py +98 -0
- novel_downloader/core/savers/epub_utils/text_to_html.py +132 -0
- novel_downloader/core/savers/epub_utils/volume_intro.py +61 -0
- novel_downloader/core/savers/qidian_saver.py +22 -0
- novel_downloader/locales/en.json +91 -0
- novel_downloader/locales/zh.json +91 -0
- novel_downloader/resources/config/rules.toml +196 -0
- novel_downloader/resources/config/settings.yaml +73 -0
- novel_downloader/resources/css_styles/main.css +104 -0
- novel_downloader/resources/css_styles/volume-intro.css +56 -0
- novel_downloader/resources/images/volume_border.png +0 -0
- novel_downloader/resources/js_scripts/qidian_decrypt_node.js +82 -0
- novel_downloader/resources/json/replace_word_map.json +4 -0
- novel_downloader/resources/text/blacklist.txt +22 -0
- novel_downloader/utils/__init__.py +0 -0
- novel_downloader/utils/cache.py +24 -0
- novel_downloader/utils/constants.py +158 -0
- novel_downloader/utils/crypto_utils.py +144 -0
- novel_downloader/utils/file_utils/__init__.py +43 -0
- novel_downloader/utils/file_utils/io.py +252 -0
- novel_downloader/utils/file_utils/normalize.py +68 -0
- novel_downloader/utils/file_utils/sanitize.py +77 -0
- novel_downloader/utils/fontocr/__init__.py +23 -0
- novel_downloader/utils/fontocr/ocr_v1.py +304 -0
- novel_downloader/utils/fontocr/ocr_v2.py +658 -0
- novel_downloader/utils/hash_store.py +288 -0
- novel_downloader/utils/hash_utils.py +103 -0
- novel_downloader/utils/i18n.py +41 -0
- novel_downloader/utils/logger.py +104 -0
- novel_downloader/utils/model_loader.py +72 -0
- novel_downloader/utils/network.py +287 -0
- novel_downloader/utils/state.py +156 -0
- novel_downloader/utils/text_utils/__init__.py +27 -0
- novel_downloader/utils/text_utils/chapter_formatting.py +46 -0
- novel_downloader/utils/text_utils/diff_display.py +75 -0
- novel_downloader/utils/text_utils/font_mapping.py +31 -0
- novel_downloader/utils/text_utils/text_cleaning.py +57 -0
- novel_downloader/utils/time_utils/__init__.py +22 -0
- novel_downloader/utils/time_utils/datetime_utils.py +146 -0
- novel_downloader/utils/time_utils/sleep_utils.py +49 -0
- novel_downloader-1.1.0.dist-info/METADATA +157 -0
- novel_downloader-1.1.0.dist-info/RECORD +115 -0
- novel_downloader-1.1.0.dist-info/WHEEL +5 -0
- novel_downloader-1.1.0.dist-info/entry_points.txt +2 -0
- novel_downloader-1.1.0.dist-info/licenses/LICENSE +21 -0
- novel_downloader-1.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader
|
5
|
+
----------------
|
6
|
+
|
7
|
+
Core package for the Novel Downloader project.
|
8
|
+
"""
|
9
|
+
|
10
|
+
__version__ = "1.1.0"
|
11
|
+
|
12
|
+
__author__ = "Saudade Z"
|
13
|
+
__email__ = "saudadez217@gmail.com"
|
14
|
+
__license__ = "MIT"
|
@@ -0,0 +1,134 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader.cli.clean
|
5
|
+
-----------------------------
|
6
|
+
|
7
|
+
"""
|
8
|
+
|
9
|
+
import shutil
|
10
|
+
from pathlib import Path
|
11
|
+
from typing import List, Optional
|
12
|
+
|
13
|
+
import click
|
14
|
+
|
15
|
+
from novel_downloader.utils.constants import (
|
16
|
+
CONFIG_DIR,
|
17
|
+
DATA_DIR,
|
18
|
+
JS_SCRIPT_DIR,
|
19
|
+
LOGGER_DIR,
|
20
|
+
MODEL_CACHE_DIR,
|
21
|
+
REC_CHAR_MODEL_REPO,
|
22
|
+
STATE_DIR,
|
23
|
+
)
|
24
|
+
from novel_downloader.utils.i18n import t
|
25
|
+
|
26
|
+
|
27
|
+
def delete_path(p: Path) -> None:
|
28
|
+
if p.exists():
|
29
|
+
if p.is_file():
|
30
|
+
p.unlink()
|
31
|
+
else:
|
32
|
+
shutil.rmtree(p, ignore_errors=True)
|
33
|
+
click.echo(f"[clean] {t('clean_deleted')}: {p}")
|
34
|
+
else:
|
35
|
+
click.echo(f"[clean] {t('clean_not_found')}: {p}")
|
36
|
+
|
37
|
+
|
38
|
+
def clean_model_repo_cache(repo_id: Optional[str] = None, all: bool = False) -> bool:
|
39
|
+
"""
|
40
|
+
Delete Hugging Face cache for a specific repo.
|
41
|
+
"""
|
42
|
+
from huggingface_hub import scan_cache_dir
|
43
|
+
|
44
|
+
cache_info = scan_cache_dir()
|
45
|
+
|
46
|
+
if all:
|
47
|
+
targets = cache_info.repos
|
48
|
+
elif repo_id:
|
49
|
+
targets = [r for r in cache_info.repos if r.repo_id == repo_id]
|
50
|
+
else:
|
51
|
+
return False
|
52
|
+
|
53
|
+
strategy = cache_info.delete_revisions(
|
54
|
+
*[rev.commit_hash for r in targets for rev in r.revisions]
|
55
|
+
)
|
56
|
+
print(f"[clean] Will free {strategy.expected_freed_size_str}")
|
57
|
+
strategy.execute()
|
58
|
+
return True
|
59
|
+
|
60
|
+
|
61
|
+
@click.command(name="clean", help=t("help_clean")) # type: ignore
|
62
|
+
@click.option("--logs", is_flag=True, help=t("clean_logs")) # type: ignore
|
63
|
+
@click.option("--cache", is_flag=True, help=t("clean_cache")) # type: ignore
|
64
|
+
@click.option("--state", is_flag=True, help=t("clean_state")) # type: ignore
|
65
|
+
@click.option("--data", is_flag=True, help=t("clean_data")) # type: ignore
|
66
|
+
@click.option("--config", is_flag=True, help=t("clean_config")) # type: ignore
|
67
|
+
@click.option("--models", is_flag=True, help=t("clean_models")) # type: ignore
|
68
|
+
@click.option("--hf-cache", is_flag=True, help=t("clean_hf_cache")) # type: ignore
|
69
|
+
@click.option("--hf-cache-all", is_flag=True, help=t("clean_hf_cache_all")) # type: ignore
|
70
|
+
@click.option("--all", is_flag=True, help=t("clean_all")) # type: ignore
|
71
|
+
@click.option("--yes", is_flag=True, help=t("clean_yes")) # type: ignore
|
72
|
+
def clean_cli(
|
73
|
+
logs: bool,
|
74
|
+
cache: bool,
|
75
|
+
state: bool,
|
76
|
+
data: bool,
|
77
|
+
config: bool,
|
78
|
+
models: bool,
|
79
|
+
hf_cache: bool,
|
80
|
+
hf_cache_all: bool,
|
81
|
+
all: bool,
|
82
|
+
yes: bool,
|
83
|
+
) -> None:
|
84
|
+
targets: List[Path] = []
|
85
|
+
|
86
|
+
if all:
|
87
|
+
if not yes:
|
88
|
+
confirm = click.prompt(t("clean_confirm"), default="n")
|
89
|
+
if confirm.lower() != "y":
|
90
|
+
click.echo(t("clean_cancelled"))
|
91
|
+
return
|
92
|
+
targets = [
|
93
|
+
LOGGER_DIR,
|
94
|
+
JS_SCRIPT_DIR,
|
95
|
+
STATE_DIR,
|
96
|
+
DATA_DIR,
|
97
|
+
CONFIG_DIR,
|
98
|
+
MODEL_CACHE_DIR,
|
99
|
+
]
|
100
|
+
else:
|
101
|
+
if logs:
|
102
|
+
targets.append(LOGGER_DIR)
|
103
|
+
if cache:
|
104
|
+
targets.append(JS_SCRIPT_DIR)
|
105
|
+
if state:
|
106
|
+
targets.append(STATE_DIR)
|
107
|
+
if data:
|
108
|
+
targets.append(DATA_DIR)
|
109
|
+
if config:
|
110
|
+
targets.append(CONFIG_DIR)
|
111
|
+
if models:
|
112
|
+
targets.append(MODEL_CACHE_DIR)
|
113
|
+
|
114
|
+
if hf_cache_all:
|
115
|
+
try:
|
116
|
+
if clean_model_repo_cache(all=True):
|
117
|
+
click.echo(t("clean_hf_cache_all_done"))
|
118
|
+
except Exception as e:
|
119
|
+
click.echo(t("clean_hf_cache_all_fail", err=e))
|
120
|
+
elif hf_cache:
|
121
|
+
try:
|
122
|
+
if clean_model_repo_cache(REC_CHAR_MODEL_REPO):
|
123
|
+
click.echo(t("clean_hf_model_done", repo=REC_CHAR_MODEL_REPO))
|
124
|
+
else:
|
125
|
+
click.echo(t("clean_hf_model_not_found", repo=REC_CHAR_MODEL_REPO))
|
126
|
+
except Exception as e:
|
127
|
+
click.echo(t("clean_hf_model_fail", err=e))
|
128
|
+
|
129
|
+
if not targets and not hf_cache and not hf_cache_all:
|
130
|
+
click.echo(t("clean_nothing"))
|
131
|
+
return
|
132
|
+
|
133
|
+
for path in targets:
|
134
|
+
delete_path(path)
|
@@ -0,0 +1,132 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader.cli.download
|
5
|
+
-----------------------------
|
6
|
+
|
7
|
+
Download full novels by book IDs
|
8
|
+
(supports config files, site switching, and localization prompts).
|
9
|
+
"""
|
10
|
+
|
11
|
+
from typing import List
|
12
|
+
|
13
|
+
import click
|
14
|
+
from click import Context
|
15
|
+
|
16
|
+
from novel_downloader.config import ConfigAdapter, load_config
|
17
|
+
from novel_downloader.core.factory import (
|
18
|
+
get_async_downloader,
|
19
|
+
get_async_requester,
|
20
|
+
# get_downloader,
|
21
|
+
get_parser,
|
22
|
+
# get_requester,
|
23
|
+
get_saver,
|
24
|
+
get_sync_downloader,
|
25
|
+
get_sync_requester,
|
26
|
+
)
|
27
|
+
from novel_downloader.utils.i18n import t
|
28
|
+
from novel_downloader.utils.logger import setup_logging
|
29
|
+
|
30
|
+
|
31
|
+
@click.command(
|
32
|
+
name="download",
|
33
|
+
help=t("download_help"),
|
34
|
+
short_help=t("download_short_help"),
|
35
|
+
) # type: ignore
|
36
|
+
@click.argument("book_ids", nargs=-1) # type: ignore
|
37
|
+
@click.option(
|
38
|
+
"--site",
|
39
|
+
default="qidian",
|
40
|
+
show_default=True,
|
41
|
+
help=t("download_option_site", default="qidian"),
|
42
|
+
) # type: ignore
|
43
|
+
@click.pass_context # type: ignore
|
44
|
+
def download_cli(ctx: Context, book_ids: List[str], site: str) -> None:
|
45
|
+
"""Download full novels by book IDs."""
|
46
|
+
config_path = ctx.obj.get("config_path")
|
47
|
+
|
48
|
+
click.echo(t("download_using_config", path=config_path))
|
49
|
+
click.echo(t("download_site_info", site=site))
|
50
|
+
|
51
|
+
config_data = load_config(config_path)
|
52
|
+
adapter = ConfigAdapter(config=config_data, site=site)
|
53
|
+
|
54
|
+
# Retrieve each sub-component's configuration from the adapter
|
55
|
+
requester_cfg = adapter.get_requester_config()
|
56
|
+
downloader_cfg = adapter.get_downloader_config()
|
57
|
+
parser_cfg = adapter.get_parser_config()
|
58
|
+
saver_cfg = adapter.get_saver_config()
|
59
|
+
|
60
|
+
# If no book_ids provided on the command line, try to load them from config
|
61
|
+
if not book_ids:
|
62
|
+
try:
|
63
|
+
book_ids = adapter.get_book_ids()
|
64
|
+
except Exception as e:
|
65
|
+
click.echo(t("download_fail_get_ids", err=e))
|
66
|
+
return
|
67
|
+
|
68
|
+
# Filter out placeholder/example IDs
|
69
|
+
invalid_ids = {"0000000000"}
|
70
|
+
valid_book_ids = [bid for bid in book_ids if bid not in invalid_ids]
|
71
|
+
|
72
|
+
if not book_ids:
|
73
|
+
click.echo(t("download_no_ids"))
|
74
|
+
return
|
75
|
+
|
76
|
+
if not valid_book_ids:
|
77
|
+
click.echo(t("download_only_example", example="0000000000"))
|
78
|
+
click.echo(t("download_edit_config"))
|
79
|
+
return
|
80
|
+
|
81
|
+
# Initialize the requester, parser, saver, and downloader components
|
82
|
+
if downloader_cfg.mode == "async":
|
83
|
+
import asyncio
|
84
|
+
|
85
|
+
async_requester = get_async_requester(site, requester_cfg)
|
86
|
+
async_parser = get_parser(site, parser_cfg)
|
87
|
+
async_saver = get_saver(site, saver_cfg)
|
88
|
+
setup_logging()
|
89
|
+
async_downloader = get_async_downloader(
|
90
|
+
requester=async_requester,
|
91
|
+
parser=async_parser,
|
92
|
+
saver=async_saver,
|
93
|
+
site=site,
|
94
|
+
config=downloader_cfg,
|
95
|
+
)
|
96
|
+
|
97
|
+
async def async_download_all() -> None:
|
98
|
+
prepare = getattr(async_downloader, "prepare", None)
|
99
|
+
if prepare and asyncio.iscoroutinefunction(prepare):
|
100
|
+
await prepare()
|
101
|
+
|
102
|
+
for book_id in valid_book_ids:
|
103
|
+
click.echo(t("download_downloading", book_id=book_id, site=site))
|
104
|
+
await async_downloader.download_one(book_id)
|
105
|
+
|
106
|
+
if requester_cfg.auto_close:
|
107
|
+
input(t("download_prompt_parse"))
|
108
|
+
await async_requester.shutdown()
|
109
|
+
|
110
|
+
asyncio.run(async_download_all())
|
111
|
+
else:
|
112
|
+
sync_requester = get_sync_requester(site, requester_cfg)
|
113
|
+
sync_parser = get_parser(site, parser_cfg)
|
114
|
+
sync_saver = get_saver(site, saver_cfg)
|
115
|
+
setup_logging()
|
116
|
+
sync_downloader = get_sync_downloader(
|
117
|
+
requester=sync_requester,
|
118
|
+
parser=sync_parser,
|
119
|
+
saver=sync_saver,
|
120
|
+
site=site,
|
121
|
+
config=downloader_cfg,
|
122
|
+
)
|
123
|
+
|
124
|
+
for book_id in book_ids:
|
125
|
+
click.echo(t("download_downloading", book_id=book_id, site=site))
|
126
|
+
sync_downloader.download_one(book_id)
|
127
|
+
|
128
|
+
if requester_cfg.auto_close:
|
129
|
+
input(t("download_prompt_parse"))
|
130
|
+
sync_requester.shutdown()
|
131
|
+
|
132
|
+
return
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader.cli.interactive
|
5
|
+
--------------------------------
|
6
|
+
|
7
|
+
Interactive CLI mode for novel_downloader.
|
8
|
+
Supports multilingual prompt, input validation, and quit control.
|
9
|
+
"""
|
10
|
+
|
11
|
+
import click
|
12
|
+
from click import Context
|
13
|
+
|
14
|
+
from novel_downloader.cli.download import download_cli
|
15
|
+
from novel_downloader.utils.i18n import t
|
16
|
+
|
17
|
+
|
18
|
+
@click.group( # type: ignore
|
19
|
+
name="interactive", help=t("interactive_help"), invoke_without_command=True
|
20
|
+
)
|
21
|
+
@click.pass_context # type: ignore
|
22
|
+
def interactive_cli(ctx: Context) -> None:
|
23
|
+
"""Interactive mode for novel selection and preview."""
|
24
|
+
if ctx.invoked_subcommand is None:
|
25
|
+
click.echo(t("interactive_no_sub"))
|
26
|
+
|
27
|
+
options = [
|
28
|
+
t("interactive_option_download"),
|
29
|
+
t("interactive_option_browse"),
|
30
|
+
t("interactive_option_preview"),
|
31
|
+
t("interactive_option_exit"),
|
32
|
+
]
|
33
|
+
for idx, opt in enumerate(options, 1):
|
34
|
+
click.echo(f"{idx}. {opt}")
|
35
|
+
|
36
|
+
choice = click.prompt(t("interactive_prompt_choice"), type=int)
|
37
|
+
|
38
|
+
if choice == 1:
|
39
|
+
default_site = "qidian"
|
40
|
+
site: str = click.prompt(
|
41
|
+
t("download_option_site", default=default_site),
|
42
|
+
default_site,
|
43
|
+
)
|
44
|
+
ids_input: str = click.prompt(t("interactive_prompt_book_ids"))
|
45
|
+
book_ids = ids_input.strip().split()
|
46
|
+
ctx.invoke(download_cli, book_ids=book_ids, site=site)
|
47
|
+
elif choice == 2:
|
48
|
+
ctx.invoke(browse)
|
49
|
+
elif choice == 3:
|
50
|
+
ctx.invoke(preview)
|
51
|
+
else:
|
52
|
+
click.echo(t("interactive_exit"))
|
53
|
+
return
|
54
|
+
|
55
|
+
|
56
|
+
@interactive_cli.command(help=t("interactive_browse_help")) # type: ignore
|
57
|
+
@click.pass_context # type: ignore
|
58
|
+
def browse(ctx: Context) -> None:
|
59
|
+
"""Browse available novels interactively."""
|
60
|
+
click.echo(t("interactive_browse_start"))
|
61
|
+
|
62
|
+
|
63
|
+
@interactive_cli.command(help=t("interactive_preview_help")) # type: ignore
|
64
|
+
@click.pass_context # type: ignore
|
65
|
+
def preview(ctx: Context) -> None:
|
66
|
+
"""Preview chapters before downloading."""
|
67
|
+
click.echo(t("interactive_preview_start"))
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader.cli.main
|
5
|
+
--------------------------
|
6
|
+
|
7
|
+
Unified CLI entry point. Parses arguments and delegates to parser or interactive.
|
8
|
+
"""
|
9
|
+
|
10
|
+
from typing import Optional
|
11
|
+
|
12
|
+
import click
|
13
|
+
from click import Context
|
14
|
+
|
15
|
+
from novel_downloader.cli import clean, download, interactive, settings
|
16
|
+
from novel_downloader.utils.i18n import t
|
17
|
+
|
18
|
+
|
19
|
+
@click.group(help=t("cli_help"), invoke_without_command=True) # type: ignore
|
20
|
+
@click.option(
|
21
|
+
"--config",
|
22
|
+
type=click.Path(exists=True, dir_okay=False, resolve_path=True),
|
23
|
+
default=None,
|
24
|
+
help=t("help_config"),
|
25
|
+
) # type: ignore
|
26
|
+
@click.pass_context # type: ignore
|
27
|
+
def cli_main(ctx: Context, config: Optional[str]) -> None:
|
28
|
+
"""Novel Downloader CLI."""
|
29
|
+
ctx.ensure_object(dict)
|
30
|
+
ctx.obj["config_path"] = config
|
31
|
+
|
32
|
+
if ctx.invoked_subcommand is None:
|
33
|
+
click.echo(t("main_no_command"))
|
34
|
+
ctx.invoke(interactive.interactive_cli)
|
35
|
+
|
36
|
+
|
37
|
+
# Register subcommands
|
38
|
+
cli_main.add_command(clean.clean_cli)
|
39
|
+
cli_main.add_command(download.download_cli)
|
40
|
+
cli_main.add_command(interactive.interactive_cli)
|
41
|
+
cli_main.add_command(settings.settings_cli)
|
42
|
+
|
43
|
+
|
44
|
+
if __name__ == "__main__":
|
45
|
+
cli_main()
|
@@ -0,0 +1,177 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader.cli.settings
|
5
|
+
-----------------------------
|
6
|
+
|
7
|
+
Commands to configure novel downloader settings.
|
8
|
+
"""
|
9
|
+
|
10
|
+
import shutil
|
11
|
+
from importlib.resources import as_file
|
12
|
+
from pathlib import Path
|
13
|
+
from typing import Optional
|
14
|
+
|
15
|
+
import click
|
16
|
+
from click import Context
|
17
|
+
|
18
|
+
from novel_downloader.config import save_config_file, save_rules_as_json
|
19
|
+
from novel_downloader.utils.constants import DEFAULT_SETTINGS_PATHS
|
20
|
+
from novel_downloader.utils.i18n import t
|
21
|
+
from novel_downloader.utils.logger import setup_logging
|
22
|
+
from novel_downloader.utils.state import state_mgr
|
23
|
+
|
24
|
+
|
25
|
+
@click.group(name="settings", help=t("settings_help")) # type: ignore
|
26
|
+
def settings_cli() -> None:
|
27
|
+
"""Configure downloader settings."""
|
28
|
+
setup_logging()
|
29
|
+
pass
|
30
|
+
|
31
|
+
|
32
|
+
@settings_cli.command(name="init", help=t("settings_init_help")) # type: ignore
|
33
|
+
@click.option("--force", is_flag=True, help=t("settings_init_force_help")) # type: ignore
|
34
|
+
def init_settings(force: bool) -> None:
|
35
|
+
"""Initialize default settings and rules in the current directory."""
|
36
|
+
cwd = Path.cwd()
|
37
|
+
|
38
|
+
for resource in DEFAULT_SETTINGS_PATHS:
|
39
|
+
target_path = cwd / resource.name
|
40
|
+
should_copy = True
|
41
|
+
|
42
|
+
if target_path.exists():
|
43
|
+
if force:
|
44
|
+
should_copy = True
|
45
|
+
click.echo(t("settings_init_overwrite", filename=resource.name))
|
46
|
+
else:
|
47
|
+
click.echo(t("settings_init_exists", filename=resource.name))
|
48
|
+
should_copy = click.confirm(
|
49
|
+
t("settings_init_confirm_overwrite", filename=resource.name),
|
50
|
+
default=False,
|
51
|
+
)
|
52
|
+
|
53
|
+
if not should_copy:
|
54
|
+
click.echo(t("settings_init_skip", filename=resource.name))
|
55
|
+
continue
|
56
|
+
|
57
|
+
try:
|
58
|
+
with as_file(resource) as actual_path:
|
59
|
+
shutil.copy(actual_path, target_path)
|
60
|
+
click.echo(t("settings_init_copy", filename=resource.name))
|
61
|
+
except Exception as e:
|
62
|
+
raise click.ClickException(
|
63
|
+
t("settings_init_error", filename=resource.name, err=e)
|
64
|
+
)
|
65
|
+
|
66
|
+
|
67
|
+
@settings_cli.command(name="set-lang", help=t("settings_set_lang_help")) # type: ignore
|
68
|
+
@click.argument("lang", type=click.Choice(["zh", "en"])) # type: ignore
|
69
|
+
@click.pass_context # type: ignore
|
70
|
+
def set_language(ctx: Context, lang: str) -> None:
|
71
|
+
"""Switch language between Chinese and English."""
|
72
|
+
state_mgr.set_language(lang)
|
73
|
+
click.echo(t("settings_set_lang", lang=lang))
|
74
|
+
|
75
|
+
|
76
|
+
@settings_cli.command(name="set-config", help=t("settings_set_config_help")) # type: ignore
|
77
|
+
@click.argument("path", type=click.Path(exists=True, dir_okay=False, resolve_path=True)) # type: ignore
|
78
|
+
def set_config(path: str) -> None:
|
79
|
+
"""Set and save a custom YAML configuration file."""
|
80
|
+
try:
|
81
|
+
save_config_file(path)
|
82
|
+
click.echo(t("settings_set_config", path=path))
|
83
|
+
except Exception as e:
|
84
|
+
raise click.ClickException(t("settings_set_config_fail", err=e))
|
85
|
+
|
86
|
+
|
87
|
+
@settings_cli.command(name="update-rules", help=t("settings_update_rules_help")) # type: ignore
|
88
|
+
@click.argument("path", type=click.Path(exists=True, dir_okay=False, resolve_path=True)) # type: ignore
|
89
|
+
def update_rules(path: str) -> None:
|
90
|
+
"""Update site rules from a TOML/YAML/JSON file."""
|
91
|
+
try:
|
92
|
+
save_rules_as_json(path)
|
93
|
+
click.echo(t("settings_update_rules", path=path))
|
94
|
+
except Exception as e:
|
95
|
+
raise click.ClickException(t("settings_update_rules_fail", err=e))
|
96
|
+
|
97
|
+
|
98
|
+
@settings_cli.command(
|
99
|
+
name="set-cookies", help=t("settings_set_cookies_help")
|
100
|
+
) # type: ignore
|
101
|
+
@click.argument("site", required=False) # type: ignore
|
102
|
+
@click.argument("cookies", required=False) # type: ignore
|
103
|
+
@click.pass_context # type: ignore
|
104
|
+
def set_cookies(ctx: Context, site: str, cookies: str) -> None:
|
105
|
+
"""
|
106
|
+
Set or update cookies for a site.
|
107
|
+
|
108
|
+
:param site: Site identifier (e.g. 'qidian', 'bqg').
|
109
|
+
If omitted, you will be prompted to enter it.
|
110
|
+
:param cookies: Cookie payload. Can be a JSON string (e.g. '{"k":"v"}')
|
111
|
+
or a browser-style string 'k1=v1; k2=v2'.
|
112
|
+
If omitted, you will be prompted to enter it.
|
113
|
+
"""
|
114
|
+
if not site:
|
115
|
+
site = click.prompt(t("settings_set_cookies_prompt_site"), type=str)
|
116
|
+
if not cookies:
|
117
|
+
cookies = click.prompt(t("settings_set_cookies_prompt_payload"), type=str)
|
118
|
+
|
119
|
+
try:
|
120
|
+
state_mgr.set_cookies(site, cookies)
|
121
|
+
click.echo(t("settings_set_cookies_success", site=site))
|
122
|
+
except Exception as e:
|
123
|
+
raise click.ClickException(t("settings_set_cookies_fail", err=e))
|
124
|
+
|
125
|
+
|
126
|
+
@settings_cli.command(name="add-hash", help=t("settings_add_hash_help")) # type: ignore
|
127
|
+
@click.option(
|
128
|
+
"--path",
|
129
|
+
type=click.Path(exists=True, dir_okay=False),
|
130
|
+
help=t("settings_add_hash_path_help"),
|
131
|
+
) # type: ignore
|
132
|
+
def add_image_hashes(path: Optional[str]) -> None:
|
133
|
+
"""
|
134
|
+
Add image hashes to internal store for matching.
|
135
|
+
Can be run in interactive mode (no --path), or with a JSON file.
|
136
|
+
"""
|
137
|
+
from novel_downloader.utils.hash_store import img_hash_store
|
138
|
+
|
139
|
+
if path:
|
140
|
+
try:
|
141
|
+
img_hash_store.add_from_map(path)
|
142
|
+
img_hash_store.save()
|
143
|
+
click.echo(t("settings_add_hash_loaded", path=path))
|
144
|
+
except Exception as e:
|
145
|
+
raise click.ClickException(t("settings_add_hash_load_fail", err=str(e)))
|
146
|
+
else:
|
147
|
+
click.echo(t("settings_add_hash_prompt_tip"))
|
148
|
+
while True:
|
149
|
+
img_path = click.prompt(
|
150
|
+
t("settings_add_hash_prompt_img"),
|
151
|
+
type=str,
|
152
|
+
default="",
|
153
|
+
show_default=False,
|
154
|
+
).strip()
|
155
|
+
if not img_path or img_path.lower() in {"exit", "quit"}:
|
156
|
+
break
|
157
|
+
if not Path(img_path).exists():
|
158
|
+
click.echo(t("settings_add_hash_path_invalid"))
|
159
|
+
continue
|
160
|
+
|
161
|
+
label = click.prompt(
|
162
|
+
t("settings_add_hash_prompt_label"),
|
163
|
+
type=str,
|
164
|
+
default="",
|
165
|
+
show_default=False,
|
166
|
+
).strip()
|
167
|
+
if not label or label.lower() in {"exit", "quit"}:
|
168
|
+
break
|
169
|
+
|
170
|
+
try:
|
171
|
+
img_hash_store.add_image(img_path, label)
|
172
|
+
click.echo(t("settings_add_hash_added", img=img_path, label=label))
|
173
|
+
except Exception as e:
|
174
|
+
click.echo(t("settings_add_hash_failed", err=str(e)))
|
175
|
+
|
176
|
+
img_hash_store.save()
|
177
|
+
click.echo(t("settings_add_hash_saved"))
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
"""
|
4
|
+
novel_downloader.config
|
5
|
+
------------------------
|
6
|
+
|
7
|
+
Unified interface for loading and adapting configuration files.
|
8
|
+
|
9
|
+
This module provides:
|
10
|
+
- load_config: loads YAML config from file path with fallback support
|
11
|
+
- ConfigAdapter: maps raw config + site name to structured config models
|
12
|
+
- Configuration dataclasses: RequesterConfig, DownloaderConfig, etc.
|
13
|
+
"""
|
14
|
+
|
15
|
+
from .adapter import ConfigAdapter
|
16
|
+
from .loader import load_config, save_config_file
|
17
|
+
from .models import (
|
18
|
+
BookInfoRules,
|
19
|
+
DownloaderConfig,
|
20
|
+
FieldRules,
|
21
|
+
ParserConfig,
|
22
|
+
RequesterConfig,
|
23
|
+
RuleStep,
|
24
|
+
SaverConfig,
|
25
|
+
SiteProfile,
|
26
|
+
SiteRules,
|
27
|
+
SiteRulesDict,
|
28
|
+
VolumesRules,
|
29
|
+
)
|
30
|
+
from .site_rules import (
|
31
|
+
load_site_rules,
|
32
|
+
save_rules_as_json,
|
33
|
+
)
|
34
|
+
|
35
|
+
__all__ = [
|
36
|
+
"load_config",
|
37
|
+
"save_config_file",
|
38
|
+
"ConfigAdapter",
|
39
|
+
"RequesterConfig",
|
40
|
+
"DownloaderConfig",
|
41
|
+
"ParserConfig",
|
42
|
+
"SaverConfig",
|
43
|
+
"FieldRules",
|
44
|
+
"RuleStep",
|
45
|
+
"SiteProfile",
|
46
|
+
"SiteRules",
|
47
|
+
"SiteRulesDict",
|
48
|
+
"VolumesRules",
|
49
|
+
"BookInfoRules",
|
50
|
+
"load_site_rules",
|
51
|
+
"save_rules_as_json",
|
52
|
+
]
|