Guest User

Untitled

a guest
Nov 19th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. import sys, base64, wget, os, signal
  2. lines = sys.stdin.read().split('\n')
  3.  
  4. class TimeoutException(Exception): # Custom exception class
  5. pass
  6.  
  7. def timeout_handler(signum, frame): # Custom signal handler
  8. raise TimeoutException
  9.  
  10. # Change the behavior of SIGALRM
  11. signal.signal(signal.SIGALRM, timeout_handler)
  12.  
  13. count = 1
  14. fail_ls = []
  15. if not os.path.exists('re'):
  16. os.makedirs('re')
  17. for line in lines:
  18. if 'data:image' in line:
  19. with open("re/image_{}.jpg".format(count), "wb") as f:
  20. img_data = str.encode(line[23:])
  21. f.write(base64.decodebytes(img_data))
  22. else:
  23. signal.alarm(5)
  24. try:
  25. f = wget.download(line, out="re/image_{}.jpg".format(count))
  26. except TimeoutException:
  27. fail_ls.append(line)
  28. continue
  29. except:
  30. fail_ls.append(line)
  31. continue
  32. else:
  33. signal.alarm(0)
  34. count += 1
  35.  
  36. for x in fail_ls:
  37. print('\n')
  38. print(x)
Add Comment
Please, Sign In to add comment