--- /dev/null
+#! /usr/bin/env python
+
+import os
+from shutil import copyfileobj
+from zipfile import ZipFile
+from .config import get_config
+
+__all__ = ['download']
+
+
+_library_path = None
+
+
+def download(archive, filename):
+ global _library_path
+ if _library_path is None:
+ _library_path = get_config().get('library', 'path')
+
+ zf = ZipFile(os.path.join(_library_path, archive), 'r')
+ infile = zf.open(filename)
+ outfile = open(filename, 'wb')
+ copyfileobj(infile, outfile)
+ outfile.close()
+ infile.close()
+ zf.close()
from m_lib.defenc import default_encoding
from m_librarian.db import Author, Book, Extension, Genre, Language, open_db
+from m_librarian.download import download
from m_librarian.search import mk_search_conditions, \
search_authors, search_books, \
search_extensions, search_genres, search_languages
if args.count:
print_count(books.count())
return
+ if args.get:
+ count = books.count()
+ if count != 1:
+ sys.stderr.write("There must be exactly 1 book for --get; "
+ "(found %d).\n" % count)
+ sys.exit(1)
+ book = books[0]
+ download(book.archive, '%s.%s' % (book.file, book.extension.name))
+ return
count = 0
for book in books:
print book.title.encode(default_encoding),
parser.add_argument('-s', '--series', help='search by series')
parser.add_argument('-a', '--archive', help='search by archive (zip file)')
parser.add_argument('-f', '--file', help='search by file name')
+ parser.add_argument('--get', action='store_true',
+ help='download exactly one book')
parser.add_argument('--id', help='search by database id')
parser.add_argument('--surname', help='search by author\'s surname')
parser.add_argument('--name', help='search by author\'s name')