Skip to content

Commit

Permalink
增加单目录下载模式
Browse files Browse the repository at this point in the history
  • Loading branch information
FengYu committed Dec 16, 2014
1 parent 0ee1863 commit 451adf3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 10 additions & 6 deletions getComic-gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, parent=None):

comicNameLabel = QLabel("漫画名: ")
self.comicNameLabel = QLabel("暂无")
self.one_folder_checkbox = QCheckBox("单目录")

comicIntroLabel = QLabel("简介: ")
self.comicIntro = QLabel("暂无")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -135,6 +136,7 @@ def anaysisURL(self):

def download(self):
self.downloadButton.setText("下载中...")
one_folder = self.one_folder_checkbox.isChecked()

self.enableWidget(False)

Expand All @@ -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()
Expand All @@ -158,14 +160,15 @@ 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
self.comicPath = comicPath
self.contentList = contentList
self.contentNameList = contentNameList
self.id = id
self.one_folder = one_folder

def run(self):
try:
Expand All @@ -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('完毕!')

Expand Down
20 changes: 13 additions & 7 deletions getComic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

#目标文件存在就跳过下载
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand All @@ -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+)*$')
Expand All @@ -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)

0 comments on commit 451adf3

Please sign in to comment.