X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=m_librarian%2Fdownload.py;h=281efcaf7a2c81b4f32013da206b71c314ea2f74;hb=924bbc89d4f1ffa35e747ca4a5d63601275c8760;hp=db09f12f08e263b786abef9fbb804a2ae7de7ed6;hpb=9b4f50c79b856146e1d90444df10e64797869997;p=m_librarian.git diff --git a/m_librarian/download.py b/m_librarian/download.py index db09f12..281efca 100755 --- a/m_librarian/download.py +++ b/m_librarian/download.py @@ -17,12 +17,12 @@ compiled_format = '%(file)s' def _compile_format(): global format, compile_format, compiled_format - if compile_format: - compile_format = False - try: - format = get_config().get('download', 'format') - except: - return + if not compile_format: + return + compile_format = False + format = get_config().get('download', 'format') + if not format: + return got_percent = False compiled = [] for c in format: @@ -36,7 +36,9 @@ def _compile_format(): if got_percent: got_percent = False if c == 'a': - new_format = u'%(author)s' + new_format = u'%(author1)s' + elif c == 'A': + new_format = u'%(authors)s' elif c == 'e': new_format = u'%(extension)s' elif c == 'f': @@ -45,6 +47,10 @@ def _compile_format(): new_format = u'%(gname)s' elif c == 'g': new_format = u'%(gtitle)s' + elif c == 'J': + new_format = u'%(gname_list)s' + elif c == 'j': + new_format = u'%(gtitle_list)s' elif c == 'l': new_format = u'%(language)s' elif c == 'n': @@ -64,27 +70,30 @@ def _compile_format(): _library_path = None -def download(book, path=None, a_format=None): - if path is None: +def download(book, dest_path=None, lib_path=None, a_format=None): + if lib_path is None: global _library_path if _library_path is None: - _library_path = get_config().get('library', 'path') - path = _library_path + _library_path = get_config().getpath('library', 'path') + lib_path = _library_path global format, compile_format, compiled_format if a_format: - format = a_format compile_format = True + format = a_format _compile_format() if compiled_format[-1] in ('\0', '\\', '/'): raise ValueError('Bad format: "%s"' % compiled_format) bdict = {} - bdict['author'] = book.authors[0].fullname + bdict['author1'] = book.author1 + bdict['authors'] = book.author_list bdict['extension'] = book.extension.name bdict['file'] = book.file genre = book.genres[0] bdict['gname'] = genre.name bdict['gtitle'] = genre.title + bdict['gname_list'] = book.genre_name_list + bdict['gtitle_list'] = book.genre_title_list bdict['language'] = book.language.name bdict['ser_no'] = book.ser_no or 0 bdict['series'] = book.series @@ -92,19 +101,20 @@ def download(book, path=None, a_format=None): if '%(extension)s' not in compiled_format: compiled_format += '.%(extension)s' filename = compiled_format % bdict + full_path = os.path.join(dest_path, filename) try: - os.makedirs(os.path.dirname(filename)) + os.makedirs(os.path.dirname(full_path)) except OSError: pass # Already exists - zf = ZipFile(os.path.join(path, book.archive), 'r') + zf = ZipFile(os.path.join(lib_path, book.archive), 'r') infile = zf.open('%s.%s' % (book.file, book.extension.name)) - outfile = open(filename, 'wb') + outfile = open(full_path, 'wb') copyfileobj(infile, outfile) outfile.close() infile.close() zf.close() dt = mktime(book.date.timetuple()) - os.utime(filename, (dt, dt)) + os.utime(full_path, (dt, dt)) def test():