Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import sys
- import os
- import urllib.request
- # Delay before exit
- def wait_and_exit():
- wait = input("PRESS ENTER TO EXIT")
- exit()
- # Download single picture
- def download_pic(url, dir_name, num):
- # regex
- urllib.request.urlretrieve(url, "{0}/{1}.jpg".format(dir_name, num))
- # Find unique directory name recursively
- def original_dir(dir_name, num = 0):
- temp_name = dir_name if num == 0 else "{0}{1}".format(dir_name, num)
- if not os.path.exists(temp_name):
- os.makedirs(temp_name)
- return temp_name
- return original_dir(dir_name, num + 1)
- # Start:
- # Enter file path by hand
- if (len(sys.argv) < 2):
- print(' - Tip: You can drop *.html file on me\n')
- file_path = input('Enter path to *.html file: ')
- # Read html file
- else:
- file_path = sys.argv[1]
- # It can take a while
- print("Working...")
- # Open & read file
- file_in = open(file_path, 'r')
- f_content = file_in.read()
- if (f_content == ""):
- print('Can\'t read from file\n')
- file_in.close()
- wait_and_exit()
- file_in.close()
- # Create regex pattern and find links
- result = re.findall('https://\S+\.jpg', f_content)
- if (result == []):
- print('No links found in ' + file_path.split('\\')[-1])
- wait_and_exit()
- # Download found photos into auto-named folder
- dir_name = original_dir(sys.argv[0].split('.')[0])
- i = 1
- for r in result:
- download_pic(r, dir_name, i)
- i = i + 1
- print("Done. Check folder \"" + dir_name.split('\\')[-1] + "\"")
- wait_and_exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement