1
0
mirror of https://github.com/uvoteam/img-fetcher.git synced 2024-07-24 02:42:26 +00:00
img-fetcher/img-fetcher.py
2019-03-14 17:19:05 +02:00

30 lines
936 B
Python

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