Coding

“Python Learning: Automate Boring Stuff with Python | Chapter 11 : My Solution to Image Downloader from Imgur

So you want to download images without a browser.. 

challenge accepted 3:43:41 PM

 


#Image Site Downloader 
#USAGE: python igmurdownloader.py <category> <limit>

import sys, requests, os, logging, bs4, re

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")

logging.disable(logging.CRITICAL)
if len(sys.argv) == 3:
	logging.info("Command line argument 3")
	#TODO: get category, and limit 
	category = sys.argv[1]
	limit  = sys.argv[2]
	imgur = "https://imgur.com/"
	try:
		#TODO: make request to https://imgur.com/search?q=
		res = requests.get(imgur + "search?q=" + category)
		res.raise_for_status()
		logging.info("Request successful")
		imgur_soup = bs4.BeautifulSoup(res.text, "html.parser")
		#TODO: find all img tages 
		imgur_image_list = imgur_soup.select(".image-list-link > img")
		dir = None
		#CREATE DIRECTORY FOR IMAGES 
		if not os.path.exists( os.path.abspath("imgur_images") ):
			dir = os.mkdir("imgur_images")
		else: dir = os.path.abspath("imgur_images")
		#TODO: loop through tags and download into directory 
		iterator = 0
		while iterator < int(limit):
			image = imgur_image_list[iterator]
			#TODO GET SRC OF IMAGE 
			image_src = image.get("src")
			image_src = "http:" + image_src
			logging.info("Image Url: " + str( image_src ))
			#TODO: make request to source 
			try:
				image_res = requests.get(image_src)
				image_res.raise_for_status()
				image_ext = re.compile(r"(\w{3})$")
				image_ext = image_ext.search(image_src)
				ext = "jpg"
				if image_ext:
					ext = image_ext.groups()[0]
				image_file_name = "imgur_foto" + str(iterator) +  "." + ext
				#TODO: create file in 'wb' mode 
				image_file = open(os.path.join(dir, image_file_name), "wb")
				logging.info("File created: " + image_file_name)
				
				#TODO: write into file 
				for chunk in image_res.iter_content(10000):
					image_file.write(chunk)
				logging.info("Image has completed download")
			except Exception as err:
				logging.error("Image Download Error: " + str(  err ))
			iterator += 1
	except Exception as err:
		logging.error( str(  err ))
	
else:
	logging.warning("USAGE: python isd.py <category> <limit>")



And that is it! Improvement suggestions are welcomed!

error: Content is protected !!