def upload_album_name(service, album_name): # Get album in local library by name album_cache_jpg = LocalLibrary.cache_jpg() album_names = album_cache_jpg['album_names'] if album_name not in album_names: logging.error(f"No local album found by name '{album_name}'") return local_album_idx = album_names[album_name] local_albums = album_cache_jpg['albums'] local_album = local_albums[local_album_idx] # Call common upload method Uploader.upload_album(service, local_album)
def main(): gphoto.init() # Load Local picsHres jpg Library LocalLibrary.load_library('jpg') local_cache = LocalLibrary.cache_jpg() local_albums = local_cache.get('albums') local_album_paths = local_cache.get('album_paths') local_album_names = local_cache.get('album_names') local_images = local_cache.get('images') local_image_ids = local_cache.get('image_ids') # temp_result is a dict holding image name as key # and an array value of image paths temp_result = {} # Loop through each images, get their leaf names and see # if image part with this name already exists for local_image in local_images: # Add image name and path to the temp_result image_name = local_image.get('name') image_path = local_image.get('path') image_path_list = temp_result.get(image_name) if image_path_list is None: image_path_list = [image_path] temp_result[image_name] = image_path_list else: image_path_list.append(image_path) # Now remove entries for temp_result where there is only one image path result = {} found = False for image_name in temp_result: image_path_list = temp_result.get(image_name) if len(image_path_list) > 1: found = True result[image_name] = image_path_list print(f"found duplicates = {found}") # Save to cache file also if found: gphoto.save_to_file(result, "find_duplicate_local_image_names.json")
def upload_album(service, local_album): logging.info( "------------------------------------------------------------------" ) logging.info(f" album: '{local_album['path']}'") logging.info( "------------------------------------------------------------------" ) # Create the album and make it sharable # -------------------------------------- google_album = None share_info = None try: google_album = AlbumAPI.create_album(service, local_album['name']) share_info = AlbumAPI.make_album_sharable(service, google_album['id'], share=True) except Exception as e: raise google_album_id = google_album['id'] # Loop through all images under the album and upload them # -------------------------------------------------------- local_album_image_idxs = local_album['images'] local_library_cache = LocalLibrary.cache_jpg() local_images = local_library_cache['images'] for local_image_idx in local_album_image_idxs: local_image = local_images[local_image_idx] local_image_name = local_image['name'] local_image_path = local_image['path']
def main(): gphoto.init() # Load Google Library GoogleLibrary.load_library() google_cache = GoogleLibrary.cache() google_album_ids = google_cache['album_ids'] google_album_titles = google_cache['album_titles'] google_image_ids = google_cache['image_ids'] google_image_filenames = google_cache['image_filenames'] google_album_images = google_cache['album_images'] google_image_albums = google_cache['image_albums'] # Load Local picsHres jpg Library LocalLibrary.load_library('jpg') local_cache = LocalLibrary.cache_jpg() local_albums = local_cache.get('albums') local_album_paths = local_cache.get('album_paths') local_album_names = local_cache.get('album_names') local_images = local_cache.get('images') local_image_ids = local_cache.get('image_ids') local_image_names = local_cache.get('image_names') # Initialize the result missing_images_with_album_reason = "MISSING_IMAGES_WITH_ALBUM" missing_images_with_no_album_reason = "MISSING_IMAGES_WITH_NO_ALBUM" image_exist_locally_reason = "IMAGE_EXIST_LOCALLY" result_missing_images_with_album = {} result_missing_images_with_no_album = [] result_image_exist_locally = [] result = { missing_images_with_album_reason: result_missing_images_with_album, missing_images_with_no_album_reason: result_missing_images_with_no_album, image_exist_locally_reason: result_image_exist_locally } # Walk through each Google images that begins with PFILMmmm_nnn.jpg for google_image_id in google_image_ids: google_image = google_image_ids[google_image_id] # Ignore images not begining with "PFILM" image_name = google_image.get('filename') if image_name is not None and not image_name.startswith("PFILM"): continue # Check for image exist locally local_image_idx = local_image_names.get(image_name) if local_image_idx is not None: local_image = local_images[local_image_idx] result_image_exist_locally.append(local_image.get('path')) continue # We now know that the image is missing locally # No figure out if this images does not have an album parent google_albums_of_this_image = google_image_albums.get(google_image_id) if google_albums_of_this_image is not None: # Images does have parent albums # add first album to the result first if not already done google_album_idx = None for idx in google_albums_of_this_image: google_album_idx = idx break google_album = google_album[google_album_idx] google_album_id = google_album.get('id') result_album = result.get(google_album_id) # If album not in result then add the album missing_images_with_album = None if result_album is None: missing_images_with_album = [] result_album = { 'id': google_album_id, 'title': google_album.get('title'), 'images': missing_images_with_album } result_missing_images_with_album[google_album_id] = result_album # Add missing image to parent album result missing_images_with_album.append({ 'id': google_image_id, 'filename': image_name, 'productUrl': google_image['productUrl'] }) # Google image is missing locally and has no parent album else: result_missing_images_with_no_album.append({ 'id': google_image_id, 'filename': image_name, 'productUrl': google_image['productUrl'] }) # Save to cache file also gphoto.save_to_file(result, "can_PFILMs_be_deleted.json")
def main(): """ Collect all the images with the date shot of the given year. Check if they follow the YYYYMMDD_HHMMDD_.... format If all follow this format then images of the whole year can be deleted in one shot. Otherwise list out the odd image months from the date shot as culprits. For each of these images see in the local folder album what to do. """ if len(sys.argv) < 2: logging.error("Too few arguments. Specify date shot year") return # Get arguments args_year = sys.argv[1] LocalLibrary.load_library('jpg') local_cache = LocalLibrary.cache_jpg() local_albums = local_cache.get('albums') local_album_paths = local_cache.get('album_paths') local_images = local_cache.get('images') GoogleLibrary.load_library() google_cache = GoogleLibrary.cache() google_album_ids = google_cache['album_ids'] google_album_titles = google_cache['album_titles'] google_image_ids = google_cache['image_ids'] google_image_filenames = google_cache['image_filenames'] google_album_images = google_cache['album_images'] google_image_albums = google_cache['image_albums'] google_images_with_missing_dateshot = [] google_images_missing_locally = [] local_images_with_non_standandard_filename = [] google_images_with_non_standandard_filename = [] google_images_with_arg_year = [] google_images_by_datetime = {} local_images_by_datetime = {} result = { 'google_images_with_missing_dateshot': google_images_with_missing_dateshot, 'google_images_missing_locally': google_images_missing_locally, 'local_images_with_non_standandard_filename': local_images_with_non_standandard_filename, 'google_images_with_non_standandard_filename': google_images_with_non_standandard_filename, 'google_images_with_arg_year': google_images_with_arg_year, 'google_images_by_datetime': google_images_by_datetime, 'local_images_by_datetime': local_images_by_datetime } # First collect all google images in the given year for google_image_id, google_image in google_image_ids.items(): mediaMetadata = google_image.get('mediaMetadata') if mediaMetadata is None: google_images_with_missing_dateshot.append(google_image) else: creationTime = mediaMetadata.get('creationTime') if creationTime is None: google_images_with_missing_dateshot.append(google_image) else: # Date shot is of the format "2021-02-15T20:29:52Z # Extract the year from it image_year = creationTime.split('-')[0] if image_year == args_year: google_images_with_arg_year.append(google_image) # If the google images does not have format YYYYMMDD_HHMMSS_... # then there is an issue for google_image in google_images_with_arg_year: filename = google_image.get('filename') splits = filename.split('_') if len(splits) < 3: google_images_with_non_standandard_filename.append(google_image) else: image_date = splits[0] image_time = splits[1] if len(image_date) < 8 or not image_date.isdecimal(): google_images_with_non_standandard_filename.append(google_image) elif len(image_time) < 6 or not image_time.isdecimal(): google_images_with_non_standandard_filename.append(google_image) else: pass # image_datetime = image_date + '_' + image_time # google_images_by_datetime[image_datetime] = { # 'filename': google_image.get('filename'), # 'productUrl': google_image.get('productUrl') # } # now make a list of all the local images in the year specified # and add them to the local_images_by_dateshot. pattern = f"\\{args_year}\\" for local_album_idx, local_album in enumerate(local_albums): album_path = local_album.get('path') if pattern not in album_path: continue album_image_idxs = local_album.get('images') for album_image_idx in album_image_idxs: local_image = local_images[album_image_idx] local_image_name = local_image.get('name') splits = local_image_name.split('_') if len(splits) < 3: local_images_with_non_standandard_filename.append(local_image.get('path')) image_date = splits[0] image_time = splits[1] if len(image_date) < 8 or not image_date.isdecimal(): local_images_with_non_standandard_filename.append(local_image.get('path')) elif len(image_time) < 6 or not image_time.isdecimal(): local_images_with_non_standandard_filename.append(local_image.get('path')) else: image_datetime = image_date + '_' + image_time local_images_by_datetime[image_datetime] = { 'filename': local_image.get('name'), 'path': local_image.get('path') } # Now traverse through all the google images with date shot # and locate them in local images # If not found then error for datetime, google_image in google_images_by_datetime.items(): local_image = local_images_by_datetime.get(datetime) if local_image is None: google_images_missing_locally.append(google_image) bn = os.path.basename(args_year) gphoto.save_to_file(result, f"can_google_images_be_deleted_by_year_{bn}.json")
def main(): """ Given a folder tree root like p:\\pics\\2014 loop through each album and find its images in Google photos. if the images do not have albums then they can be deleted. if the images have an album then and if the album have more images that the local album images and the albums is not shared then the images can be deleted """ if len(sys.argv) < 2: logging.error("Too few arguments. Specify folder pattern") return # Get arguments arg_album_year = sys.argv[1] arg_album_pattern = f"\\{arg_album_year}\\" LocalLibrary.load_library('jpg') local_cache = LocalLibrary.cache_jpg() local_albums = local_cache.get('albums') local_album_paths = local_cache.get('album_paths') local_images = local_cache.get('images') GoogleLibrary.load_library() google_cache = GoogleLibrary.cache() google_album_ids = google_cache['album_ids'] google_album_titles = google_cache['album_titles'] google_image_ids = google_cache['image_ids'] google_image_filenames = google_cache['image_filenames'] google_album_images = google_cache['album_images'] google_image_albums = google_cache['image_albums'] result = [] # Loop through each local folder under the root tree for local_album in local_albums: local_album_path = local_album.get('path') # filter out the ones that are not under the tree if local_album_path.find(arg_album_pattern) == -1: continue # if not local_album_path.startswith(arg_album_pattern): # continue # Add this album to the list result_album = {'path': local_album.get('path')} result.append(result_album) # Get first jpeg image of the local album first_local_image = None local_album_image_idxs = local_album.get('images') for local_album_image_idx in local_album_image_idxs: local_image = local_images[local_album_image_idx] if local_image.get('mime') == 'image/jpeg': first_local_image = local_image break if first_local_image is None: result_album[ 'ERROR'] = f"No jpeg images in local album '{local_album.get('path')}'" continue result_album['first_image'] = first_local_image['name'] # Locate this image in Google photos. Identify the pattern # If the image is of the form # YYYYMMDD_hhmmss_nn_AAAA_D800.jpeg # or just the actual name # First look for the images with actual name, if not found then # Look by date time in the filename first_google_image_id, pattern_found = find_google_image( first_local_image, google_image_ids, google_image_filenames) if first_google_image_id is None: result_album[ 'WARNING'] = f"First album image not in Google {first_local_image.get('name')}" continue first_google_image = google_image_ids.get(first_google_image_id) result_album['first_google_image'] = { 'id': first_google_image.get('id'), 'filename': first_google_image.get('filename'), 'mine': first_google_image.get('mine'), 'productUrl': first_google_image.get('productUrl') } # if the first image part of google album then # we need to know if the image is part of a shared album google_image_album_list = google_image_albums.get( first_google_image_id) if google_image_album_list is None or len( google_image_album_list) <= 0: result_album['NO-GOOGLE-ALBUM'] = True else: result_image_albums = [] result_album['HAS-ALBUMS'] = result_image_albums for google_image_album_id in google_image_album_list: google_album = google_album_ids.get(google_image_album_id) result_image_albums.append({ 'id': google_album.get('id'), 'title': google_album.get('title'), 'productUrl': google_album.get('productUrl'), 'shared': google_album.get('shared') }) gphoto.save_to_file(result, f"can_google_images_be_deleted_{arg_album_year}.json")