From: Oleg Broytman Date: Sun, 5 Jun 2016 14:37:37 +0000 (+0300) Subject: Download exactly one book X-Git-Tag: 0.0.11~6 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=51132595d65c48a10906e266b64e696bc3b3f87d;p=m_librarian.git Download exactly one book --- diff --git a/m_librarian/download.py b/m_librarian/download.py new file mode 100755 index 0000000..75f2b11 --- /dev/null +++ b/m_librarian/download.py @@ -0,0 +1,25 @@ +#! /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() diff --git a/sample/m_librarian.conf b/sample/m_librarian.conf index 17fc310..e3863aa 100644 --- a/sample/m_librarian.conf +++ b/sample/m_librarian.conf @@ -8,3 +8,6 @@ ; URI = postgres://user@host/database?debug=&cache= ; URI = sqlite:///full/path/to/database ; URI = sqlite:/C:/full/path/to/database + +[library] +path = /var/lib/LRE_Flibusta diff --git a/scripts/ml-search.py b/scripts/ml-search.py index b35d7eb..2f5767b 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -6,6 +6,7 @@ from sqlobject.sqlbuilder import CONCAT 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 @@ -138,6 +139,15 @@ def _search_books(case_sensitive, search_type, args): 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), @@ -279,6 +289,8 @@ if __name__ == '__main__': 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')