when automatically naming a book, don't assume it is a manual
[objavi2.git] / epubjavi.cgi
blobb646cd51642f0ef3d25995466193034ff80ddb53
1 #!/usr/bin/python
2 import os, sys
3 import cgi
4 from getopt import gnu_getopt
6 from objavi.fmbook import log, ZipBook, make_book_name
7 from objavi.cgi_utils import shift_file, parse_args, optionise, print_template
8 from objavi import config
10 USE_CACHED_IMAGES = True #avoid network -- will make out of date books in production!
12 BOOKS = [x[:-4] for x in os.listdir(config.BOOKI_BOOK_DIR) if x.endswith('.zip')]
14 def print_form(booklink):
15 print_template('templates/epubjavi.html',
16 {'booklink': booklink,
17 'booklist': optionise(sorted(BOOKS))}
20 def epubjavi(book, use_cached_images=USE_CACHED_IMAGES):
21 book = ZipBook(config.LOCALHOST, book)
22 book.make_epub(use_cache=use_cached_images)
23 return shift_file(book.epubfile, config.EPUB_DIR)
26 # ARG_VALIDATORS is a mapping between the expected cgi arguments and
27 # functions to validate their values. (None means no validation).
28 ARG_VALIDATORS = {
29 "book": BOOKS.__contains__,
32 if __name__ == '__main__':
33 args = parse_args(ARG_VALIDATORS)
34 if 'book' in args:
35 url = epubjavi(args['book'])
36 book_link = '<p>Download <a href="%s">%s epub</a>.</p>' %(url, args['book'])
37 else:
38 book_link = ''
39 print_form(book_link)