-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg-fetcher.py
29 lines (26 loc) · 936 Bytes
/
img-fetcher.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
import sys
import re
import os
import urllib.request
import shutil
allowedExt = ["png", "jpg", "jpeg", "svg"]
for data in sys.stdin:
urls = re.findall(r'\"(https?://[^"]+)"', data)
if not os.path.exists('./output'):
os.mkdir('output')
if len(urls):
for url in urls:
if len(url):
try:
lastDot = url.rindex('.')
ext = url[lastDot+1:]
lastSlash = url.rindex('/')
imageName = url[lastSlash+1:]
endpoint = os.getcwd() + "/output/" + imageName
if not os.path.exists(endpoint) and ext in allowedExt:
filename, headers = urllib.request.urlretrieve(url)
shutil.move(filename, endpoint)
print(imageName)
except ValueError:
print(url)
pass