-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmangagrab.py
executable file
·360 lines (323 loc) · 12.8 KB
/
mangagrab.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/python
from threading import Thread, activeCount
import gtk
import time
from gobject import threads_init, idle_add
import re
import urllib2
from urllib import urlencode
from sgmllib import SGMLParser
import os
from reportlab.platypus import SimpleDocTemplate, Image
import Image as PImage
import shutil
import gzip
from StringIO import StringIO
threads_init()
class MainWindow(gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.checked = False
self.connect("destroy", self.do_exit)
self.set_size_request(400, 150)
self.set_position(gtk.WIN_POS_CENTER)
self.set_title('MangaFox Grabber')
try:
self.set_icon_from_file('logo.png')
except:
pass
halign = {}
vbox = gtk.VBox(False, 2)
hbox = {}
self.progress = gtk.ProgressBar()
self.progress.set_text("Press Check, when ready.")
self.button = gtk.Button('Check')
self.button.connect('clicked', self.on_continue)
self.help = gtk.Button('How to use this application?')
self.help.connect('clicked', self.on_help)
self.entry = gtk.Entry()
self.entrylabel = gtk.Label('Manga')
self.threads = gtk.combo_box_new_text()
for sel in ['5 threads(slowest, most stable)',
'10 threads(recommended)',
'20 threads',
'50 threads (fastest, least stable)']:
self.threads.append_text(sel)
self.threads.set_active(1)
self.selection = gtk.combo_box_new_text()
for sel in ['A5 page size',
'A4 page size',
'A3 page size']:
self.selection.append_text(sel)
self.selection.set_active(0)
widgs = [[self.progress],
[self.threads],
[self.selection],
[self.entrylabel, self.entry],
[self.button, self.help], ]
for key, widgets in enumerate(widgs):
halign[key] = gtk.Alignment(1, 0, 1, 0)
hbox[key] = gtk.HBox(False, 2)
for widget in widgets:
hbox[key].add(widget)
halign[key].add(hbox[key])
vbox.pack_start(halign[key], False, False, 1)
self.add(vbox)
self.show_all()
def on_continue(self, widget):
##Block buttons
self.button.set_sensitive(False)
##Do Step 2 if done.
if self.checked:
self.progress.set_fraction(0)
step = StepTwoContainer(self.links)
step.start()
return True
##Set URL fetching thingy.
self.headers = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13'}
self.urllib = urlencode({'name': 'Grabber', 'location': 'Hell', 'language': 'Python', 'port': 80, 'timeout': 10})
self.regex = re.compile('http://[a-zA-Z0-9.-_]*/store/manga/[0-9]*/[0-9-.]*/compressed/[a-zA-Z0-9._-]*.jpg')
##Set application cache
self.links = []
self.images = {}
self.temps = []
self.seriesLen = 0
##Set some healthy variables
self.currentDir = os.getcwd()
self.force = False
self.completed = 0
##Get user input
self.series = self.entry.get_text()
self.maxThreads = self.get_threads()
self.psize = self.get_psize()
step = StepOne()
step.start()
def on_help(self, widget):
from webbrowser import open as webopen
webopen('https://github.com/simukis/Manga-Fox-Grabber/blob/master/HOWTO')
def get_threads(self):
active = self.threads.get_active()
if active == 0:
return 5
elif active == 1:
return 10
elif active == 2:
return 20
elif active == 3:
return 30
else:
return 10
def get_psize(self):
from reportlab.lib.pagesizes import A3, A4, A5
active = self.selection.get_active()
if active == 0:
return A5
elif active == 1:
return A4
elif active == 2:
return A3
else:
return A5
def get_author(self):
try:
url = 'http://www.mangafox.com/manga/'+self.series+'/?no_warning=1'
request = urllib2.Request(url, self.urllib, self.headers)
content = urllib2.urlopen(request).read()
a=re.compile('<th>Author\(s\)</th>(.*?)<td><a href="/search/author/(.*?)/" style="color:#666">(.*?)</a></td>', re.MULTILINE| re.DOTALL)
return a.search(content).group(3)
except:
return 'Unknown'
def do_exit(self, widget):
os._exit(0)
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []
def start_a(self, attrs):
href = [v for k, v in attrs if k=='href']
if href:
self.urls.extend(href)
class StepOne(Thread):
def __init__(self):
super(StepOne, self).__init__()
def update_status(self, value=None, text=None):
if value != None:
interface.progress.set_fraction(value)
if text != None:
interface.progress.set_text(text)
return False
def run(self):
try:
idle_add(self.update_status, 0, 'Fetching author')
interface.author = interface.get_author()
idle_add(self.update_status, 0.05, 'Fetching page')
url = 'http://www.mangafox.com/manga/'+interface.series+'/?no_warning=1'
request = urllib2.Request(url, interface.urllib, interface.headers)
content = urllib2.urlopen(request).read()
idle_add(self.update_status, 0.2, 'Processing page')
parser = URLLister()
parser.feed(content)
linkslen = len(parser.urls)
currentFraction = 0.2
for link in parser.urls:
if interface.series in link and not "rss" in link and not link in interface.links and '1.html' in link:
interface.links.append(link)
currentFraction+=0.799/linkslen
idle_add(self.update_status, currentFraction)
interface.links.reverse()
idle_add(self.update_status, 1, 'Done. Press Download to proceed.')
interface.button.set_label('Download')
interface.checked = True
interface.seriesLen = len(interface.links)
except:
idle_add(self.update_status, 0, 'Cannot correctly get info. Check manga field.')
finally:
interface.button.set_sensitive(True)
class StepTwoContainer(Thread):
def __init__(self, links):
super(StepTwoContainer, self).__init__()
self.links = links
def update_status(self, value=None, text=None):
if value != None:
interface.progress.set_fraction(value)
if text != None:
interface.progress.set_text(text)
return False
def run(self):
thrds = []
for link in self.links:
while activeCount() >= interface.maxThreads:
time.sleep(0.1)
step = StepTwo(link)
thrds.append(step)
step.start()
for thrd in thrds:
thrd.join()
for dire in interface.temps:
shutil.rmtree(dire)
idle_add(self.update_status, 1, 'Done, do anything else if you want.')
interface.checked=False
interface.button.set_label('Check')
interface.button.set_sensitive(True)
class StepTwo(Thread):
def __init__(self, link):
super(StepTwo, self).__init__()
self.link = link
self.images = []
def update_status(self, value=None, text=None):
if value != None:
interface.progress.set_fraction(value)
if text != None:
interface.progress.set_text(text)
return False
def run(self):
linkbundle = self.link.split('/')[-3:][:2]
if linkbundle[0] == interface.series:
linkbundle[0] = '/'
else:
linkbundle[0] = '/'+linkbundle[0]+'/'
##Check if not downloaded already.
try:
chapters = os.listdir(interface.series)
except:
chapters = []
if linkbundle[1]+'.pdf' in chapters and not interface.force:
interface.completed = interface.completed+1.0/interface.seriesLen
idle_add(self.update_status, interface.completed, 'Chapter '+linkbundle[1].strip('c')+' was found downloaded.')
return True
##Completed that chapter!
idle_add(self.update_status, None, 'Start reading chapter '+linkbundle[1].strip('c'))
for page in range(1, 1000):
url = 'http://mangafox.me/manga/'+interface.series+linkbundle[0]+linkbundle[1]+'/'+str(page)+'.html'
request = urllib2.Request(url, interface.urllib, interface.headers)
try:
response = urllib2.urlopen(request)
content = response.read()
if response.info().get('Content-Encoding') == 'gzip':
content = gzip.GzipFile(fileobj=StringIO(content)).read()
except Exception as e:
print 'Could not get html for chapter '+linkbundle[1]+' page '+str(page)
print 'Exception: {0}'.format(e)
continue
try:
image=interface.regex.search(content).group(0)
if not image in self.images:
self.images.append(image)
else:
break ##Chapter END
except:
print 'Could not get image for chapter '+linkbundle[1]+' page '+str(page)
break ##Could not get image!
interface.completed = interface.completed+(0.25/interface.seriesLen)
idle_add(self.update_status, interface.completed, 'Downloading chapter '+linkbundle[1].strip('c'))
###
##Downloading images.
###
chapterLen = len(self.images)
if chapterLen < 2:
interface.completed = interface.completed+(0.75/interface.seriesLen)
idle_add(self.update_status, interface.completed, 'Done chapter '+linkbundle[1].strip('c'))
return True
try:
os.mkdir(interface.series)
except:
pass
try:
os.mkdir(os.path.join(interface.series, linkbundle[1]))
except:
pass
for image in self.images:
imagename=image.split('/')[-1]
img = open(os.path.join(interface.series, linkbundle[1], imagename), 'w')
img.write(urllib2.urlopen(image).read())
img.close()
interface.completed = interface.completed+(0.5/interface.seriesLen/chapterLen)
idle_add(self.update_status, interface.completed, None)
###
##Making PDF
###
c=SimpleDocTemplate(os.path.join(interface.series, linkbundle[1]+'.pdf'),
pagesize=interface.psize,
rightMargin=0,
leftMargin=0,
topMargin=0,
bottomMargin=0)
Story=[]
maxh = interface.psize[1]-20
maxw = interface.psize[0]-30
title=' '.join(interface.series.split('_'))
c.title=title+' '+linkbundle[1]
c.author=interface.author
directory=os.path.join(interface.series, linkbundle[1])
images=sorted(os.listdir(directory))
for image in images:
img = PImage.open(os.path.join(directory, image))
width, height = img.size
img = img.crop(( 0, 0, width, height-40))
img.save(os.path.join(directory, image))
img = PImage.open(os.path.join(directory, image))
width, height = img.size
if width/maxw>height/maxh:
height=height/(width/maxw)
width=maxw
if width>height:
img = img.rotate(90)
img.save(os.path.join(directory, image))
width, height = img.size
if width/maxw>height/maxh:
height=height/(width/maxw)
width=maxw
else:
width=width/(height/maxh)
height=maxh
else:
width=width/(height/maxh)
height=maxh
im = Image(os.path.join(directory, image), width, height)
Story.append(im)
c.build(Story)
interface.completed = interface.completed+(0.2499/interface.seriesLen)
idle_add(self.update_status, interface.completed, 'Done chapter '+linkbundle[1].strip('c'))
interface.temps.append(os.path.join(interface.series, linkbundle[1]))
interface = MainWindow()
gtk.main()