forked from YoongiKim/AutoCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_crawler.py
212 lines (164 loc) · 7.55 KB
/
auto_crawler.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
"""
Copyright 2018 YoongiKim
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import requests
import shutil
from multiprocessing import Pool
import argparse
import google
import naver
class AutoCrawler:
def __init__(self, skip_already_exist=True, n_threads=4, do_google=True, do_naver=True, download_path='download'):
"""
:param skip_already_exist: Skips keyword already downloaded before. This is needed when re-downloading.
:param n_threads: Number of threads to download.
:param do_google: Download from google.com (boolean)
:param do_naver: Download from naver.com (boolean)
"""
self.skip = skip_already_exist
self.n_threads = n_threads
self.do_google = do_google
self.do_naver = do_naver
self.download_path = download_path
@staticmethod
def all_dirs(path):
paths = []
for dir in os.listdir(path):
if os.path.isdir(path + '/' + dir):
paths.append(path + '/' + dir)
return paths
@staticmethod
def all_files(path):
paths = []
for root, dirs, files in os.walk(path):
for file in files:
if os.path.isfile(path + '/' + file):
paths.append(path + '/' + file)
return paths
@staticmethod
def make_dir(dirname):
current_path = os.getcwd()
path = os.path.join(current_path, dirname)
if not os.path.exists(path):
os.makedirs(path)
@staticmethod
def get_keywords(keywords_file='keywords.txt'):
# read search keywords from file
with open(keywords_file, 'r', encoding='utf-8-sig') as f:
text = f.read()
lines = text.split('\n')
if '' in lines:
lines.remove('')
keywords = sorted(set(lines))
print('{} keywords found: {}'.format(len(keywords), keywords))
# re-save sorted keywords
with open(keywords_file, 'w+', encoding='utf-8') as f:
for keyword in keywords:
f.write('{}\n'.format(keyword))
return keywords
def save_image_to_file(self, image, file_path):
try:
with open('{}'.format(file_path), 'wb') as file:
shutil.copyfileobj(image.raw, file)
except Exception as e:
print('Save failed - {}'.format(e))
def download_images(self, keyword, links, site):
self.make_dir('{}/{}'.format(self.download_path, keyword))
total = len(links)
for index, link in enumerate(links):
try:
print('Downloading {} from {}: {} / {}'.format(keyword, site, index+1, total))
response = requests.get(link, stream=True)
self.save_image_to_file(response, '{}/{}/{}_{}.jpg'.format(self.download_path, keyword, site, index))
del response
except Exception as e:
print('Download failed - ', e)
continue
def download_from_site(self, keyword, site, collect_links_func):
try:
dirname = '{}/{}'.format(self.download_path, keyword)
if os.path.exists(os.path.join(os.getcwd(), dirname)) and self.skip:
print('Skipping already existing directory {}'.format(dirname))
return
print('Collecting links... {} from {}'.format(keyword, site))
links = collect_links_func(keyword)
print('Downloading images from collected links... {} from {}'.format(keyword, site))
self.download_images(keyword, links, site)
print('Done {} : {}'.format(site, keyword))
except Exception as e:
print('Exception {} - {}'.format(keyword, e))
def download(self, args):
if args[1] == 'google':
self.download_from_site(keyword=args[0], site=args[1], collect_links_func=google.collect_links)
elif args[1] == 'naver':
self.download_from_site(keyword=args[0], site=args[1], collect_links_func=naver.collect_links)
def do_crawling(self):
keywords = self.get_keywords()
tasks = []
for keyword in keywords:
if self.do_google:
tasks.append([keyword, 'google'])
if self.do_naver:
tasks.append([keyword, 'naver'])
pool = Pool(self.n_threads)
pool.map_async(self.download, tasks)
pool.close()
pool.join()
print('pool join')
self.integrity_check()
def integrity_check(self):
print('Integrity Checking...')
dict_num_files = {}
for dir in self.all_dirs(self.download_path):
n_files = len(self.all_files(dir))
dict_num_files[dir] = n_files
avg = 0
for dir, n_files in dict_num_files.items():
avg += n_files / len(dict_num_files)
print('dir: {}, file_count: {}'.format(dir, n_files))
dict_too_small = {}
for dir, n_files in dict_num_files.items():
if n_files < avg * 0.5:
dict_too_small[dir] = n_files
if len(dict_too_small) >= 1:
for dir, n_files in dict_too_small.items():
print('_________________________________')
print('Below keywords have smaller than 50% of average file count.')
print('I recommend you to remove these directories and re-download for that keyword.')
print('_________________________________')
print('Too small file count directories:')
print('dir: {}, file_count: {}'.format(dir, n_files))
print("Remove directories above? (y/n)")
answer = input()
if answer == 'y':
# removing directories too small files
print("Removing too small file count directories...")
for dir, n_files in dict_too_small.items():
shutil.rmtree(dir)
print('Removed {}'.format(dir))
print('Now re-run this program to re-download removed files. (with skip_already_exist=True)')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--skip', type=str, default='true',
help='Skips keyword already downloaded before. This is needed when re-downloading.')
parser.add_argument('--threads', type=int, default=4, help='Number of threads to download.')
parser.add_argument('--google', type=str, default='true', help='Download from google.com (boolean)')
parser.add_argument('--naver', type=str, default='true', help='Download from naver.com (boolean)')
args = parser.parse_args()
_skip = False if str(args.skip).lower() == 'false' else True
_threads = args.threads
_google = False if str(args.google).lower() == 'false' else True
_naver = False if str(args.naver).lower() == 'false' else True
print('Options - skip:{}, threads:{}, google:{}, naver:{}'.format(_skip, _threads, _google, _naver))
crawler = AutoCrawler(skip_already_exist=_skip, n_threads=_threads, do_google=_google, do_naver=_naver)
crawler.do_crawling()