/
main.py
265 lines (194 loc) · 6.28 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding:utf-8 -*-
from gevent import monkey; monkey.patch_all()
import gevent
from utils import http_get, HttpReq
from bs4 import BeautifulSoup
import urllib2
import re, os, shutil
import utils
from operator import itemgetter, attrgetter
# import parseTuku as Parse
# import parseHentai as Parse
# conf = {
# "comic_name": u"政宗君的复仇",
# "comic_url": "/comic/10533/",
# }
# import parseBuka as Parse
import parse36mh
import parseManhuadui
# conf = {
# "comic_name": u"1年A班的Monster",
# "comic_url": "",
# "firs_chp_url":"/view/221736/65537",
# }
# conf = {
# "comic_name":u"黑色四叶草",
# "comic_url":"/manhua/heisesiyecao/",
# "Parse":parse36mh,
# }
conf = {
"comic_name":u"鬼灭之刃",
"comic_url":"/manhua/guimiezhiren/",
"Parse":parseManhuadui,
}
download_path = "comics"
import gevent
from gevent import Timeout
from gevent.pool import Pool
seconds = 20
# timeout = Timeout(seconds)
# timeout.start()
down_pool = utils.DownPool(100)
down_pool.reg_fun(utils.down_and_save)
pool = Pool(100)
class Comic(object):
def __init__(self, name, url, down_path,parse,first_chp_url=''):
self.chapters = [] #
self.name = name
self.url = url
self.first_chp_url = first_chp_url
self.comic_path = os.path.join(down_path, name)
self.Parse = parse
def load_chapters(self,start_id=0,end_id=10000000):
if self.url == "":
return self.load_chps_from_fist_chps()
chps = self.Parse.get_chapters(self.url)
# print chps
for chp_id,chp_url in chps.items():
if(int(chp_id) < start_id) or int(chp_id)>end_id:
continue
self.add_chapter(chp_id, chp_url)
self.chapters.sort(key=attrgetter("chp_id"))
# print self.chapters
# self.chapters = self.chapters[:2]
def load_chps_from_fist_chps(self):
http_req = HttpReq()
chps = self.Parse.get_chps_from_first_chp(self.first_chp_url,http_req)
for chp_id,item in chps.items():
self.add_chapter(chp_id,item["url"],item["imgs"])
self.chapters.sort(key=attrgetter("chp_id"))
# print self.chapters
def add_chapter(self,chp_id, chp_url,imgs=None):
self.chapters.append(Chapter(chp_id,chp_url,self.comic_path,self.Parse,imgs))
def download_all(self):
def _down(chp):
print "going to down chapter:%s"%chp.chp_id
chp.load()
print "load chp:%s,success."%chp.chp_id
chp.down_imgs()
print "down chp:%s,suceess."%chp.chp_id
pool.map(_down, self.chapters)
# gs = [gevent.spawn(_down, arg) for arg in self.chapters]
# gevent.joinall(gs)
class Chapter(object):
def __init__(self, chp_id, url, comic_path,parse,imgs=None):
self.chp_id = chp_id
self.url = url
self.imgs = [] if imgs is None else imgs # (name,url)
self.chp_path = os.path.join(comic_path,chp_id)
self.Parse = parse
def load(self):
if len(self.imgs) == 0:
self.imgs = self.Parse.get_imgs_url(self.url)
def down_imgs(self):
if not os.path.exists(self.chp_path):
os.makedirs(self.chp_path)
def _down(args):
img_name, img_url = args
img_path = os.path.join(self.chp_path, img_name)
if os.path.exists(img_path):
print "skip:%s" % img_path
return
utils.down_and_save2(img_url, img_path)
pool.map(_down, self.imgs)
# greents = [gevent.spawn(_down, *img) for img in self.imgs]
# print "going to joinall imgs:%s"%self.chp_id
# gevent.joinall(greents)
def __str__(self):
return self.chp_id+":,"+str(self.imgs)
# for img_name, img_url in self.imgs:
def download_chapter_by_gevent(imgs_url,dst_dir):
if not os.path.exists(dst_dir):
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for img_url in imgs_url:
img_name = img_url[img_url.rfind("/") + 1:]
img_path = os.path.join(dst_dir, img_name)
if os.path.exists(img_path):
print "skip:%s" % img_path
continue
down_pool.add_task(0, (img_url, img_path))
# greents.append(gevent.spawn(utils.down_and_save, img_url, img_path))
# gevent.joinall(greents)
def download_chapter(imgs_url,dst_dir):
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
# imgs_url = Parse.get_imgs_url(chp_url)
for img_url in imgs_url:
img_name = img_url[img_url.rfind("/") + 1:]
img_path = os.path.join(dst_dir, img_name)
if os.path.exists(img_path):
print "skip:%s" % img_path
continue
img_context = http_get(img_url)
with open(img_path,"wb") as f:
f.write(img_context)
print "download %s:%s" % (img_url, img_path)
def down_comic_by_gevent(comic_conf):
comic_name = comic_conf['comic_name']
comic_url = comic_conf['comic_url']
parse = comic_conf.get("parse") or Parse
chps_url = parse.get_chapters(comic_url)
chps_ids = chps_url.keys()
chps_ids.sort()
greents = []
down_count = 0
def down_chp(chp_id):
chp_url = chps_url[chp_id]
imgs = parse.get_imgs_url(chp_url)
dst_dir = os.path.join(download_path, comic_name, chp_id)
dowload_chapter_by_gevent(imgs, dst_dir)
utils.compress_folder(dst_dir, os.path.join(download_path, comic_name, "compress"))
down_pool.reg_fun(down_chp)
def start_pool():
down_pool.start()
#greents.append(gevent.spawn(start_pool))
g = gevent.spawn(start_pool)
# chps_ids = chps_ids[:4]
for chp_id in chps_ids:
#greents.append(gevent.spawn(down_chp, chp_id))
down_pool.add_task(1, (chp_id,))
g.join()
#gevent.joinall(greents)
def down_comic(comic_conf):
comic_name = comic_conf['comic_name']
comic_url = comic_conf['comic_url']
parse = comic_conf.get("parse") or Parse
chps_url = parse.get_chapters(comic_url)
chps_ids= chps_url.keys()
chps_ids.sort()
for chp_id in chps_ids:
chp_url = chps_url[chp_id]
imgs = parse.get_imgs_url(chp_url)
dst_dir = os.path.join(download_path, comic_name, chp_id)
#download_chapter(imgs, dst_dir)
dowload_chapter_by_gevent(imgs, dst_dir)
utils.compress_folder(dst_dir, os.path.join(download_path, comic_name, "compress"))
def make_zip(comic,dst,rewrite=False):
for chp in comic.chapters:
chp_dst = os.path.join(dst,comic.name)
chp_src = os.path.join(comic.comic_path,chp.chp_id)
utils.compress_folder(chp_src,chp_dst,rewrite)
def main():
import time
s_time = time.time()
# down_comic_by_gevent(conf)
comic = Comic(conf['comic_name'], conf['comic_url'], \
download_path, conf["Parse"],conf.get('firs_chp_url',""))
comic.load_chapters()
comic.download_all()
# make_zip(comic, "zips", True) # down_comic(conf)
e_time = time.time()
print "cost time:",e_time - s_time
if __name__ == "__main__":
main()