]> git.phdru.name Git - m_librarian.git/blob - m_librarian/download.py
db09f12f08e263b786abef9fbb804a2ae7de7ed6
[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         try:
23             format = get_config().get('download', 'format')
24         except:
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'%(author)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, path=None, a_format=None):
68     if path is None:
69         global _library_path
70         if _library_path is None:
71             _library_path = get_config().get('library', 'path')
72         path = _library_path
73
74     global format, compile_format, compiled_format
75     if a_format:
76         format = a_format
77         compile_format = True
78     _compile_format()
79     if compiled_format[-1] in ('\0', '\\', '/'):
80         raise ValueError('Bad format: "%s"' % compiled_format)
81     bdict = {}
82     bdict['author'] = book.authors[0].fullname
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     try:
96         os.makedirs(os.path.dirname(filename))
97     except OSError:
98         pass  # Already exists
99     zf = ZipFile(os.path.join(path, book.archive),  'r')
100     infile = zf.open('%s.%s' % (book.file, book.extension.name))
101     outfile = open(filename, 'wb')
102     copyfileobj(infile, outfile)
103     outfile.close()
104     infile.close()
105     zf.close()
106     dt = mktime(book.date.timetuple())
107     os.utime(filename, (dt, dt))
108
109
110 def test():
111     _compile_format()
112     print(compiled_format)
113
114
115 if __name__ == '__main__':
116     test()