]> git.phdru.name Git - m_librarian.git/commitdiff
Download exactly one book
authorOleg Broytman <phd@phdru.name>
Sun, 5 Jun 2016 14:37:37 +0000 (17:37 +0300)
committerOleg Broytman <phd@phdru.name>
Sun, 5 Jun 2016 14:38:16 +0000 (17:38 +0300)
m_librarian/download.py [new file with mode: 0755]
sample/m_librarian.conf
scripts/ml-search.py

diff --git a/m_librarian/download.py b/m_librarian/download.py
new file mode 100755 (executable)
index 0000000..75f2b11
--- /dev/null
@@ -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()
index 17fc31093d96d9c981f8ddeb9e2f814b7be42b93..e3863aafca6c3f22a3bff70de664fd395b1d8224 100644 (file)
@@ -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
index b35d7eba546c375b85eaf8ad72d24db172c1ff9e..2f5767bc9408553e4ce4c4c74e7c247dccb10ffb 100755 (executable)
@@ -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')