"""Lambda gda_tiktok_scraper function module.""" from lambdacommon.common_config import logger from lambdacommon.common_config import SENTRY_DSN from requests import RequestException 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 errors from src import tiktok_parser sentry_sdk.init( dsn=SENTRY_DSN, environment=config.ENVIRONMENT, integrations=[AwsLambdaIntegration(timeout_warning=True)] ) def handler(event, context): """Lambda entry point.""" try: followers = None correlation_id = event.get('correlation_id') logger.info(f'CorrelationID: {correlation_id}') tiktok_url = event.get('tiktok_url') if tiktok_url: logger.info(f'Scraping Tiktok followers for: {tiktok_url}') followers = tiktok_parser.get_tiktok_followers(tiktok_url) logger.info(f'Tiktok followers for {tiktok_url} is {followers}') else: logger.info('Tiktok URL was not provided.') return {'tiktok_followers': followers} except RequestException as e: logger.exception(constants.ERROR_HTTP) capture_exception(e) raise errors.RetryException(str(e)) except errors.TikTokCaptchaException as e: logger.exception(constants.ERROR_TIKTOK_CAPTCHA) capture_exception(e) raise errors.RetryException(str(e)) except Exception as e: logger.exception(constants.ERROR_LAMBDA_FATAL) capture_exception(e) raise errors.FatalException(str(e))