Advertisement
heroys6

vk image saver

Dec 10th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. import re
  2. import sys
  3. import os
  4. import urllib.request
  5.  
  6. # Delay before exit
  7. def wait_and_exit():
  8.     wait = input("PRESS ENTER TO EXIT")
  9.     exit()
  10.  
  11. # Download single picture
  12. def download_pic(url, dir_name, num):
  13.     # regex
  14.     urllib.request.urlretrieve(url, "{0}/{1}.jpg".format(dir_name, num))
  15.  
  16. # Find unique directory name recursively
  17. def original_dir(dir_name, num = 0):
  18.     temp_name = dir_name if num == 0 else "{0}{1}".format(dir_name, num)
  19.        
  20.     if not os.path.exists(temp_name):
  21.         os.makedirs(temp_name)
  22.         return temp_name
  23.     return original_dir(dir_name, num + 1)
  24.  
  25. # Start:
  26.  
  27. # Enter file path by hand
  28. if (len(sys.argv) < 2):
  29.     print(' - Tip: You can drop *.html file on me\n')
  30.     file_path = input('Enter path to *.html file: ')
  31. # Read html file
  32. else:
  33.     file_path = sys.argv[1]
  34. # It can take a while
  35. print("Working...")
  36.  
  37. # Open & read file
  38. file_in = open(file_path, 'r')
  39. f_content = file_in.read()
  40.  
  41. if (f_content == ""):
  42.     print('Can\'t read from file\n')
  43.     file_in.close()
  44.     wait_and_exit()
  45. file_in.close()
  46.  
  47. # Create regex pattern and find links
  48. result = re.findall('https://\S+\.jpg', f_content)
  49.  
  50. if (result == []):
  51.     print('No links found in ' + file_path.split('\\')[-1])
  52.     wait_and_exit()
  53.  
  54. # Download found photos into auto-named folder
  55. dir_name = original_dir(sys.argv[0].split('.')[0])
  56. i = 1
  57.  
  58. for r in result:
  59.     download_pic(r, dir_name, i)
  60.     i = i + 1
  61.  
  62. print("Done. Check folder \"" + dir_name.split('\\')[-1] + "\"")
  63. wait_and_exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement