import os
import re
import sys
import urllib2

# --- DESCRIPTION ---
# A script for circumventing the image "protection" from http://celebforum.to/
# specifically for imagevenue.com links (will also support imagebam in the future)
# 
# USAGE:
# python grabber.py <html source of gallery>


# http://img184.imagevenue.com/loc663/th_74708_0005_blood_orange_dress05_123_663lo.jpg
# http://img184.imagevenue.com/img.php?loc=loc663&image=74708_0005_blood_orange_dress05_123_663lo.jpg
IMG_PAGE_RE = r'"http://(img\d{3})\.imagevenue\.com/(loc\d{3})/(.+?\.jpg)"'

IMG_SRC_RE = r'<img id="thepic" .+? SRC="(.+?)"'

# Location where the images should be saved
IMAGE_SAVE_PATH = 'images'

def main():

	if len(sys.argv) < 2:
		print 'Not enough arguments specified!'
		return -1

	# Iterate over the input source file and look for all thumbnail image matches
	matches = []
	with open(sys.argv[1], 'r') as input_file:
		for line in input_file:
			line_matches = re.findall(IMG_PAGE_RE, line)
			for line_match in line_matches:
				matches.append(line_match)

	# Iterate over each match and build the correct image page URL
	# Major thanks to http://vintage-erotica-forum.com/showthread.php?p=694632
	# for explaining the secret sauce!
	total = len(matches)
	for idx, match in enumerate(matches):
		server = 'http://{:s}.imagevenue.com/'.format(match[0])
		image_name = match[2][3:]
		image_loc = 'img.php?loc={:s}&image={:s}'.format(match[1], image_name)
		link = '{:s}{:s}'.format(server, image_loc)

		# Open the image page and download the source HTML
		response = urllib2.urlopen(link)
		html = response.read()

		# Iterate over the source HTML looking for the image source link
		for line in html.split('\n'):
			img = re.search(IMG_SRC_RE, line)
			if img:
				location = '{:s}{:s}'.format(server, img.group(1))
				print '{:3d} of {:3d} - {:s}'.format(idx + 1, total, location)

				# Download the freakin image!
				try:
					response = urllib2.urlopen(location, timeout=4)
					data = response.read()
				except:
					print 'Unable to download: {:s}'.format(location)
					continue


				# Save the image to the desired location
				with open(os.path.join(IMAGE_SAVE_PATH, image_name), 'wb') as new_image:
					new_image.write(data)


	print '\n\n'




if __name__ == '__main__':
	sys.exit(main())