Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code readability #330

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions google_images_download/google_images_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,16 @@ def get_all_tabs(self,page):

#Format the object in readable format
def format_object(self,object):
formatted_object = {}
formatted_object['image_format'] = object['ity']
formatted_object['image_height'] = object['oh']
formatted_object['image_width'] = object['ow']
formatted_object['image_link'] = object['ou']
formatted_object['image_description'] = object['pt']
formatted_object['image_host'] = object['rh']
formatted_object['image_source'] = object['ru']
formatted_object['image_thumbnail_url'] = object['tu']
formatted_object = {
'image_format': object['ity'],
'image_height': object['oh'],
'image_width': object['ow'],
'image_link': object['ou'],
'image_description': object['pt'],
'image_host': object['rh'],
'image_source': object['ru'],
'image_thumbnail_url': object['tu'],
}
return formatted_object


Expand Down Expand Up @@ -322,7 +323,7 @@ def single_image(self,image_url):
print("completed ====> " + image_name.encode('raw_unicode_escape').decode('utf-8'))
return

def similar_images(self,similar_images):
def get_similar_images(self,similar_images):
version = (3, 0)
cur_version = sys.version_info
if cur_version >= version: # If the Current Version of Python is 3.0 or above
Expand Down Expand Up @@ -426,7 +427,7 @@ def build_search_url(self,search_term,params,url,similar_images,specific_site,sa
url = url
elif similar_images:
print(similar_images)
keywordem = self.similar_images(similar_images)
keywordem = self.get_similar_images(similar_images)
url = 'https://www.google.com/search?q=' + keywordem + '&espv=2&biw=1366&bih=667&site=webhp&source=lnms&tbm=isch&sa=X&ei=XosDVaCXD8TasATItgE&ved=0CAcQ_AUoAg'
elif specific_site:
url = 'https://www.google.com/search?q=' + quote(
Expand Down Expand Up @@ -586,6 +587,7 @@ def download_image(self,image_url,image_format,main_directory,dir_name,count,pri
return "success", "Skipping image download...", str(image_url[(image_url.rfind('/')) + 1:]), image_url
if no_download:
return "success","Printed url without downloading",None,image_url
error_raised = False
try:
req = Request(image_url, headers={
"User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"})
Expand Down Expand Up @@ -660,50 +662,40 @@ def download_image(self,image_url,image_format,main_directory,dir_name,count,pri
print("Image Size: " + str(self.file_size(path)))

except UnicodeEncodeError as e:
download_status = 'fail'
download_message = "UnicodeEncodeError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except URLError as e:
download_status = 'fail'
download_message = "URLError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except BadStatusLine as e:
download_status = 'fail'

download_message = "BadStatusLine on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except HTTPError as e: # If there is any HTTPError
download_status = 'fail'
download_message = "HTTPError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except URLError as e:
download_status = 'fail'
download_message = "URLError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except ssl.CertificateError as e:
download_status = 'fail'
download_message = "CertificateError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except IOError as e: # If there is any IOError
download_status = 'fail'
download_message = "IOError on an image...trying next one..." + " Error: " + str(e)
return_image_name = ''
absolute_path = ''
error_raised = True

except IncompleteRead as e:
download_status = 'fail'
download_message = "IncompleteReadError on an image...trying next one..." + " Error: " + str(e)
error_raised = True

if error_raised is True:
download_status = 'fail'
return_image_name = ''
absolute_path = ''

Expand Down