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