From 451adf38b4a48883e8246431c1ee47f377201452 Mon Sep 17 00:00:00 2001 From: FengYu Date: Tue, 16 Dec 2014 16:22:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- getComic-gui.py | 16 ++++++++++------ getComic.py | 20 +++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/getComic-gui.py b/getComic-gui.py index 753fa46..6a9215d 100755 --- a/getComic-gui.py +++ b/getComic-gui.py @@ -30,6 +30,7 @@ def __init__(self, parent=None): comicNameLabel = QLabel("漫画名: ") self.comicNameLabel = QLabel("暂无") + self.one_folder_checkbox = QCheckBox("单目录") comicIntroLabel = QLabel("简介: ") self.comicIntro = QLabel("暂无") @@ -60,13 +61,13 @@ def __init__(self, parent=None): 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) @@ -135,6 +136,7 @@ def anaysisURL(self): def download(self): self.downloadButton.setText("下载中...") + one_folder = self.one_folder_checkbox.isChecked() self.enableWidget(False) @@ -149,7 +151,7 @@ def download(self): if not os.path.isdir(comicPath): os.makedirs(comicPath) - self.downloadThread = Downloader(selectedChapterList, comicPath, self.contentList, self.contentNameList, self.id) + 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() @@ -158,7 +160,7 @@ class Downloader(QThread): output = pyqtSignal(['QString']) finished = pyqtSignal() - def __init__(self, selectedChapterList, comicPath, contentList, contentNameList, id, parent=None): + def __init__(self, selectedChapterList, comicPath, contentList, contentNameList, id, one_folder=False, parent=None): super(Downloader, self).__init__(parent) self.selectedChapterList = selectedChapterList @@ -166,6 +168,7 @@ def __init__(self, selectedChapterList, comicPath, contentList, contentNameList, self.contentList = contentList self.contentNameList = contentNameList self.id = id + self.one_folder = one_folder def run(self): try: @@ -176,10 +179,11 @@ def run(self): forbiddenRE = re.compile(r'[\\/":*?<>|]') #windows下文件名非法字符\ / : * ? " < > | self.contentNameList[i] = re.sub(forbiddenRE, '_', self.contentNameList[i]) contentPath = os.path.join(self.comicPath, '第{0:0>4}话-{1}'.format(i+1, self.contentNameList[i])) - if not os.path.isdir(contentPath): - os.mkdir(contentPath) + if not self.one_folder: + if not os.path.isdir(contentPath): + os.mkdir(contentPath) imgList = getComic.getImgList(self.contentList[i], self.id) - getComic.downloadImg(imgList, contentPath) + getComic.downloadImg(imgList, contentPath, self.one_folder) self.output.emit('完毕!') diff --git a/getComic.py b/getComic.py index cd5895c..7a248df 100755 --- a/getComic.py +++ b/getComic.py @@ -105,14 +105,17 @@ def getImgList(contentJson, id): imgList.append(g) return imgList -def downloadImg(imgUrlList, contentPath): +def downloadImg(imgUrlList, contentPath, one_folder=False): count = len(imgUrlList) print('该集漫画共计{}张图片'.format(count)) i = 1 for imgUrl in imgUrlList: print('\r正在下载第{}张图片...'.format(i), end = '') - imgPath = os.path.join(contentPath, '{0:0>3}.jpg'.format(i)) + if not one_folder: + imgPath = os.path.join(contentPath, '{0:0>3}.jpg'.format(i)) + else: + imgPath = contentPath + '{0:0>3}.jpg'.format(i) i += 1 #目标文件存在就跳过下载 @@ -163,7 +166,7 @@ def parseLIST(lst): parsedLIST = sorted(set(parsedLIST)) #按照从小到大的顺序排序并去重 return parsedLIST -def main(url, path, lst=None): +def main(url, path, lst=None, one_folder=False): '''url: 要爬取的漫画首页。 path: 漫画下载路径。 lst: 要下载的章节列表(-l|--list后面的参数)''' try: if not os.path.isdir(path): @@ -210,11 +213,12 @@ def main(url, path, lst=None): except Exception: print('正在下载第{0:0>4}话: {1}'.format(i)) - if not os.path.isdir(contentPath): - os.mkdir(contentPath) + if not one_folder: + if not os.path.isdir(contentPath): + os.mkdir(contentPath) imgList = getImgList(contentList[i - 1], id) - downloadImg(imgList, contentPath) + downloadImg(imgList, contentPath, one_folder) except ErrorCode as e: exit(e.code) @@ -232,6 +236,7 @@ def main(url, path, lst=None): 'http://ac.qq.com/naruto') parser.add_argument('-p', '--path', help='漫画下载路径。 默认: {}'.format(defaultPath), default=defaultPath) + parser.add_argument('-d', '--dir', action='store_true', help='将所有图片下载到一个目录(适合腾讯漫画等软件连看使用)') parser.add_argument('-l', '--list', help=("要下载的漫画章节列表,不指定则下载所有章节。格式范例: \n" "N - 下载具体某一章节,如-l 1, 下载第1章\n" 'N,N... - 下载某几个不连续的章节,如 "-l 1,3,5", 下载1,3,5章\n' @@ -241,6 +246,7 @@ def main(url, path, lst=None): url = args.url path = args.path lst = args.list + one_folder = args.dir if lst: legalListRE = re.compile(r'^\d+([,-]\d+)*$') @@ -254,4 +260,4 @@ def main(url, path, lst=None): if not path: path = defaultPath - main(url, path, lst) + main(url, path, lst, one_folder)