]> git.phdru.name Git - m_librarian.git/blob - m_librarian/download.py
Feat(db): Add book.genre_list_* properties
[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 == 'A':
41                     new_format = u'%(authors)s'
42                 elif c == 'e':
43                     new_format = u'%(extension)s'
44                 elif c == 'f':
45                     new_format = u'%(file)s'
46                 elif c == 'G':
47                     new_format = u'%(gname)s'
48                 elif c == 'g':
49                     new_format = u'%(gtitle)s'
50                 elif c == 'J':
51                     new_format = u'%(gname_list)s'
52                 elif c == 'j':
53                     new_format = u'%(gtitle_list)s'
54                 elif c == 'l':
55                     new_format = u'%(language)s'
56                 elif c == 'n':
57                     new_format = u'%(ser_no)d'
58                 elif c == 's':
59                     new_format = u'%(series)s'
60                 elif c == 't':
61                     new_format = u'%(title)s'
62                 else:
63                     raise ValueError('Bad format specifier "%%%c"' % c)
64                 compiled.append(new_format)
65             else:
66                 compiled.append(c)
67     compiled_format = ''.join(compiled)
68
69
70 _library_path = None
71
72
73 def download(book, dest_path=None, lib_path=None, a_format=None):
74     if lib_path is None:
75         global _library_path
76         if _library_path is None:
77             _library_path = get_config().getpath('library', 'path')
78         lib_path = _library_path
79
80     global format, compile_format, compiled_format
81     if a_format:
82         compile_format = True
83         format = a_format
84     _compile_format()
85     if compiled_format[-1] in ('\0', '\\', '/'):
86         raise ValueError('Bad format: "%s"' % compiled_format)
87     bdict = {}
88     bdict['author1'] = book.author1
89     bdict['authors'] = book.author_list
90     bdict['extension'] = book.extension.name
91     bdict['file'] = book.file
92     genre = book.genres[0]
93     bdict['gname'] = genre.name
94     bdict['gtitle'] = genre.title
95     bdict['gname_list'] = book.genre_name_list
96     bdict['gtitle_list'] = book.genre_title_list
97     bdict['language'] = book.language.name
98     bdict['ser_no'] = book.ser_no or 0
99     bdict['series'] = book.series
100     bdict['title'] = book.title
101     if '%(extension)s' not in compiled_format:
102         compiled_format += '.%(extension)s'
103     filename = compiled_format % bdict
104     full_path = os.path.join(dest_path, filename)
105     try:
106         os.makedirs(os.path.dirname(full_path))
107     except OSError:
108         pass  # Already exists
109     zf = ZipFile(os.path.join(lib_path, book.archive),  'r')
110     infile = zf.open('%s.%s' % (book.file, book.extension.name))
111     outfile = open(full_path, 'wb')
112     copyfileobj(infile, outfile)
113     outfile.close()
114     infile.close()
115     zf.close()
116     dt = mktime(book.date.timetuple())
117     os.utime(full_path, (dt, dt))
118
119
120 def test():
121     _compile_format()
122     print(compiled_format)
123
124
125 if __name__ == '__main__':
126     test()