#!/usr/bin/env python3

try:
import urllib.request, sys, os

for i in range(1, len(sys.argv)):
urlfile = urllib.request.urlopen(sys.argv[i])
lines = str(urlfile.read()).split("\\n")
urlfile.close()

dfname = sys.argv[i].replace(".html", "").split("/")
dfname = dfname[len(dfname) - 1]
os.makedirs(dfname)
folder = dfname + os.sep

print("\n[", dfname , "]", sep="")

j = 0
while j < len(lines):
if "imageanchor" in lines[j]:
j += 1
else:
del lines[j]

links = []
for k in range(0, len(lines)):
strings = lines[k].split(" ")
l = 0
while l < len(strings):
if "href=" in strings[l]:
links.append(strings[l].replace("\"", "").replace("href=", ""))
l += 1
else:
del strings[l]

print()
current = 1
count = len(links)
for m in links:
name = m.split("/")
name = name[len(name) - 1]
print("    Getting image ", current, " of ", count, "    [", name ,"]", sep="")
imgfile = urllib.request.urlopen(m)
image = imgfile.read()
imgfile.close()
f = open(folder + str(current) + "_" + str(name), "wb")
f.write(image)
f.close()
current += 1

print()
i += 1

except KeyboardInterrupt:
print("\n\nAborted.\n")