fromozu commited on
Commit
026edb5
·
verified ·
1 Parent(s): 26b6417

Upload hf_backend/ebook_convert.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. hf_backend/ebook_convert.py +51 -0
hf_backend/ebook_convert.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import subprocess
5
+ from pathlib import Path
6
+
7
+
8
+ def normalize_epub_for_translation(config, source_path: Path, work_dir: Path) -> Path:
9
+ convert_dir = work_dir / "converted"
10
+ convert_dir.mkdir(parents=True, exist_ok=True)
11
+
12
+ mobi_path = convert_dir / f"{source_path.stem}.mobi"
13
+ normalized_epub = convert_dir / source_path.name
14
+ env = os.environ.copy()
15
+ env["PYTHONNOUSERSITE"] = "1"
16
+
17
+ epub_to_mobi = subprocess.run(
18
+ [config.ebook_convert_bin, str(source_path), str(mobi_path)],
19
+ check=False,
20
+ capture_output=True,
21
+ text=True,
22
+ encoding="utf-8",
23
+ errors="replace",
24
+ env=env,
25
+ )
26
+ if epub_to_mobi.returncode != 0 or not mobi_path.exists():
27
+ output = "\n".join(
28
+ part for part in [epub_to_mobi.stdout.strip(), epub_to_mobi.stderr.strip()] if part
29
+ )
30
+ raise RuntimeError(
31
+ f"ebook-convert EPUB->MOBI failed for {source_path.name}\n{output[-3000:]}"
32
+ )
33
+
34
+ mobi_to_epub = subprocess.run(
35
+ [config.ebook_convert_bin, str(mobi_path), str(normalized_epub)],
36
+ check=False,
37
+ capture_output=True,
38
+ text=True,
39
+ encoding="utf-8",
40
+ errors="replace",
41
+ env=env,
42
+ )
43
+ if mobi_to_epub.returncode != 0 or not normalized_epub.exists():
44
+ output = "\n".join(
45
+ part for part in [mobi_to_epub.stdout.strip(), mobi_to_epub.stderr.strip()] if part
46
+ )
47
+ raise RuntimeError(
48
+ f"ebook-convert MOBI->EPUB failed for {source_path.name}\n{output[-3000:]}"
49
+ )
50
+
51
+ return normalized_epub