def test_get_username_invalids(self): config = config_parser.read_config() # None config self.assertIsNone(config_parser.get_username(config=None)) # Empty username config['credentials']['username'] = '' self.assertIsNone(config_parser.get_username(config=config))
def sync_drive(): while True: config = config_parser.read_config() verbose = config_parser.get_verbose(config=config) username = config_parser.get_username(config=config) destination_path = config_parser.prepare_destination(config=config) if username and destination_path: try: api = PyiCloudService(apple_id=username, password=utils.get_password_from_keyring( username=username)) if not api.requires_2sa: sync_directory(drive=api.drive, destination_path=destination_path, root=destination_path, items=api.drive.dir(), top=True, filters=config['filters'], remove=config_parser.get_remove_obsolete( config=config), verbose=verbose) else: print('Error: 2FA is required. Please log in.') except exceptions.PyiCloudNoStoredPasswordAvailableException: print( 'password is not stored in keyring. Please save the password in keyring.' ) sleep_for = config_parser.get_sync_interval(config=config) next_sync = (datetime.datetime.now() + datetime.timedelta( minutes=sleep_for)).strftime('%l:%M%p %Z on %b %d, %Y') print(f'Resyncing at {next_sync} ...') if sleep_for < 0: break time.sleep(sleep_for)
def test_get_username_given(self): config = read_config(config_path=tests.CONFIG_PATH) # Given username self.assertEqual( config["app"]["credentials"]["username"], config_parser.get_username(config=config), )
def test_get_username_valids(self): config = config_parser.read_config() # Given username self.assertEqual(config['credentials']['username'], config_parser.get_username(config=config))
def test_get_username_empty(self): # Empty username config = read_config(config_path=tests.CONFIG_PATH) config["app"]["credentials"]["username"] = "" self.assertIsNone(config_parser.get_username(config=config))
def test_get_username_none_config(self): # None config self.assertIsNone(config_parser.get_username(config=None))
def sync(): last_send = None enable_sync_drive = True enable_sync_photos = True drive_sync_interval = 0 photos_sync_interval = 0 sleep_for = 10 while True: config = read_config() username = config_parser.get_username(config=config) if username: try: if ENV_ICLOUD_PASSWORD_KEY in os.environ: password = os.environ.get(ENV_ICLOUD_PASSWORD_KEY) utils.store_password_in_keyring(username=username, password=password) else: password = utils.get_password_from_keyring( username=username) api = ICloudPyService( apple_id=username, password=password, cookie_directory=DEFAULT_COOKIE_DIRECTORY, ) if not api.requires_2sa: if "drive" in config and enable_sync_drive: sync_drive.sync_drive(config=config, drive=api.drive) drive_sync_interval = config_parser.get_drive_sync_interval( config=config) if "photos" in config and enable_sync_photos: sync_photos.sync_photos(config=config, photos=api.photos) photos_sync_interval = config_parser.get_photos_sync_interval( config=config) if "drive" not in config and "photos" not in config: LOGGER.warning( "Nothing to sync. Please add drive: and/or photos: section in config.yaml file." ) else: LOGGER.error("Error: 2FA is required. Please log in.") # Retry again sleep_for = config_parser.get_retry_login_interval( config=config) next_sync = ( datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c") LOGGER.info(f"Retrying login at {next_sync} ...") last_send = notify.send(config, last_send) sleep(sleep_for) continue except exceptions.ICloudPyNoStoredPasswordAvailableException: LOGGER.error( "Password is not stored in keyring. Please save the password in keyring." ) sleep_for = config_parser.get_retry_login_interval( config=config) next_sync = ( datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c") LOGGER.info(f"Retrying login at {next_sync} ...") last_send = notify.send(config, last_send) sleep(sleep_for) continue if "drive" not in config and "photos" in config: sleep_for = photos_sync_interval enable_sync_drive = False enable_sync_photos = True elif "drive" in config and "photos" not in config: sleep_for = drive_sync_interval enable_sync_drive = True enable_sync_photos = False elif ("drive" in config and "photos" in config and drive_sync_interval <= photos_sync_interval): sleep_for = photos_sync_interval - drive_sync_interval photos_sync_interval -= drive_sync_interval enable_sync_drive = True enable_sync_photos = False else: sleep_for = drive_sync_interval - photos_sync_interval drive_sync_interval -= photos_sync_interval enable_sync_drive = False enable_sync_photos = True next_sync = (datetime.datetime.now() + datetime.timedelta(seconds=sleep_for)).strftime("%c") LOGGER.info(f"Resyncing at {next_sync} ...") if (config_parser.get_drive_sync_interval(config=config) < 0 if "drive" in config else True and config_parser.get_photos_sync_interval( config=config) < 0 if "photos" in config else True): break sleep(sleep_for)