-
Notifications
You must be signed in to change notification settings - Fork 6
/
output_fmt.py
executable file
·100 lines (85 loc) · 3.49 KB
/
output_fmt.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
##################################################################
# OUTPUT GENERATING FUNCTIONS FOR FEEDME
# Define functions for each output format you need to support.
#
def run_conversion_cmd(appargs):
if verbose:
cmd = " ".join(appargs)
print("Running:", cmd, file=sys.stderr)
sys.stdout.flush()
retval = os.spawnvp(os.P_WAIT, appargs[0], appargs)
#retval = os.system(cmd)
if retval != 0:
raise OSError(retval, "Couldn't run: " + ' '.join(appargs))
#
# Generate a Plucker file
#
def make_plucker_file(indexfile, feedname, levels, ascii):
day = time.strftime("%a")
docname = day + " " + feedname
cleanfilename = day + "_" + feedname.replace(" ", "_")
# Make sure the plucker directory exists:
pluckerdir = os.path.join(expanduser("~/.plucker"), "feedme")
if not os.path.exists(pluckerdir):
os.makedirs(pluckerdir)
# Run plucker. This should eventually be configurable --
# but how, with arguments like these?
# Plucker mysteriously creates unbeamable files if the
# document name has a colons in it.
# So use the less pretty but safer underscored docname.
#docname = cleanfilename
appargs = [ "plucker-build", "-N", docname,
"-f", os.path.join("feedme", cleanfilename),
"--stayonhost", "--noimages",
"--maxdepth", str(levels),
"--zlib-compression", "--beamable",
"-H", "file://" + indexfile ]
if not ascii:
appargs.append("--charset=utf-8")
run_conversion_cmd(appargs)
#
# http://calibre-ebook.com/user_manual/conversion.html
#
def make_calibre_file(indexfile, feedname, extension, levels, ascii,
author, flags):
day = time.strftime("%a")
# Prepend daynum to the filename because fbreader can only sort by filename
#daynum = time.strftime("%w")
cleanfilename = day + "_" + feedname.replace(" ", "_")
outdir = os.path.join(utils.g_config.get('DEFAULT', 'dir'), extension[1:])
if not os.access(outdir, os.W_OK):
os.makedirs(outdir)
appargs = [ "ebook-convert",
indexfile,
#os.path.join(expanduser("~/feeds"),
# cleanfilename + extension),
# directory should be configurable too, probably
os.path.join(outdir, cleanfilename + extension),
"--authors", author ]
for flag in flags:
appargs.append(flag)
if verbose:
cmd = " ".join(appargs)
print("Running:", cmd, file=sys.stderr)
sys.stdout.flush()
run_conversion_cmd(appargs)
#
# Generate a fictionbook2 file
#
def make_fb2_file(indexfile, feedname, levels, ascii):
make_calibre_file(indexfile, feedname, ".fb2", levels, ascii,
"feedme", flags = [ "--disable-font-rescaling" ] )
#
# Generate an ePub file
# http://calibre-ebook.com/user_manual/cli/ebook-convert-3.html#html-input-to-epub-output
# XXX Would be nice to have a way to do this without needing calibre,
# so it could run on servers that don't have X/Qt libraries installed.
#
def make_epub_file(indexfile, feedname, levels, ascii):
make_calibre_file(indexfile, feedname, ".epub", levels, ascii,
time.strftime("%m-%d %a") + " feeds",
flags = [ '--no-default-epub-cover',
'--dont-split-on-page-breaks' ])
# END OUTPUT GENERATING FUNCTIONS
##################################################################