]> git.phdru.name Git - m_librarian.git/blob - m_librarian/download.py
Feat(db): Add book.author1 property
[m_librarian.git] / m_librarian / download.py
1 #! /usr/bin/env python
2
3 from __future__ import print_function
4 import os
5 from time import mktime
6 from shutil import copyfileobj
7 from zipfile import ZipFile
8 from .config import get_config
9
10 __all__ = ['download']
11
12
13 format = '%f'
14 compile_format = True
15 compiled_format = '%(file)s'
16
17
18 def _compile_format():
19     global format, compile_format, compiled_format
20     if not compile_format:
21         return
22     compile_format = False
23     format = get_config().get('download', 'format')
24     if not format:
25         return
26     got_percent = False
27     compiled = []
28     for c in format:
29         if c == '%':
30             if got_percent:
31                 got_percent = False
32                 compiled.append('%')
33             else:
34                 got_percent = True
35         else:
36             if got_percent:
37                 got_percent = False
38                 if c == 'a':
39                     new_format = u'%(author1)s'
40                 elif c == 'e':
41                     new_format = u'%(extension)s'
42                 elif c == 'f':
43                     new_format = u'%(file)s'
44                 elif c == 'G':
45                     new_format = u'%(gname)s'
46                 elif c == 'g':
47                     new_format = u'%(gtitle)s'
48                 elif c == 'l':
49                     new_format = u'%(language)s'
50                 elif c == 'n':
51                     new_format = u'%(ser_no)d'
52                 elif c == 's':
53                     new_format = u'%(series)s'
54                 elif c == 't':
55                     new_format = u'%(title)s'
56                 else:
57                     raise ValueError('Bad format specifier "%%%c"' % c)
58                 compiled.append(new_format)
59             else:
60                 compiled.append(c)
61     compiled_format = ''.join(compiled)
62
63
64 _library_path = None
65
66
67 def download(book, dest_path=None, lib_path=None, a_format=None):
68     if lib_path is None:
69         global _library_path
70         if _library_path is None:
71             _library_path = get_config().getpath('library', 'path')
72         lib_path = _library_path
73
74     global format, compile_format, compiled_format
75     if a_format:
76         compile_format = True
77         format = a_format
78     _compile_format()
79     if compiled_format[-1] in ('\0', '\\', '/'):
80         raise ValueError('Bad format: "%s"' % compiled_format)
81     bdict = {}
82     bdict['author1'] = book.author1
83     bdict['extension'] = book.extension.name
84     bdict['file'] = book.file
85     genre = book.genres[0]
86     bdict['gname'] = genre.name
87     bdict['gtitle'] = genre.title
88     bdict['language'] = book.language.name
89     bdict['ser_no'] = book.ser_no or 0
90     bdict['series'] = book.series
91     bdict['title'] = book.title
92     if '%(extension)s' not in compiled_format:
93         compiled_format += '.%(extension)s'
94     filename = compiled_format % bdict
95     full_path = os.path.join(dest_path, filename)
96     try:
97         os.makedirs(os.path.dirname(full_path))
98     except OSError:
99         pass  # Already exists
100     zf = ZipFile(os.path.join(lib_path, book.archive),  'r')
101     infile = zf.open('%s.%s' % (book.file, book.extension.name))
102     outfile = open(full_path, 'wb')
103     copyfileobj(infile, outfile)
104     outfile.close()
105     infile.close()
106     zf.close()
107     dt = mktime(book.date.timetuple())
108     os.utime(full_path, (dt, dt))
109
110
111 def test():
112     _compile_format()
113     print(compiled_format)
114
115
116 if __name__ == '__main__':
117     test()