"""Lambda gda_spotify_scraper function module.""" from lambdacommon.common_config import logger from lambdacommon.common_config import SENTRY_DSN from requests import HTTPError import sentry_sdk from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration from sentry_sdk import capture_exception import config from src import constants from src import spotify_parser from src.spotify_parser import ParsingError sentry_sdk.init( dsn=SENTRY_DSN, environment=config.ENVIRONMENT, integrations=[AwsLambdaIntegration(timeout_warning=True)] ) class RetryException(Exception): """Retry Exception. It tells the state machine to retry lambda execution. """ pass class FatalException(Exception): """Fatal exception. It tells the state machine to abort the lambda execution. """ pass def handler(event, context): """Lambda entry point.""" try: listeners = None correlation_id = event.get('correlation_id') logger.info(f'CorrelationID: {correlation_id}') spotify_id = event.get('spotify_id') if spotify_id: logger.info(f'Scraping Spotify montly listeners for: {spotify_id}') listeners = spotify_parser.get_spotify_monthly_listeners(spotify_id) logger.info(f'Spotify monthly listeners for {spotify_id} is {listeners}') else: logger.info('Spotify ID was not provided.') return {'spotify_monthly_listeners': listeners} except ParsingError as e: logger.exception(constants.ERROR_PARSE_EXCEPTION) capture_exception(e) raise RetryException(str(e)) except HTTPError as e: logger.exception(constants.ERROR_HTTP) capture_exception(e) raise RetryException(str(e)) except Exception as e: logger.exception(constants.ERROR_LAMBDA_FATAL) capture_exception(e) raise FatalException(str(e))