import os
import sys
import urllib2

from lxml import html

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

def main():
	""" ImageFap full gallery downloader
	"""

	if len(sys.argv) < 3:
		print 'ERROR: not enough arguments specified\n'
		print 'imgfap_dler.py <page.html> <magic num>'
		return -1

	input_file = sys.argv[1]
	magic_number = sys.argv[2]

	# Create list to hold all the image links
	image_links = []

	# Parse the inputfile
	print '\nParsing "{:s}"...'.format(input_file),
	tree = html.fromstring(open(input_file).read())

	# Iterate over all the links in the document and find all that link to 
	# full images
	for element, attribute, link, pos in tree.iterlinks():
		if link.startswith('/photo'):
			# Parse image number from link
			img_num = link[7:link[7:].find('/') + 7]

			# Create image download link
			image_link = 'http://fap.to/images/full/{:s}/{:s}/{:s}'\
						 '.jpg'.format(magic_number, img_num[:3], img_num)
			image_links.append(image_link)

	print 'DONE'

	total_images = len(image_links)
	print 'Found {:d} images to download\n'.format(total_images)

	# Loop through the list of images and download each
	for idx, image_link in enumerate(image_links):
		print 'Downloading image {:d} of {:d}...'.format(idx + 1, total_images),
		sys.stdout.flush()

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

		# Save the image to the desired location
		new_image_name = '{:003d}{:s}'.format(idx + 1, image_link[-4:])
		with open(os.path.join(IMAGE_SAVE_PATH, 
							   new_image_name), 'wb') as new_image:
			new_image.write(data)

		print 'DONE'
	

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