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