Esempio n. 1
0
def main_library_type(et, old_word, new_word, library_type):

    LocalLibrary.load_library(library_type)
    LocalLibraryMetadata.load_library_metadata(library_type)

    library_cache = LocalLibrary.cache(library_type)
    albums = library_cache.get('albums')
    album_paths = library_cache.get('album_paths')
    album_names = library_cache.get('album_names')
    images = library_cache.get('images')
    image_ids = library_cache.get('image_ids')

    library_metadata_cache = LocalLibraryMetadata.cache(library_type)

    for image_path, image_metadata in library_metadata_cache.items():

        desc = image_metadata.get('Description')
        title = image_metadata.get('Title')
        if desc is None:
            desc = title

        if desc is None:
            continue

        if not type(desc) == str:
            continue

        # If misspelled word found in the images description
        # then try to correct it
        if old_word in desc:
            old_desc = desc
            new_desc = desc.replace(old_word, new_word)

            print(f"image='{image_path}'")
            print(f"    old='{old_desc}'")
            print(f"    new='{new_desc}'")

            mime_type = image_metadata.get('MIMEType').split('/')[0]
            is_video = mime_type != 'image'
            ImageUtils.set_caption(et, image_path, new_desc, is_video)

    for album in albums:
        album_path = album.get('path')
        if old_word in album_path:
            new_album_path = album_path.replace(old_word, new_word)
            print(f"album='{album_path}'")
            print(f"    old='{album_path}'")
            print(f"    new='{new_album_path}'")
            if (os.path.exists(album_path)):
                shutil.move(album_path, new_album_path)
    def map_recursive(self, root, test):
        """
        High-level algorithm:
        1. For each local folder locate the Google album in cache
        2. If Google album does not exist then call 'gphotocli album upload <...path_to_album...>'
            - Add local images to Google Album from the Local album if missing
            - Remove images from Google album that are not in Local album
        """
        # Argument validation
        if not os.path.exists(root):
            logging.error(f"Folder does not exist: ({root})")
            return

        # Remove trailing slash
        slash_char = root[len(root) - 1]
        if slash_char == '/' or slash_char == '\\':
            root = root[:len(root)-1]

        # Get Google API service
        service = GoogleService.service()

        # Initialize Google API and load cache.
        google_cache = GoogleLibrary.cache()
        google_album_ids = google_cache.get('album_ids')
        google_album_titles = google_cache.get('album_titles')

        # Load local library cache
        local_cache = LocalLibrary.cache('jpg')
        local_albums = local_cache.get('albums')

        # Traverse all the sub folders in the cache
        for local_album in local_albums:

            local_album_name = local_album['name']
            local_album_path = local_album['path']

            if not local_album_path.lower().startswith(root.lower()):
                continue

            # If album not in Google Cache, ignore and then error out
            google_album_id = google_album_titles.get(local_album_name)
            google_album = google_album_ids[google_album_id] if google_album_id is not None else None

            if google_album is None:
                logging.error(f"Ignoring album not in Google Cache: '{google_album.get('title')}'")
                continue

            # Do mapping for each Local/Google album
            self.map_album(local_album, google_album, test)
Esempio n. 3
0
    def upload_recursive(self, root, test):

        # Argument validation
        if not os.path.exists(root):
            logging.error(f"Folder does not exist: ({root})")
            return

        # Remove trailing slash
        slash_char = root[len(root) - 1]
        if slash_char == '/' or slash_char == '\\':
            root = root[:len(root) - 1]

        # Get Google API service
        service = GoogleService.service()

        # Initialize Google API and load cache.
        google_cache = GoogleLibrary.cache()
        google_album_ids = google_cache.get('album_ids')
        google_album_titles = google_cache.get('album_titles')

        # Traverse all the sub folders in the cache
        local_cache = LocalLibrary.cache('jpg')
        local_albums = local_cache.get('albums')

        for local_album in local_albums:

            local_album_name = local_album['name']
            local_album_path = local_album['path']

            if not local_album_path.lower().startswith(root.lower()):
                continue

            # Check if album already in Google Cache
            google_album_id = google_album_titles.get(local_album_name)
            google_album = google_album_ids[
                google_album_id] if google_album_id is not None else None

            if google_album is not None:
                logging.info(
                    f"Album already uploaded: '{google_album.get('title')}'")
                continue

            # Do the actual creating of Google album
            album_response = self.create_shareable_album(
                service=service, album_name=local_album_name, test=test)
            if album_response:
                self.modified = True
    def map_album(self, local_album, google_album, test):

        logging.error(f"Mapping album: '{google_album.get('title')}'")

        # Initialize Google API and load cache.
        google_cache = GoogleLibrary.cache()
        google_album_ids = google_cache.get('album_ids')
        google_album_titles = google_cache.get('album_titles')
        google_image_ids = google_cache.get('image_ids')
        google_album_to_images = google_cache.ge_ids('album_images')

        # Load local library cache
        local_cache = LocalLibrary.cache('jpg')
        local_albums = local_cache.get('albums')
        local_images = local_cache.get('images')
        # local_image_ids = local_cache.get('image_ids')

        # Collect local images belonging to local album
        local_album_image_idxs = local_album.get('images')
        if local_album_image_idxs is None or len(local_album_image_idxs) <= 0:
            logging.info(f"No images found in album '{local_album.get('name')}'")
            return

        # from local album images indices, build local album image list
        local_album_images = {}
        for idx in local_album_image_idxs:
            local_image = local_images[idx]
            local_image_name = local_image.get('name')
            local_album_images[local_image_name] = local_image

        # From google album get images already in it
        google_album_id = google_album.get('id')
        google_album_to_image_ids = google_album_to_images.get(id)
        google_album_images = {}
        for google_image_id in google_album_to_image_ids:
            google_album_image = google_image_ids.get(google_image_id)
            google_album_images[google_image_id] = google_album_image