]> git.lizzy.rs Git - nhentai.git/blob - nhentai/doujinshi.py
0.2.1
[nhentai.git] / nhentai / doujinshi.py
1 # coding: utf-8
2 from __future__ import print_function, unicode_literals
3 from tabulate import tabulate
4 from future.builtins import range
5
6 from nhentai.constant import DETAIL_URL, IMAGE_URL
7 from nhentai.logger import logger
8
9
10 class DoujinshiInfo(dict):
11     def __init__(self, **kwargs):
12         super(DoujinshiInfo, self).__init__(**kwargs)
13
14     def __getattr__(self, item):
15         try:
16             return dict.__getitem__(self, item)
17         except KeyError:
18             return ''
19
20
21 class Doujinshi(object):
22     def __init__(self, name=None, id=None, img_id=None, ext='jpg', pages=0, **kwargs):
23         self.name = name
24         self.id = id
25         self.img_id = img_id
26         self.ext = ext
27         self.pages = pages
28         self.downloader = None
29         self.url = '%s/%d' % (DETAIL_URL, self.id)
30         self.info = DoujinshiInfo(**kwargs)
31
32     def __repr__(self):
33         return '<Doujinshi: {0}>'.format(self.name)
34
35     def show(self):
36         table = [
37             ["Doujinshi", self.name],
38             ["Subtitle", self.info.subtitle],
39             ["Characters", self.info.characters],
40             ["Authors", self.info.artists],
41             ["Language", self.info.language],
42             ["Tags", self.info.tags],
43             ["URL", self.url],
44             ["Pages", self.pages],
45         ]
46         logger.info(u'Print doujinshi information of {0}\n{1}'.format(self.id, tabulate(table)))
47
48     def download(self):
49         logger.info('Start download doujinshi: %s' % self.name)
50         if self.downloader:
51             download_queue = []
52             for i in range(1, self.pages + 1):
53                 download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext))
54             self.downloader.download(download_queue, self.id)
55         else:
56             logger.critical('Downloader has not be loaded')
57
58
59 if __name__ == '__main__':
60     test = Doujinshi(name='test nhentai doujinshi', id=1)
61     print(test)
62     test.show()
63     try:
64         test.download()
65     except Exception as e:
66         print('Exception: %s' % str(e))