[doc] 增加单独执行 dedrm 的说明

1. 需单独 pip 安装的模块
2. 增加仅 azw 或 epub 的命令行参数选项
This commit is contained in:
wushuzh
2024-05-27 11:25:01 +08:00
parent 31c094cbc6
commit c91accdcaf
2 changed files with 32 additions and 5 deletions

View File

@@ -226,7 +226,14 @@ python3 kindle.py --cn --cookie ${cookie} ${csrfToken}
- 如果有很多同名 pdoc 或 book 可以使用 `--resolve_duplicate_names` 解决同名冲突 - 如果有很多同名 pdoc 或 book 可以使用 `--resolve_duplicate_names` 解决同名冲突
- error log 记录在 .error_books.log 中 - error log 记录在 .error_books.log 中
- 支持生成最近读完书的 README `--readme` 生成的文件在 `my_kindle_stats.md` 中 - 支持生成最近读完书的 README `--readme` 生成的文件在 `my_kindle_stats.md` 中
- 支持 mobi 类型的文件直接 dedrm `--dedrm` 生成的文件在 `DEDRMS` 里 - 支持 mobi 类型的文件直接 dedrm `--dedrm` 生成的文件在 `DEDRMS` 里,默认同时输出 azw 和 epub 两种格式
```
pip install mobi
# 其中后 3 个参数,仅在必要时指定
python3 dedrm.py 源目录 目标目录 密钥 输出格式
```
## Note ## Note

View File

@@ -17,7 +17,7 @@ def read_key_from_file(directory):
print(f"No key file found in {directory}. Proceeding without a key.") print(f"No key file found in {directory}. Proceeding without a key.")
return None return None
def process_azw_files(source_directory, output_directory='DeDRMed', device_serial_number=None): def process_azw_files(source_directory, output_directory='DeDRMed', device_serial_number=None, dump_azw=True, dump_epub=True):
if device_serial_number is None: if device_serial_number is None:
device_serial_number = read_key_from_file(source_directory) device_serial_number = read_key_from_file(source_directory)
if device_serial_number is None: if device_serial_number is None:
@@ -33,6 +33,7 @@ def process_azw_files(source_directory, output_directory='DeDRMed', device_seria
for filename in os.listdir(source_directory): for filename in os.listdir(source_directory):
if filename.endswith((".azw", ".azw3")): if filename.endswith((".azw", ".azw3")):
print(f"Processed {filename}: ", end="")
path = os.path.join(source_directory, filename) path = os.path.join(source_directory, filename)
try: try:
out = path # Original file path out = path # Original file path
@@ -47,6 +48,11 @@ def process_azw_files(source_directory, output_directory='DeDRMed', device_seria
mb.make_drm_file(totalpids, out_dedrm) mb.make_drm_file(totalpids, out_dedrm)
time.sleep(1) time.sleep(1)
if dump_azw:
print(f"DeDRMed file saved in {azw_output_dir} ")
if not dump_epub: continue
# Extract and determine if output is EPUB or HTML # Extract and determine if output is EPUB or HTML
epub_dir, output_file = extract(out_dedrm) epub_dir, output_file = extract(out_dedrm)
output_extension = os.path.splitext(output_file)[1].lower() output_extension = os.path.splitext(output_file)[1].lower()
@@ -62,17 +68,31 @@ def process_azw_files(source_directory, output_directory='DeDRMed', device_seria
shutil.copy2(output_file, final_output_path) shutil.copy2(output_file, final_output_path)
shutil.rmtree(epub_dir) # Clean up extraction directory shutil.rmtree(epub_dir) # Clean up extraction directory
print(f"Processed {filename}: DeDRMed file saved in {azw_output_dir}, EPUB/HTML saved as {final_output_path}.") print(f"EPUB/HTML saved as {final_output_path}.")
if not dump_azw:
os.remove(out_dedrm)
except Exception as e: except Exception as e:
print(f"Error processing {filename}: {e}") print(f"Error processing {filename}: {e}")
if __name__ == "__main__": if __name__ == "__main__":
cmd_usage = "Usage: script.py <source_directory> [output_directory] [device_serial_number] [specific_filetype(azw/epub)]"
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: script.py <source_directory> [output_directory] [device_serial_number]") print(cmd_usage)
sys.exit(1) sys.exit(1)
source_directory = sys.argv[1] source_directory = sys.argv[1]
output_directory = sys.argv[2] if len(sys.argv) > 2 else 'DeDRMed' output_directory = sys.argv[2] if len(sys.argv) > 2 else 'DeDRMed'
device_serial_number = sys.argv[3] if len(sys.argv) > 3 else None device_serial_number = sys.argv[3] if len(sys.argv) > 3 else None
process_azw_files(source_directory, output_directory, device_serial_number) dump_azw = True
dump_epub = True
if len(sys.argv) > 4:
if sys.argv[4] == 'azw':
dump_epub = False
elif sys.argv[4] == 'epub':
dump_azw = False
else:
print(cmd_usage)
sys.exit(1)
process_azw_files(source_directory, output_directory, device_serial_number, dump_azw, dump_epub)