forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmirror2kobo.py
More file actions
executable file
·67 lines (54 loc) · 1.91 KB
/
mirror2kobo.py
File metadata and controls
executable file
·67 lines (54 loc) · 1.91 KB
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
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# Convert a hierarchy of epub ebooks in a source directory
# to a kepub hierarchy in a destination directory
# that can be copied to a Kobo e-reader,
# preserving directory structure and keeping creation and modification dates
# unchanged.
import koboize
import sys, os
import shutil
try:
srcdir, dstdir = sys.argv[1], sys.argv[2]
except:
print("Usage: %s srcdir dstdir" % os.path.basename(sys.argv[0]))
sys.exit(1)
for root, dirs, files in os.walk(srcdir):
# Get part of root that's relative to srcdir.
if not root.startswith(srcdir):
print(f"root '{root}' doesn't start with srcdir '{srcdir}'!",
file=sys.stderr)
sys.exit(1)
relroot = root[len(srcdir):]
while relroot.startswith('/'):
relroot = relroot[1:]
dstroot = os.path.join(dstdir, relroot)
try:
os.makedirs(dstroot)
print("mkdir:", dstroot)
except FileExistsError:
print(dstroot, "already exists")
pass
for f in files:
origpath = os.path.join(root, f)
flower= f.lower()
if not flower.endswith(".epub"):
print("Skipping", origpath, ": not epub")
continue
if flower.endswith(".kepub.epub"):
dstpath = os.path.join(dstroot, f)
if os.path.exists(dstpath):
print(dstpath, "already exists")
else:
shutil.copy2(origpath, dstpath)
print("Copied", dstpath)
continue
try:
newfilename = f[:-5] + ".kepub.epub"
dstpath = os.path.join(dstroot, newfilename)
if os.path.exists(dstpath):
print(dstpath, "already exists")
else:
koboize.convert_file(origpath, dstroot)
shutil.copystat(origpath, dstpath)
except Exception as e:
print("Couldn't convert", origpath, ":", e, file=sys.stderr)