-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmomentum_spider.py
52 lines (39 loc) · 1.09 KB
/
momentum_spider.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
# coding: utf-8
import re
import os
import urllib
from multiprocessing.pool import ThreadPool
MOMENTUM_URL = 'https://d3cbihxaqsuq0s.cloudfront.net/'
def spider():
context = urllib.urlopen(MOMENTUM_URL).read()
return context
def parse_context(c):
image_keys = re.findall(r'\<Key\>(.+?)\<\/Key\>', c)
return [
MOMENTUM_URL + key for key in image_keys
]
def main():
context = spider()
images = parse_context(context)[1:]
pool = ThreadPool(6)
pool.map(save_img, images)
pool.close()
pool.join()
def save_img(img_url, file_path='/Users/ziv/Downloads/momentums'):
try:
if not os.path.exists(file_path):
os.makedirs(file_path)
file_suffix = os.path.basename(img_url)
filename = '{}{}{}'.format(file_path, os.sep, file_suffix)
urllib.urlretrieve(img_url, filename=filename)
print filename
except IOError as e:
print e
except Exception as e:
print e
if __name__ == '__main__':
import time
s = time.time()
main()
print 'finished.'
print time.time() - s