Skip to content

Commit

Permalink
Fixed bug where files are left open (hardikvasa#74)
Browse files Browse the repository at this point in the history
* Fixed bug where files are not closed

* Reordered code so urlopen, response.read, and response.close are next to each other

* Close response sooner, easier for future error handling
  • Loading branch information
adayton1 authored and hardikvasa committed Apr 27, 2018
1 parent eccc6ac commit 9e0c215
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions google_images_download/google_images_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,30 @@ def single_image(self,image_url):
pass
req = Request(url, headers={
"User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"})

response = urlopen(req, None, 10)
data = response.read()
response.close()

image_name = str(url[(url.rfind('/')) + 1:])
if '?' in image_name:
image_name = image_name[:image_name.find('?')]
if ".jpg" in image_name or ".gif" in image_name or ".png" in image_name or ".bmp" in image_name or ".svg" in image_name or ".webp" in image_name or ".ico" in image_name:
output_file = open(main_directory + "/" + image_name, 'wb')
file_name = main_directory + "/" + image_name
else:
output_file = open(main_directory + "/" + image_name + ".jpg", 'wb')
file_name = main_directory + "/" + image_name + ".jpg"
image_name = image_name + ".jpg"

data = response.read()
output_file.write(data)
response.close()
try:
output_file = open(file_name, 'wb')
output_file.write(data)
except OSError as e:
raise e
except IOError as e:
raise e
finally:
output_file.close()

print("completed ====> " + image_name)
return

Expand Down Expand Up @@ -465,14 +476,25 @@ def download_image_thumbnail(self,image_url,main_directory,dir_name,return_image
timeout = float(socket_timeout)
else:
timeout = 10
response = urlopen(req, None, timeout)

path = main_directory + "/" + dir_name + " - thumbnail" + "/" + return_image_name
output_file = open(path, 'wb')
response = urlopen(req, None, timeout)
data = response.read()
output_file.write(data)
response.close()

path = main_directory + "/" + dir_name + " - thumbnail" + "/" + return_image_name

try:
output_file = open(path, 'wb')
output_file.write(data)
except OSError as e:
download_status = 'fail'
download_message = "OSError on an image...trying next one..." + " Error: " + str(e)
except IOError as e:
download_status = 'fail'
download_message = "IOError on an image...trying next one..." + " Error: " + str(e)
finally:
output_file.close()

download_status = 'success'
download_message = "Completed Image Thumbnail ====> " + return_image_name

Expand Down Expand Up @@ -515,7 +537,10 @@ def download_image(self,image_url,image_format,main_directory,dir_name,count,pri
timeout = float(socket_timeout)
else:
timeout = 10

response = urlopen(req, None, timeout)
data = response.read()
response.close()

# keep everything after the last '/'
image_name = str(image_url[(image_url.rfind('/')) + 1:])
Expand All @@ -536,16 +561,25 @@ def download_image(self,image_url,image_format,main_directory,dir_name,count,pri
prefix = ''

path = main_directory + "/" + dir_name + "/" + prefix + str(count) + ". " + image_name
output_file = open(path, 'wb')
data = response.read()
output_file.write(data)
response.close()

#return image name back to calling method to use it for thumbnail downloads
return_image_name = prefix + str(count) + ". " + image_name
try:
output_file = open(path, 'wb')
output_file.write(data)
except OSError as e:
download_status = 'fail'
download_message = "OSError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
except IOError as e:
download_status = 'fail'
download_message = "IOError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
finally:
output_file.close()

#return image name back to calling method to use it for thumbnail downloads
download_status = 'success'
download_message = "Completed Image ====> " + prefix + str(count) + ". " + image_name
download_message = "Completed Image ====> " + prefix + str(count) + ". " + image_name
return_image_name = prefix + str(count) + ". " + image_name

# image size parameter
if print_size:
Expand Down Expand Up @@ -575,6 +609,7 @@ def download_image(self,image_url,image_format,main_directory,dir_name,count,pri
download_status = 'fail'
download_message = "IOError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''

return download_status,download_message,return_image_name


Expand Down

0 comments on commit 9e0c215

Please sign in to comment.