def download_album(album_name, path, resume = False, verbose = False): """Download an album from smugmug to the local path""" if album_name == None: raise "Error: Album name not specified" sys.exit(1) if path == None: raise "Error: Path is not specified" sys.exit(1) smugmug = SmugMug(verbose) print "#### Processing album: " + album_name album_id = smugmug.get_album_id(album_name) if album_id == None: raise "Error: Album does not exist at SmugMug. Skipping." # Creating the album album_path = os.path.join(path, album_name) if os.path.exists(album_path): if resume == False: print "Error: Local album \'" + album_name + "\' already exists and resuming is not enabled." sys.exit(1) else: print "Local album already exists. Adding images." else: print "Creating new local album." os.makedirs(album_path) images_info = smugmug.get_album_images_info(album_id) count = 0 total = len(images_info) for image_info in images_info: count += 1 image_url = image_info['original_url'] image_name = image_info['name'] image_path = os.path.join(album_path, image_name) sys.stdout.write('Downloading ' + album_name.encode('utf-8') + '/' + image_name + ' [' + str(count) + '/' + str(total) + ']... ') sys.stdout.flush() if verbose == True: print '' if os.path.exists(image_path): if resume == True: print "Already exists. Skipping." else: raise Exception("Error: File already exists and resuming is not enables.") else: smugmug.download_image(image_info = image_info, image_path = image_path) print "Done" print "Done downloading album '" + album_name + "'"
def test_album_creation(self): smugmug = SmugMug(verbose=True) albums = smugmug.get_album_names() # Let's create a name that doesn't exist yet album_name = "SmugMug Test Album " index = 1 while (album_name + str(index)) in albums: index += 1 album_name += str(index) # Selecting the last category category_name = None for category in smugmug.get_category_names(): category_name = category # Selecting the last template template_name = None for template in smugmug.get_template_names(): template_name = template # Setting a password password = "******" # Getting category id category_id = smugmug.get_category_id(category_name) self.assertTrue(category_name == None or int(category_id) > 0) # Getting template id template_id = smugmug.get_template_id(template_name) self.assertTrue(template_name == None or int(template_id) > 0) # Creating a new album smugmug.create_album(album_name, category_id=category_id, template_id=template_id, password=password) # Checking that the album was created albums = smugmug.get_album_names() self.assertTrue(album_name in albums) # Getting the id of the new album album_id = smugmug.get_album_id(album_name) self.assertTrue(album_id != None) self.assertTrue(int(album_id) > 0) # Checking properties album_info = smugmug.get_album_info(album_id) self.assertTrue(album_info["album_name"] == album_name) self.assertTrue(album_info["album_id"] == album_id) self.assertTrue(album_info["category_id"] == category_id) self.assertTrue(album_info["category_name"] == category_name) ## self.assertTrue(album_info['password'] == password) ## Apparently password is not returned by the request any more # Let's load the sample photo image_path = "sampleimage.jpg" try: image_data = open(image_path, "rb").read() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) raise image_name = os.path.basename(image_path) image_type = mimetypes.guess_type(image_path)[0] smugmug.upload_image(image_data=image_data, image_name=image_name, image_type=image_type, album_id=album_id) # Now let's check that the image is in the album album_images = smugmug.get_album_images(album_id) self.assertTrue(len(album_images) == 1) self.assertTrue(image_name in album_images) images_info = smugmug.get_album_images_info(album_id) self.assertTrue(len(images_info) == 1) # Have to wait a bit time.sleep(20) # Dowloading the image image_path2 = "sampleimage2.jpg" smugmug.download_image(image_info=images_info[0], image_path=image_path2) # Checking that the new image is same as the old one try: image_data2 = open(image_path2, "rb").read() except IOError as e: raise "I/O error({0}): {1}".format(e.errno, e.strerror) self.assertTrue(image_data == image_data2)
def upload_images(album_name, category_name, template_name, password, resume, verbose, image_paths): if album_name == None: print 'Error: Album name not specified.' sys.exit(1) if len(image_paths) == 0: print 'Error: No images specified for uploading.' sys.exit(1) smugmug = SmugMug(verbose=verbose) # Finding / creating the album album_id = smugmug.get_album_id(album_name) if album_id == None: # Getting category id category_id = None if category_name != None: category_id = smugmug.get_category_id(category_name) if category_id == None: print 'Error: Could not find category named \'' + category_name + '\'' sys.exit(1) # Getting template id template_id = None if template_name != None: template_id = smugmug.get_template_id(template_name) if template_id == None: print 'Error: Could not find album template named \'' + template_name + '\'' sys.exit(1) # Creating a new album smugmug.create_album(album_name = album_name, category_id = category_id, template_id = template_id, password = password) print 'Created new album \''+album_name+'\'' album_id = smugmug.get_album_id(album_name) elif album_id != None and resume == True: print 'Uploading to existing album \'' + album_name + '\'' else: print 'Error: Album already exists. Use \'--resume\' to add to existing album.' sys.exit(1) # Uploading the images total = len(image_paths) count = 0 album_images = smugmug.get_album_images(album_id) for image_path in image_paths: if verbose == True: print '----------------------------------------------------' count += 1 image_name = os.path.basename(image_path) sys.stdout.write('Uploading ' + album_name + '/' + image_name + ' [' + str(count) + '/' + str(total) + ']... ') sys.stdout.flush() if verbose == True: print '' if image_name in album_images: print 'File already exists, skipping.' sys.stdout.flush() else: # Loading the image data try: image_data = open(image_path, 'rb').read() except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) raise # Finding the mime type image_type = mimetypes.guess_type(image_path)[0] # Uploading image result = smugmug.upload_image(image_data=image_data, image_name=image_name, image_type=image_type, album_id=album_id) if result['stat'] != 'ok': print 'Error: Upload failed for file \'' + image + '\'' print 'Printing server response:' print result sys.exit(1) print 'Done' # Small additional check if the number of images matches album_images = smugmug.get_album_images(album_id) if len(image_paths) != len(image_paths): print 'Warning: You selected ' + str(len(args.images)) + ' images, but there are ' + str(len(existing_images)) + ' in the online album.' print 'Album done: \''+album_name+'\''