def process_google_changes(items_added): new_file_paths = [] # process items added if not items_added: return True for file_id, file_paths in items_added.items(): for file_path in file_paths: if file_path in new_file_paths: continue new_file_paths.append(file_path) # remove files that already exist in the plex database removed_rejected_exists = utils.remove_files_exist_in_plex_database( new_file_paths, conf.configs['PLEX_DATABASE_PATH']) if removed_rejected_exists: logger.info( "Rejected %d file(s) from Google Drive changes for already being in Plex!", removed_rejected_exists) # process the file_paths list if len(new_file_paths): logger.info( "Proceeding with scan of %d file(s) from Google Drive changes: %s", len(new_file_paths), new_file_paths) # loop each file, remapping and starting a scan thread for file_path in new_file_paths: final_path = utils.map_pushed_path(conf.configs, file_path) start_scan(final_path, 'Google Drive', 'Download') return True
def process_google_changes(changes): global google file_paths = [] # convert changes to file paths list for change in changes: logger.debug("Processing Google change: %s", change) if 'file' in change and 'fileId' in change: # dont consider trashed/removed events for processing if ('trashed' in change['file'] and change['file']['trashed']) or ( 'removed' in change and change['removed']): # remove item from cache if google.remove_item_from_cache(change['fileId']): logger.info("Removed '%s' from cache: %s", change['fileId'], change['file']['name']) continue # we always want to add changes to the cache so renames etc can be reflected inside the cache google.add_item_to_cache(change['fileId'], change['file']['name'], [] if 'parents' not in change['file'] else change['file']['parents']) # dont process folder events if 'mimeType' in change['file'] and 'vnd.google-apps.folder' in change['file']['mimeType']: # ignore this change as we dont want to scan folders continue # get this files paths success, item_paths = google.get_id_file_paths(change['fileId'], change['file']['teamDriveId'] if 'teamDriveId' in change[ 'file'] else None) if success and len(item_paths): file_paths.extend(item_paths) elif 'teamDrive' in change and 'teamDriveId' in change: # this is a teamdrive change # dont consider trashed/removed events for processing if 'removed' in change and change['removed']: # remove item from cache if google.remove_item_from_cache(change['teamDriveId']): logger.info("Removed teamDrive '%s' from cache: %s", change['teamDriveId'], change['teamDrive']['name'] if 'name' in change['teamDrive'] else 'Unknown teamDrive') continue if 'id' in change['teamDrive'] and 'name' in change['teamDrive']: # we always want to add changes to the cache so renames etc can be reflected inside the cache google.add_item_to_cache(change['teamDrive']['id'], change['teamDrive']['name'], []) continue # always dump the cache after running changes google.dump_cache() # remove files that are not of an allowed extension type removed_rejected_extensions = 0 for file_path in copy(file_paths): if not utils.allowed_scan_extension(file_path, conf.configs['GDRIVE']['SCAN_EXTENSIONS']): # this file did not have an allowed extension, remove it file_paths.remove(file_path) removed_rejected_extensions += 1 if removed_rejected_extensions: logger.info("Ignored %d file(s) from Google Drive changes for disallowed file extensions", removed_rejected_extensions) # remove files that are in the ignore paths list removed_rejected_paths = 0 for file_path in copy(file_paths): for ignore_path in conf.configs['GDRIVE']['IGNORE_PATHS']: if file_path.lower().startswith(ignore_path.lower()): # this file was from an ignored path, remove it file_paths.remove(file_path) removed_rejected_paths += 1 if removed_rejected_paths: logger.info("Ignored %d file(s) from Google Drive changes for disallowed file paths", removed_rejected_paths) # remove files that already exist in the plex database removed_rejected_exists = utils.remove_files_exist_in_plex_database(file_paths, conf.configs['PLEX_DATABASE_PATH']) if removed_rejected_exists: logger.info("Ignored %d file(s) from Google Drive changes for already being in Plex!", removed_rejected_exists) # process the file_paths list if len(file_paths): logger.info("Proceeding with scan of %d file(s) from Google Drive changes: %s", len(file_paths), file_paths) # loop each file, remapping and starting a scan thread for file_path in file_paths: final_path = utils.map_pushed_path(conf.configs, file_path) start_scan(final_path, 'Google Drive', 'Download') return True