forked from hang333/HbookerAppNovelDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
187 lines (168 loc) · 7.02 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from instance import *
from bookshelf import *
try:
import HbookerAPI
except Exception as er:
print('[错误]', er)
print('[错误]', '请先安装requests库pycrypto或pycryptodome')
input()
exit()
import re
def refresh_bookshelf_list():
response = HbookerAPI.BookShelf.get_shelf_list()
if response.get('code') == '100000':
Vars.cfg.data['shelf_list'] = response['data']['shelf_list']
Vars.cfg.save()
BookShelfList.clear()
for shelf in Vars.cfg.data['shelf_list']:
BookShelfList.append(BookShelf(shelf))
for shelf in BookShelfList:
shelf.show_info()
if len(BookShelfList) == 1:
shell_bookshelf(['bookshelf', '1'])
else:
print('[提示]', '可输入"bookshelf <书架编号>"来选择/切换书架')
def shell_login(inputs):
if len(inputs) >= 3:
response = HbookerAPI.SignUp.login(inputs[1], inputs[2])
if response.get('code') == '100000':
Vars.cfg.data['reader_name'] = response['data']['reader_info']['reader_name']
Vars.cfg.data['user_code'] = response['data']['user_code']
Vars.cfg.data['common_params'] = {'login_token': response['data']['login_token'],
'account': response['data']['reader_info']['account']}
Vars.cfg.save()
HbookerAPI.setcommonparams(Vars.cfg.data['common_params'])
print('[提示]', '登录成功, 当前用户昵称为:', Vars.cfg.data['reader_name'])
else:
print('[提示]', response.get('tip'))
else:
print('[提示]', '请输入正确的参数')
def shell_config(inputs):
if len(inputs) >= 2:
if inputs[1].startswith('l'):
Vars.cfg.load()
print('[提示]', '配置文件已重新加载')
if Vars.cfg.data.get('user_code') is not None:
HbookerAPI.setcommonparams(Vars.cfg.data['common_params'])
elif inputs[1].startswith('sa'):
Vars.cfg.save()
print('[提示]', '配置文件已保存')
elif inputs[1].startswith('se'):
if len(inputs) >= 3:
Vars.cfg[inputs[2]] = inputs[3]
else:
Vars.cfg[inputs[2]] = None
print('[提示]', '配置项已修改')
else:
print('[提示]', 'config:', str(Vars.cfg.data))
def shell_bookshelf(inputs):
if len(inputs) >= 2:
Vars.current_bookshelf = get_bookshelf_by_index(inputs[1])
if Vars.current_bookshelf is None:
print('[提示]', '请输入正确的参数')
else:
print('[提示]', '已经选择书架: "' + Vars.current_bookshelf.shelf_name + '"')
Vars.current_bookshelf.get_book_list()
Vars.current_bookshelf.show_book_list()
else:
refresh_bookshelf_list()
def shell_books(inputs):
if len(inputs) >= 2:
Vars.current_book = Vars.current_bookshelf.get_book(inputs[1])
if Vars.current_book is None:
response = HbookerAPI.Book.get_info_by_id(inputs[1])
if response.get('code') == '100000':
Vars.current_book = Book(None, response['data']['book_info'])
else:
print('[提示]', '获取书籍信息失败, book_id:', inputs[1])
return
print('[提示]', '已经选择书籍《' + Vars.current_book.book_name + '》')
Vars.current_book.get_division_list()
Vars.current_book.get_chapter_catalog()
Vars.current_book.show_division_list()
Vars.current_book.show_chapter_latest()
else:
if Vars.current_bookshelf is None:
print('[提示]', '未选择书架')
else:
Vars.current_bookshelf.get_book_list()
Vars.current_bookshelf.show_book_list()
def shell_download(inputs):
if Vars.cfg.data.get('downloaded_book_id_list') is None:
Vars.cfg.data['downloaded_book_id_list'] = []
if inputs.count('-a') > 0:
for book in Vars.current_bookshelf.BookList:
book.get_division_list()
book.get_chapter_catalog()
book.download_chapter(copy_dir=os.getcwd() + '/../Hbooker/downloads')
if Vars.cfg.data['downloaded_book_id_list'].count(book.book_id) == 0:
Vars.cfg.data['downloaded_book_id_list'].append(book.book_id)
Vars.cfg.save()
print('[提示]', '书架下载已完成')
return
if Vars.current_book is None:
print('[提示]', '未选择书籍')
return
if inputs.count('-d') > 0:
if len(inputs) > inputs.index('-d') + 1:
Vars.current_book.download_division(inputs[inputs.index('-d') + 1])
else:
print('-d 参数出错')
return
chapter_index_start = None
chapter_index_end = None
if inputs.count('-s') > 0:
if len(inputs) > inputs.index('-s') + 1:
chapter_index_start = inputs[inputs.index('-s') + 1]
else:
print('-s 参数出错')
if inputs.count('-e') > 0:
if len(inputs) > inputs.index('-e') + 1:
chapter_index_end = inputs[inputs.index('-e') + 1]
else:
print('-e 参数出错')
Vars.current_book.download_chapter(chapter_index_start, chapter_index_end)
if Vars.cfg.data['downloaded_book_id_list'].count(Vars.current_book.book_id) == 0:
Vars.cfg.data['downloaded_book_id_list'].append(Vars.current_book.book_id)
Vars.cfg.save()
def shell_update():
if Vars.cfg.data.get('downloaded_book_id_list') is None:
print('[提示]', '暂无已下载书籍')
return
for book_id in Vars.cfg.data['downloaded_book_id_list']:
response = HbookerAPI.Book.get_info_by_id(book_id)
if response.get('code') == '100000':
Vars.current_book = Book(None, response['data']['book_info'])
Vars.current_book.get_division_list()
Vars.current_book.get_chapter_catalog()
Vars.current_book.download_chapter(copy_dir=os.getcwd() + '/../Hbooker/updates')
else:
print('[提示]', '获取书籍信息失败, book_id:', book_id)
print('[提示]', '书籍更新已完成')
def shell():
for info in Vars.help_info:
print('[帮助]', info)
while True:
inputs = re.split('\s+', get('>').strip())
if inputs[0].startswith('q'):
exit()
elif inputs[0].startswith('l'):
shell_login(inputs)
elif inputs[0].startswith('c'):
shell_config(inputs)
elif inputs[0].startswith('h'):
for info in Vars.help_info:
print('[帮助]', info)
elif inputs[0].startswith('books'):
shell_bookshelf(inputs)
elif inputs[0].startswith('b'):
shell_books(inputs)
elif inputs[0].startswith('d'):
shell_download(inputs)
elif inputs[0].startswith('u'):
shell_update()
Vars.cfg.load()
if Vars.cfg.data.get('user_code') is not None:
HbookerAPI.setcommonparams(Vars.cfg.data['common_params'])
refresh_bookshelf_list()
shell()