Skip to content

Commit 451adf3

Browse files
author
FengYu
committed
增加单目录下载模式
1 parent 0ee1863 commit 451adf3

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

getComic-gui.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def __init__(self, parent=None):
3030

3131
comicNameLabel = QLabel("漫画名: ")
3232
self.comicNameLabel = QLabel("暂无")
33+
self.one_folder_checkbox = QCheckBox("单目录")
3334

3435
comicIntroLabel = QLabel("简介: ")
3536
self.comicIntro = QLabel("暂无")
@@ -60,13 +61,13 @@ def __init__(self, parent=None):
6061
mainLayout.addWidget(self.browseButton, 1, 2)
6162
mainLayout.addWidget(comicNameLabel, 2, 0)
6263
mainLayout.addWidget(self.comicNameLabel, 2, 1, 1, 2)
64+
mainLayout.addWidget(self.one_folder_checkbox, 2, 2)
6365
mainLayout.addWidget(comicIntroLabel, 3, 0)
6466
mainLayout.addWidget(self.comicIntro, 3, 1, 1, 2)
6567
mainLayout.addWidget(chapterGroupBox, 4, 0, 1, 3)
6668
mainLayout.addWidget(self.downloadButton, 5, 2)
6769
mainLayout.addWidget(self.statusLabel, 5, 0, 1, 2)
6870

69-
7071
self.setLayout(mainLayout)
7172
self.setWindowTitle("腾讯漫画下载")
7273
self.setGeometry(400, 300, 800, 500)
@@ -135,6 +136,7 @@ def anaysisURL(self):
135136

136137
def download(self):
137138
self.downloadButton.setText("下载中...")
139+
one_folder = self.one_folder_checkbox.isChecked()
138140

139141
self.enableWidget(False)
140142

@@ -149,7 +151,7 @@ def download(self):
149151
if not os.path.isdir(comicPath):
150152
os.makedirs(comicPath)
151153

152-
self.downloadThread = Downloader(selectedChapterList, comicPath, self.contentList, self.contentNameList, self.id)
154+
self.downloadThread = Downloader(selectedChapterList, comicPath, self.contentList, self.contentNameList, self.id, one_folder)
153155
self.downloadThread.output.connect(self.setStatus)
154156
self.downloadThread.finished.connect(lambda: self.enableWidget(True))
155157
self.downloadThread.start()
@@ -158,14 +160,15 @@ class Downloader(QThread):
158160
output = pyqtSignal(['QString'])
159161
finished = pyqtSignal()
160162

161-
def __init__(self, selectedChapterList, comicPath, contentList, contentNameList, id, parent=None):
163+
def __init__(self, selectedChapterList, comicPath, contentList, contentNameList, id, one_folder=False, parent=None):
162164
super(Downloader, self).__init__(parent)
163165

164166
self.selectedChapterList = selectedChapterList
165167
self.comicPath = comicPath
166168
self.contentList = contentList
167169
self.contentNameList = contentNameList
168170
self.id = id
171+
self.one_folder = one_folder
169172

170173
def run(self):
171174
try:
@@ -176,10 +179,11 @@ def run(self):
176179
forbiddenRE = re.compile(r'[\\/":*?<>|]') #windows下文件名非法字符\ / : * ? " < > |
177180
self.contentNameList[i] = re.sub(forbiddenRE, '_', self.contentNameList[i])
178181
contentPath = os.path.join(self.comicPath, '第{0:0>4}话-{1}'.format(i+1, self.contentNameList[i]))
179-
if not os.path.isdir(contentPath):
180-
os.mkdir(contentPath)
182+
if not self.one_folder:
183+
if not os.path.isdir(contentPath):
184+
os.mkdir(contentPath)
181185
imgList = getComic.getImgList(self.contentList[i], self.id)
182-
getComic.downloadImg(imgList, contentPath)
186+
getComic.downloadImg(imgList, contentPath, self.one_folder)
183187

184188
self.output.emit('完毕!')
185189

getComic.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ def getImgList(contentJson, id):
105105
imgList.append(g)
106106
return imgList
107107

108-
def downloadImg(imgUrlList, contentPath):
108+
def downloadImg(imgUrlList, contentPath, one_folder=False):
109109
count = len(imgUrlList)
110110
print('该集漫画共计{}张图片'.format(count))
111111
i = 1
112112

113113
for imgUrl in imgUrlList:
114114
print('\r正在下载第{}张图片...'.format(i), end = '')
115-
imgPath = os.path.join(contentPath, '{0:0>3}.jpg'.format(i))
115+
if not one_folder:
116+
imgPath = os.path.join(contentPath, '{0:0>3}.jpg'.format(i))
117+
else:
118+
imgPath = contentPath + '{0:0>3}.jpg'.format(i)
116119
i += 1
117120

118121
#目标文件存在就跳过下载
@@ -163,7 +166,7 @@ def parseLIST(lst):
163166
parsedLIST = sorted(set(parsedLIST)) #按照从小到大的顺序排序并去重
164167
return parsedLIST
165168

166-
def main(url, path, lst=None):
169+
def main(url, path, lst=None, one_folder=False):
167170
'''url: 要爬取的漫画首页。 path: 漫画下载路径。 lst: 要下载的章节列表(-l|--list后面的参数)'''
168171
try:
169172
if not os.path.isdir(path):
@@ -210,11 +213,12 @@ def main(url, path, lst=None):
210213
except Exception:
211214
print('正在下载第{0:0>4}话: {1}'.format(i))
212215

213-
if not os.path.isdir(contentPath):
214-
os.mkdir(contentPath)
216+
if not one_folder:
217+
if not os.path.isdir(contentPath):
218+
os.mkdir(contentPath)
215219

216220
imgList = getImgList(contentList[i - 1], id)
217-
downloadImg(imgList, contentPath)
221+
downloadImg(imgList, contentPath, one_folder)
218222

219223
except ErrorCode as e:
220224
exit(e.code)
@@ -232,6 +236,7 @@ def main(url, path, lst=None):
232236
'http://ac.qq.com/naruto')
233237
parser.add_argument('-p', '--path', help='漫画下载路径。 默认: {}'.format(defaultPath),
234238
default=defaultPath)
239+
parser.add_argument('-d', '--dir', action='store_true', help='将所有图片下载到一个目录(适合腾讯漫画等软件连看使用)')
235240
parser.add_argument('-l', '--list', help=("要下载的漫画章节列表,不指定则下载所有章节。格式范例: \n"
236241
"N - 下载具体某一章节,如-l 1, 下载第1章\n"
237242
'N,N... - 下载某几个不连续的章节,如 "-l 1,3,5", 下载1,3,5章\n'
@@ -241,6 +246,7 @@ def main(url, path, lst=None):
241246
url = args.url
242247
path = args.path
243248
lst = args.list
249+
one_folder = args.dir
244250

245251
if lst:
246252
legalListRE = re.compile(r'^\d+([,-]\d+)*$')
@@ -254,4 +260,4 @@ def main(url, path, lst=None):
254260
if not path:
255261
path = defaultPath
256262

257-
main(url, path, lst)
263+
main(url, path, lst, one_folder)

0 commit comments

Comments
 (0)