forked from DobyTang/LazyLibrarian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epubandmobi.py
executable file
·46 lines (39 loc) · 1.9 KB
/
epubandmobi.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
from __future__ import print_function
import sys
import os
import subprocess
converter = "ebook-convert" # if not in your "path", put the full pathname here
calibredb = "calibredb" # and here too
books_parent_dir = '/media/eBooks' # change to your dir
for root, subFolders, files in os.walk(books_parent_dir):
for name in files:
for source, dest in [['.mobi', '.epub'], ['.epub', '.mobi']]:
if name.endswith(source):
source_file = (os.path.join(root, name))
dest_file = source_file[:-len(source)] + dest
if not os.path.exists(dest_file):
params = [converter, source_file, dest_file]
if dest == '.mobi':
params.extend(['--output-profile', 'kindle'])
try:
print("Creating %s for %s" % (dest, name))
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
try:
calibreid = root.rsplit('(', 1)[1].split(')')[0]
if not calibreid.isdigit():
calibreid = ''
except IndexError:
calibreid = ''
if calibreid:
librarydir = os.path.dirname(os.path.dirname(root))
params = [calibredb, 'add_format', '--with-library=' + librarydir, calibreid, dest_file ]
try:
print("Telling calibre about new %s" % dest)
res = subprocess.check_output(params, stderr=subprocess.STDOUT)
except Exception as e:
print("%s\n" % e)
except Exception as e:
print("%s\n" % e)