-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcalibre.py
More file actions
40 lines (31 loc) · 1.03 KB
/
calibre.py
File metadata and controls
40 lines (31 loc) · 1.03 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
# -*- coding: utf-8 -*-
import subprocess
from .logging import log
COMMAND = "ebook-meta"
class EbookMetaException(Exception):
pass
def get_version():
try:
ebook_meta = subprocess.Popen(
[COMMAND, '--version'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return ebook_meta.communicate()[0].decode('utf8')
except OSError:
raise EbookMetaException(
"Could not find {}, please ensure it is installed (via Calibre)."
.format(COMMAND))
def read_metadata(path):
version = get_version()
log.debug('Found ebook-meta version: %s' % version)
log.info("Trying to read eBook metadata...")
output = subprocess.check_output(
'{} "{}"'.format(COMMAND, path), shell=True)
result = {}
for row in output.decode('utf8').split('\n'):
if ': ' in row:
try:
key, value = row.split(': ')
result[key.strip(' .')] = value.strip()
except ValueError:
pass
return result