Ejemplo n.º 1
0
    def test_get_active_users(self, mock_get_active_users):
        t = int(time.time())
        mock_get_active_users.return_value = [
            {
                'user_id': 1,
                'musicbrainz_id': 'spotify_user',
                'user_token': 'token-token-token',
                'token_expires': t,
                'refresh_token': 'refresh-refresh-refresh',
                'last_updated': None,
                'active': True,
                'error_message': 'oops',
                'latest_listened_at': None,
            },
            {
                'user_id': 2,
                'musicbrainz_id': 'spotify_user_2',
                'user_token': 'token-token-token321',
                'token_expires': t + 31,
                'refresh_token': 'refresh-refresh-refresh321',
                'last_updated': None,
                'active': True,
                'error_message': 'oops2',
                'latest_listened_at': None,
            },
        ]

        lst = spotify.get_active_users_to_process()
        mock_get_active_users.assert_called_once()
        self.assertEqual(len(lst), 2)
        self.assertIsInstance(lst[0], spotify.Spotify)
        self.assertIsInstance(lst[1], spotify.Spotify)
        self.assertEqual(lst[0].user_id, 1)
        self.assertEqual(lst[1].user_id, 2)
Ejemplo n.º 2
0
    def test_get_active_users(self, mock_get_active_users):
        t = int(time.time())
        mock_get_active_users.return_value = [
            {
                'user_id': 1,
                'musicbrainz_id': 'spotify_user',
                'user_token': 'token-token-token',
                'token_expires': t,
                'refresh_token': 'refresh-refresh-refresh',
                'last_updated': None,
                'active': True,
                'error_message': 'oops',
                'latest_listened_at': None,
            },
            {
                'user_id': 2,
                'musicbrainz_id': 'spotify_user_2',
                'user_token': 'token-token-token321',
                'token_expires': t + 31,
                'refresh_token': 'refresh-refresh-refresh321',
                'last_updated': None,
                'active': True,
                'error_message': 'oops2',
                'latest_listened_at': None,
            },
        ]

        lst = spotify.get_active_users_to_process()
        mock_get_active_users.assert_called_once()
        self.assertEqual(len(lst), 2)
        self.assertIsInstance(lst[0], spotify.Spotify)
        self.assertIsInstance(lst[1], spotify.Spotify)
        self.assertEqual(lst[0].user_id, 1)
        self.assertEqual(lst[1].user_id, 2)
Ejemplo n.º 3
0
def process_all_spotify_users():
    """ Get a batch of users to be processed and import their Spotify plays.

    Returns:
        (success, failure) where
            success: the number of users whose plays were successfully imported.
            failure: the number of users for whom we faced errors while importing.
    """
    try:
        users = spotify.get_active_users_to_process()
    except DatabaseException as e:
        current_app.logger.error('Cannot get list of users due to error %s',
                                 str(e),
                                 exc_info=True)
        return 0, 0

    if not users:
        return 0, 0

    current_app.logger.info('Process %d users...' % len(users))
    success = 0
    failure = 0
    for u in users:
        t = time.time()
        current_app.logger.info('Importing spotify listens for user %s',
                                str(u))
        try:
            process_one_user(u)
            success += 1
        except spotify.SpotifyAPIError as e:
            # if it is an error from the Spotify API, show the error message to the user
            spotify.update_last_updated(
                user_id=u.user_id,
                success=False,
                error_message=str(e),
            )
            if not current_app.config['TESTING']:
                notify_error(u.musicbrainz_row_id, str(e))
            failure += 1
        except spotify.SpotifyListenBrainzError as e:
            current_app.logger.critical(
                'spotify_reader could not import listens: %s',
                str(e),
                exc_info=True)
            failure += 1
        except Exception as e:
            current_app.logger.critical(
                'spotify_reader could not import listens: %s',
                str(e),
                exc_info=True)
            failure += 1

        current_app.logger.info(
            'Took a total of %.2f seconds to process user %s',
            time.time() - t, str(u))

    current_app.logger.info('Processed %d users successfully!', success)
    current_app.logger.info('Encountered errors while processing %d users.',
                            failure)
    return success, failure
def process_all_spotify_users():
    """ Get a batch of users to be processed and import their Spotify plays.

    Returns:
        (success, failure) where
            success: the number of users whose plays were successfully imported.
            failure: the number of users for whom we faced errors while importing.
    """
    current_app.logger.info('Getting list of users to be processed...')
    try:
        users = spotify.get_active_users_to_process()
    except DatabaseException as e:
        current_app.logger.error('Cannot get list of users due to error %s', str(e), exc_info=True)
        return 0, 0

    if not users:
        return 0, 0

    success = 0
    failure = 0
    for u in users:
        t = time.time()
        current_app.logger.info('Importing spotify listens for user %s', str(u))
        try:
            process_one_user(u)
            success += 1
        except spotify.SpotifyAPIError as e:
            # if it is an error from the Spotify API, show the error message to the user
            spotify.update_last_updated(
                user_id=u.user_id,
                success=False,
                error_message=str(e),
            )
            failure += 1
        except spotify.SpotifyListenBrainzError as e:
            current_app.logger.critical('spotify_reader could not import listens: %s', str(e), exc_info=True)
            failure += 1
        except Exception as e:
            current_app.logger.critical('spotify_reader could not import listens: %s', str(e), exc_info=True)
            failure += 1

        current_app.logger.info('Took a total of %.2f seconds to process user %s', time.time() - t, str(u))

    current_app.logger.info('Processed %d users successfully!', success)
    current_app.logger.info('Encountered errors while processing %d users.', failure)
    return success, failure
def process_all_spotify_users():
    """ Get a batch of users to be processed and import their Spotify plays.

    Returns:
        (success, failure) where
            success: the number of users whose plays were successfully imported.
            failure: the number of users for whom we faced errors while importing.
    """
    try:
        users = spotify.get_active_users_to_process()
    except DatabaseException as e:
        current_app.logger.error('Cannot get list of users due to error %s',
                                 str(e),
                                 exc_info=True)
        return 0, 0

    if not users:
        return 0, 0

    current_app.logger.info('Process %d users...' % len(users))
    success = 0
    failure = 0
    for u in users:
        try:
            process_one_user(u)
            success += 1
        except spotify.SpotifyListenBrainzError as e:
            current_app.logger.critical(
                'spotify_reader could not import listens: %s',
                str(e),
                exc_info=True)
            failure += 1
        except Exception as e:
            current_app.logger.critical(
                'spotify_reader could not import listens: %s',
                str(e),
                exc_info=True)
            failure += 1

    current_app.logger.info('Processed %d users successfully!', success)
    current_app.logger.info('Encountered errors while processing %d users.',
                            failure)
    return success, failure