-
Notifications
You must be signed in to change notification settings - Fork 77
/
getComic-gui.py
executable file
·215 lines (166 loc) · 8.12 KB
/
getComic-gui.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
import os
import re
import sys
import traceback
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import getComic
class TencentComicDownloader(QWidget):
def __init__(self, parent=None):
super(TencentComicDownloader, self).__init__(parent)
nameLabel = QLabel("漫画首页:")
self.nameLine = QLineEdit()
self.analysisButton = QPushButton("分析")
self.analysisButton.clicked.connect(self.anaysisURL)
self.nameLine.returnPressed.connect(self.analysisButton.click)
pathLineLabel = QLabel("下载路径:")
self.pathLine = QLineEdit()
defaultPath = os.path.join(os.path.expanduser('~'), 'tencent_comic')
self.pathLine.setText(defaultPath)
self.browseButton = QPushButton("浏览")
self.browseButton.clicked.connect(self.getPath)
comicNameLabel = QLabel("漫画名: ")
self.comicNameLabel = QLabel("暂无")
self.one_folder_checkbox = QCheckBox("单目录")
comicIntroLabel = QLabel("简介: ")
self.comicIntro = QLabel("暂无")
self.comicIntro.setWordWrap(True)
chapterGroupBox = QGroupBox("章节列表: (按住CTRL点击可不连续选中,鼠标拖拽或按住SHIFT点击首尾可连续选中,CTRL+A全选)")
self.chapterListView = QListWidget(chapterGroupBox)
self.chapterListView.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.chapterListView.setEnabled(False)
groupBoxLayout = QHBoxLayout(chapterGroupBox)
groupBoxLayout.addWidget(self.chapterListView)
self.downloadButton = QPushButton("下载选中")
self.statusLabel = QLabel("输入要下载的漫画的首页,然后点分析")
self.statusLabel.setWordWrap(True)
self.downloadButton.setEnabled(False)
self.downloadButton.clicked.connect(self.download)
mainLayout = QGridLayout()
mainLayout.addWidget(nameLabel, 0, 0)
mainLayout.addWidget(self.nameLine, 0, 1)
mainLayout.addWidget(self.analysisButton, 0, 2)
mainLayout.addWidget(pathLineLabel, 1, 0)
mainLayout.addWidget(self.pathLine, 1, 1)
mainLayout.addWidget(self.browseButton, 1, 2)
mainLayout.addWidget(comicNameLabel, 2, 0)
mainLayout.addWidget(self.comicNameLabel, 2, 1, 1, 2)
mainLayout.addWidget(self.one_folder_checkbox, 2, 2)
mainLayout.addWidget(comicIntroLabel, 3, 0)
mainLayout.addWidget(self.comicIntro, 3, 1, 1, 2)
mainLayout.addWidget(chapterGroupBox, 4, 0, 1, 3)
mainLayout.addWidget(self.downloadButton, 5, 2)
mainLayout.addWidget(self.statusLabel, 5, 0, 1, 2)
self.setLayout(mainLayout)
self.setWindowTitle("腾讯漫画下载")
self.setGeometry(400, 300, 800, 500)
def setStatus(self, status):
self.statusLabel.setText(status)
def enableWidget(self, enable):
widgets_list = [
self.downloadButton,
self.nameLine,
self.pathLine,
self.chapterListView,
self.analysisButton,
self.browseButton,
self.one_folder_checkbox
]
for widget in widgets_list:
widget.setEnabled(enable)
if enable:
self.downloadButton.setText('下载选中')
self.chapterListView.setFocus()
def getPath(self):
path = str(QFileDialog.getExistingDirectory(self, "选择下载目录"))
if path:
self.pathLine.setText(path)
def anaysisURL(self):
url = self.nameLine.text()
self.downloadButton.setEnabled(False)
self.comicNameLabel.setText("暂无")
self.comicIntro.setText("暂无")
self.chapterListView.clear()
self.chapterListView.setEnabled(False)
try:
if getComic.isLegelUrl(url):
self.id = getComic.getId(url)
self.comicName, self.comicIntrd, self.count, self.contentList = getComic.getContent(self.id)
self.contentNameList = []
for item in self.contentList:
self.contentNameList.append(item['name'])
self.comicNameLabel.setText(self.comicName)
self.comicIntro.setText(self.comicIntrd)
self.chapterListView.setEnabled(True)
self.downloadButton.setEnabled(True)
self.chapterListView.setFocus()
self.statusLabel.setText('选择要下载的章节后点击右侧按钮')
for i in range(len(self.contentNameList)):
self.chapterListView.addItem('第{0:0>4}话-{1}'.format(i + 1, self.contentNameList[i]))
self.chapterListView.item(i).setSelected(True)
self.downloadButton.setEnabled(True)
else:
self.statusLabel.setText('<font color="red">错误的URL格式!请输入正确的漫画首页地址!</font>')
except getComic.ErrorCode as e:
if e.code == 2:
self.statusLabel.setText('<font color="red">无法跳转为移动端URL,请进入http://m.ac.qq.com找到该漫画地址</font>')
except KeyError:
self.statusLabel.setText('<font color="red">不存在的地址</font>')
except Exception as e:
self.statusLabel.setText('<font color="red">{}</font>'.format(e))
traceback.print_exc()
def download(self):
self.downloadButton.setText("下载中...")
one_folder = self.one_folder_checkbox.isChecked()
self.enableWidget(False)
selectedChapterList = [item.row() for item in self.chapterListView.selectedIndexes()]
path = self.pathLine.text()
comicName = self.comicName
forbiddenRE = re.compile(r'[\\/":*?<>|]') # windows下文件名非法字符\ / : * ? " < > |
comicName = re.sub(forbiddenRE, '_', comicName) # 将windows下的非法字符一律替换为_
comicPath = os.path.join(path, comicName)
if not os.path.isdir(comicPath):
os.makedirs(comicPath)
self.downloadThread = Downloader(selectedChapterList, comicPath, self.contentList, self.contentNameList,
self.id, one_folder)
self.downloadThread.output.connect(self.setStatus)
self.downloadThread.finished.connect(lambda: self.enableWidget(True))
self.downloadThread.start()
class Downloader(QThread):
output = pyqtSignal(['QString'])
finished = pyqtSignal()
def __init__(self, selectedChapterList, comicPath, contentList, contentNameList, id, one_folder=False, parent=None):
super(Downloader, self).__init__(parent)
self.selectedChapterList = selectedChapterList
self.comicPath = comicPath
self.contentList = contentList
self.contentNameList = contentNameList
self.id = id
self.one_folder = one_folder
def run(self):
try:
for i in self.selectedChapterList:
outputString = '正在下载第{0:0>4}话: {1}...'.format(i + 1, self.contentNameList[i])
print(outputString)
self.output.emit(outputString)
forbiddenRE = re.compile(r'[\\/":*?<>|]') # windows下文件名非法字符\ / : * ? " < > |
self.contentNameList[i] = re.sub(forbiddenRE, '_', self.contentNameList[i]).strip()
contentPath = os.path.join(self.comicPath, '第{0:0>4}话-{1}'.format(i + 1, self.contentNameList[i]))
if not self.one_folder:
if not os.path.isdir(contentPath):
os.mkdir(contentPath)
imgList = getComic.getImgList(self.contentList[i]['url'])
getComic.downloadImg(imgList, contentPath, self.one_folder)
self.output.emit('完毕!')
except Exception as e:
self.output.emit('<font color="red">{}</font>\n'
'遇到异常!请尝试重新点击下载按钮重试'.format(e))
raise
finally:
self.finished.emit()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = TencentComicDownloader()
main.show()
app.exec_()