-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebImage2zip.py
182 lines (141 loc) · 5.27 KB
/
webImage2zip.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
"""
webImage2zip.py
Downloads all the images on the supplied URL which is the first page of manga, comic.
and saves them to the specified directory as zip file.
Usage:
first argument : the url of the first page you want to downlaod.
comma separated input will be resolved as multiple input.
second argument : target directory.
python "www.abcd.com/some_comic_or_manga/1, www.abcd.com/other_comic_or_manga/1" "c://mangaOrComic/"
"""
from BeautifulSoup import BeautifulSoup as bs
import urlparse
from urllib2 import urlopen
import sys,getopt,os
from zipfile import ZipFile
from cStringIO import StringIO
from os import path
import string
class SiteCrawler():
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
def __init__(self, first_page_url):
"""it can be that constructor throw exception, if the given url is uncomphrensible"""
self.page_count = 1
self.first_page_url = first_page_url
self.curr_page_soup = bs(urlopen(first_page_url))
self.curr_page_url = first_page_url
self.parsed = list(urlparse.urlparse(first_page_url))
def _getImageSourceUrl(self):
"""must override """
pass
def _getNextPageUrl(self):
"""must override """
pass
def getTitle(self):
"""optional override """
return ''.join(c for c in self.parsed[2] if c in SiteCrawler.valid_chars)
def jumpToNextPage(self):
"""return a next page url string. if there are not, return None"""
url = self._getNextPageUrl()
if not url:
return None
self.curr_page_soup = bs(urlopen(url))
self.curr_page_url = url
self.page_count += 1
return self
def getCurrentPageNumber(self):
return self.page_count
def getFullImageUrl(self):
raw_src = self._getImageSourceUrl().lower()
if raw_src.startswith("http") or raw_src.startswith("https"):
return raw_src
else:
self.parsed[2] = raw_src
return urlparse.urlunparse(self.parsed)
def getAllImageUrl(self):
while(True):
yield self.getFullImageUrl()
if not self.jumpToNextPage():
break
class CwNoah(SiteCrawler):
def _getImageSourceUrl(self):
pass
def _getNextPageUrl(self):
pass
class CwH2R(SiteCrawler):
def _getImageSourceUrl(self):
pass
def _getNextPageUrl(self):
pass
def getTitle(self):
return "_".join(self.curr_page_url.split("/")[-3,-2])
class CwEH(SiteCrawler):
def _getImageSourceUrl(self):
pass
def _getNextPageUrl(self):
pass
class CwGM(SiteCrawler):
def _getImageSourceUrl(self):
pass
def _getNextPageUrl(self):
pass
class CwDummy(SiteCrawler):
def __init__(self):
SiteCrawler.__init__(self, "http://noah.kaist.ac.kr/Circle/HAJE/seminar/1d1p")
self.dummyUrl = ["/static/thumbs_desktop/547160/12_0.png",
"/static/thumbs_desktop/547160/12_1.png",
"/static/thumbs_desktop/547160/12_2.png"
]
def _getImageSourceUrl(self):
if self.page_count - 1 < self.dummyUrl.__len__():
return self.dummyUrl[self.page_count - 1]
def _getNextPageUrl(self):
if self.page_count < self.dummyUrl.__len__():
return self.first_page_url
def crawlImg2Zip(crawler, target_dir):
buf = StringIO()
zip_file = ZipFile(buf, mode='w')
for img_url in crawler.getAllImageUrl():
image = urlopen(img_url)
print "imgUrl : ", img_url
ext = img_url.split(".")[-1]
fname = path.basename("%03d.%s" % (crawler.getCurrentPageNumber(), ext))
zip_file.writestr(fname, image.read())
zip_file.close()
zip_file_name = "%s.zip" % crawler.getTitle()
output = open(os.path.join(target_dir, zip_file_name), 'wb')
output.write(buf.getvalue())
output.close()
buf.close()
def crawlerFactory(first_page_url):
if first_page_url.find("noah"):
return CwDummy()
else:
pass
from Tkinter import Tk
from tkFileDialog import askdirectory
import thread
def main(argv):
if len(argv) == 0:
Tk().withdraw()
print "interactive mode"
target_dir = askdirectory()
print "directory : ", target_dir
while True :
url = raw_input("give me url : ")
thread.start_new_thread(crawlImg2Zip, (crawlerFactory(url),target_dir) )
else:
target_urls = argv[0].strip().split(",")
if len(argv) == 2:
target_dir = argv[1]
else:
target_dir = os.getcwd()
for url in target_urls:
try:
crawlImg2Zip(crawlerFactory(url), target_dir)
except Exception as e:
print "FAIL : ", url
print e
#crawlImg2Zip(CwDummy(), out_folder)
if __name__ == "__main__":
main(sys.argv[1:])