Esempio n. 1
0
def main():

    if '-h' in sys.argv or '--help' in sys.argv:
        print_help_information()
        exit_with_code(0)
    elif '--rdc' in sys.argv:
        exit_with_code(restore_default_config_file())

    try:
        logger = default_logger(
        )  # used for error logging before custom logger can be configured
        (playlist_config, log_config,
         account_config) = load_configurations(path=get_config_filepath())
        config_validator = ConfigValidator(playlist_config, log_config,
                                           account_config)
        if not config_validator.is_valid():
            raise Exception(
                'Invalid configuration file - check \'data/config.yaml\'')

        logger = setup_logger(
            log_config)  # custom logger based on user's config
        logger.info('Starting new session of SpotifyAutoModerator')

        api_client = SpotifyHelper(logger).configure_api(
            account_config['CLIENT_ID'], account_config['CLIENT_SECRET'],
            account_config['REDIRECT_URI'])
        if not isinstance(api_client, spotipy.client.Spotify):
            raise Exception('Failed to authenticate with Spotify')
        elif not config_validator.all_protected_playlists_exist(api_client):
            raise Exception(
                'Could not find all protected playlists in Spotify')

        moderate_playlists(logger, api_client, account_config['USERNAME'],
                           playlist_config)

    except OSError as err:
        logger.error('Error: \'%s\'', err)
        if 'Address already in use' in str(err):
            logger.error('Redirect URI \'%s\' is already in use',
                         account_config['REDIRECT_URI'])
            logger.error(
                'Try to use a different (free) port number and add this address to the'
                + 'application in the Spotify Developer Dashboard')
        exit_with_code(1)

    except Exception as err:
        logger.error('Error: \'%s\'', err)
        if '401' in str(err):
            logger.error('Confirm your account/client details are correct')
        exit_with_code(1)

    exit_with_code(0)
Esempio n. 2
0
 def test_is_valid_returns_false_if_account_config_is_invalid(self):
     validator = ConfigValidator(playlist={}, log={}, account={})
     validator.validate_playlist_config = Mock(return_value=True)
     validator.validate_log_config = Mock(return_value=True)
     validator.validate_account_config = Mock(return_value=False)
     self.assertFalse(validator.is_valid())
Esempio n. 3
0
 def test_is_valid_returns_true_if_all_config_types_are_valid(self):
     validator = ConfigValidator(playlist={}, log={}, account={})
     validator.validate_playlist_config = Mock(return_value=True)
     validator.validate_log_config = Mock(return_value=True)
     validator.validate_account_config = Mock(return_value=True)
     self.assertTrue(validator.is_valid())