# import http.client # import json from util import dynamo_to_python INGESTED_COUNTRIES_STATUS = 'ingested_countries_status' def send_slack_alert(date): message = f'Ingestion for Spotify Top 200 chart for {date} started' print(message) # headers = {'content-type': 'application/json'} # body = json.dumps({'channel_id': 'C090V42GAQ2', 'message': message}) # conn = http.client.HTTPConnection("127.0.0.1:5000") # conn.request(method="POST", url="", body=body, headers=headers) # conn.close() def should_send_slack_alert(r): new_image = r['dynamodb'].get('NewImage') old_image = r['dynamodb'].get('OldImage') if (r['eventName'] == 'INSERT' and new_image is not None and INGESTED_COUNTRIES_STATUS in new_image and 'S' in new_image[INGESTED_COUNTRIES_STATUS] and len(new_image[INGESTED_COUNTRIES_STATUS]['S']) > 0 and 'date' in new_image and 'S' in new_image['date'] and len(new_image['date']['S']) > 0): return True if (r['eventName'] == 'MODIFY' and new_image is not None and old_image is not None and INGESTED_COUNTRIES_STATUS not in old_image and INGESTED_COUNTRIES_STATUS in new_image and 'S' in new_image[INGESTED_COUNTRIES_STATUS] and len(new_image[INGESTED_COUNTRIES_STATUS]['S']) > 0 and 'date' in new_image and 'S' in new_image['date'] and len(new_image['date']['S']) > 0): return True return False def handle_record(r): if should_send_slack_alert(r) == True: send_slack_alert(r['dynamodb']['NewImage']['date']['S'])