def save_img(row): prop_id, photo_id, photo_url = row # create folder for hotel hotel_path = join(IMAGES_PATH, 'amadeus/%s' % str(prop_id)) if not exists(hotel_path): makedirs(hotel_path) fpath = join(hotel_path, "%s.jpg" % photo_id) photo_url = "http:%s" % photo_url success = False attempts = 2 while attempts and not success: attempts -= 1 try: urllib.urlretrieve(photo_url, fpath) # check that a valid image was downloaded # by trying to open it with PIL im = Image.open(fpath) success = True except Exception as e: # remove possible corrupted file if exists(fpath): remove(fpath) # sleep(1) if success: return photo_id, photo_url, fpath, True else: return photo_id, photo_url, fpath, False
def download(info): ## todo: get download path as param try: name = "./dl/%s - %s.mp3" % (info['artist'], info['title']) urllib.urlretrieve(info['url'], name) # set_tags(name, info) return name, info except Exception as e: print("Some goes wrong ( ._.) %s" % e.message) return None, None
def save_img(uf): url, fpath = uf success = False attempts = 2 while attempts and not success: attempts -= 1 try: urllib.urlretrieve(url, fpath) # check that a valid image was downloaded # by trying to open it with PIL im = Image.open(fpath) success = True except Exception as e: # remove possible corrupted file if exists(fpath): remove(fpath) # sleep(1) if success: return url, True else: return url, False
return os.path.join(self.img_dir, filename) def fetch_image(self, images_dict, parent_id): image_field = images_dict.get('image') try: url = self._original_image_url_from_image_field(image_field) except (KeyError, TypeError) as e: self.logger.exception(e) else: try: filepath = self._image_filepath_from_url(url, parent_id) except ValueError, e: self.logger.exception(e) else: try: output_filepath, headers = urllib.urlretrieve( url, filepath) except Exception, e: self.logger.exception(e) else: return dict(id=parent_id, filepath=output_filepath, url=url) class MockAPI(API): def fetch_image(self, images_dict, parent_id): image_field = images_dict.get('image') try: url = self._original_image_url_from_image_field(image_field) except (KeyError, TypeError) as e: self.logger.exception(e)
return os.path.join(self.img_dir, filename) def fetch_image(self, images_dict, parent_id): image_field = images_dict.get('image') try: url = self._original_image_url_from_image_field(image_field) except (KeyError, TypeError) as e: self.logger.exception(e) else: try: filepath = self._image_filepath_from_url(url, parent_id) except ValueError, e: self.logger.exception(e) else: try: output_filepath, headers = urllib.urlretrieve(url, filepath) except Exception, e: self.logger.exception(e) else: return dict(id=parent_id, filepath=output_filepath, url=url) class MockAPI(API): def fetch_image(self, images_dict, parent_id): image_field = images_dict.get('image') try: url = self._original_image_url_from_image_field(image_field) except (KeyError, TypeError) as e: self.logger.exception(e) else: try: