Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, base64, wget, os, signal
- lines = sys.stdin.read().split('\n')
- class TimeoutException(Exception): # Custom exception class
- pass
- def timeout_handler(signum, frame): # Custom signal handler
- raise TimeoutException
- # Change the behavior of SIGALRM
- signal.signal(signal.SIGALRM, timeout_handler)
- count = 1
- fail_ls = []
- if not os.path.exists('re'):
- os.makedirs('re')
- for line in lines:
- if 'data:image' in line:
- with open("re/image_{}.jpg".format(count), "wb") as f:
- img_data = str.encode(line[23:])
- f.write(base64.decodebytes(img_data))
- else:
- signal.alarm(5)
- try:
- f = wget.download(line, out="re/image_{}.jpg".format(count))
- except TimeoutException:
- fail_ls.append(line)
- continue
- except:
- fail_ls.append(line)
- continue
- else:
- signal.alarm(0)
- count += 1
- for x in fail_ls:
- print('\n')
- print(x)
Add Comment
Please, Sign In to add comment