#! /usr/bin/env python3 from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from contextlib import closing from sql import sql_queries from datetime import date, timedelta import snowflake.connector import psycopg2 import smtplib import argparse import sys import json import datetime with open('config.json', 'r') as f: config = json.load(f) sf_user = config['snowflake']["user"] sf_password = config['snowflake']["password"] sf_account = config['snowflake']["account"] sf_warehouse = config['snowflake']['warehouse'] db_name_slz = config['postgres_slz']["database"] db_user_slz = config['postgres_slz']["user"] db_password_slz = config['postgres_slz']["password"] db_host_slz = config['postgres_slz']["host"] db_port_slz = config['postgres_slz']["port"] db_name_main = config['postgres_main']["database"] db_user_main = config['postgres_main']["user"] db_password_main = config['postgres_main']["password"] db_host_main = config['postgres_main']["host"] db_port_main = config['postgres_main']["port"] local_db_name = config['failure_counter']["database"] local_db_user = config['failure_counter']["user"] local_db_password = config['failure_counter']["password"] local_db_host = config['failure_counter']["host"] local_db_port = config['failure_counter']["port"] failure_count_increase = "UPDATE failed_count SET failed_count = failed_count + 1, last = 'FAILED', last_updated = NOW() WHERE count_id = 'bw_daily';" failure_count_to_zero = "UPDATE failed_count SET failed_count = 0, last = 'OK', last_updated = NOW() WHERE count_id = 'bw_daily';" def postgres_update_failure_count(sql_query): with closing(psycopg2.connect(database=local_db_name, user=local_db_user, password=local_db_password, host=local_db_host, port=local_db_port)) as connection: with connection.cursor() as cursor: cursor.execute(sql_query) connection.commit() def create_arg_parser(): parser = argparse.ArgumentParser() parser.add_argument('-m', '--mail', nargs='+', action='store', dest='recipients', type=str) parser.add_argument('-p', '--print', action="store_const", const=True) return parser def send_mail(recipients: str, message: str, status: str) -> None: # Create a multipart message msg = MIMEMultipart() body_part = MIMEText(message, 'plain') msg['Subject'] = f"[{status}][PROD] Brandwatch (daily)" msg['From'] = "support.sme@dataart.com" msg['To'] = recipients # Add body to email msg.attach(body_part) # Create SMTP object server = smtplib.SMTP('relay1.dataart.com', 25) server.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) server.quit() def snowflake_fetch_data(sql_query) -> dict: ctx = snowflake.connector.connect( user=sf_user, password=sf_password, account=sf_account, warehouse=sf_warehouse ) cs = ctx.cursor(snowflake.connector.DictCursor) try: cs.execute(sql_query) result_dict = cs.fetchall() finally: cs.close() ctx.close() return result_dict def postgres_fetch_data_main(sql_query) -> list: result_list = [] with closing(psycopg2.connect(database=db_name_main, user=db_user_main, password=db_password_main, host=db_host_main, port=db_port_main)) as connection: with connection.cursor() as cursor: cursor.execute(sql_query) columns = cursor.description rows = cursor.fetchall() for row in rows: tmp = {} for i in range(len(columns)): tmp[columns[i][0]] = row[i] result_list.append(tmp) return result_list def postgres_fetch_data_slz(sql_query) -> list: result_list = [] with closing(psycopg2.connect(database=db_name_slz, user=db_user_slz, password=db_password_slz, host=db_host_slz, port=db_port_slz)) as connection: with connection.cursor() as cursor: cursor.execute(sql_query) columns = cursor.description rows = cursor.fetchall() for row in rows: tmp = {} for i in range(len(columns)): tmp[columns[i][0]] = row[i] result_list.append(tmp) return result_list def check_1_to_2() -> dict: result_dict = {} full_log = {} ### Check 1 Save the list of query_ids to be downloaded tomorrow ################################################ tmp_pg_check_1 = postgres_fetch_data_slz(sql_queries.pg_check_1) query_ids = tmp_pg_check_1[0]['string_agg'] print(f"QUERY_IDS: - {tmp_pg_check_1[0]['string_agg']}") #Check if tmp_pg_check_1: result_file = open("results/" + datetime.date.today().strftime("%F") + ".txt", "w") result_file.write(query_ids) result_file.close() full_log["CHECK_1"] = {'OK': 'File with query ids created'} else: result_dict["CHECK_1"] = {'FAILED': 'File with query ids was not created'} ### Check 2 SLZ status: Required units of work created (expected result: 3 results) ################################################ tmp_pg_check_2 = postgres_fetch_data_slz(sql_queries.pg_check_2) print(f"CHECK_2: - Query returned {len(tmp_pg_check_2)} results") if len(tmp_pg_check_2) != 3: result_dict["CHECK_2"] = {'FAILED': 'Query did not return 3 results'} full_log["CHECK_2"] = result_dict["CHECK_2"] else: full_log["CHECK_2"] = {'OK': 'Query returned 3 results'} return result_dict, full_log def check_3_to_5() -> dict: result_dict = {} full_log = {} ### Check 3 SLZ status: artist_queries is successful (Google Sheets parsed successfully during the last 2 weeks) (expected result: 0 results) ################################################ tmp_pg_check_3 = postgres_fetch_data_slz(sql_queries.pg_check_3) print(f"CHECK_3: - {tmp_pg_check_3}") #Check if tmp_pg_check_3: full_log["CHECK_3"] = {} result_dict["CHECK_3"] = {} for item in tmp_pg_check_3: full_log["CHECK_3"].update({item['REPORT_DATE'].strftime("%Y-%m-%d"): item['unit_of_work_id']}) print(f"{item['REPORT_DATE']} - {item['unit_of_work_id']}") result_dict["CHECK_3"].update({item['REPORT_DATE'].strftime("%Y-%m-%d"): item['unit_of_work_id']}) else: full_log["CHECK_3"] = {'OK': 'Query returned 0 results'} ### Check 4 SLZ status: DAILY artist_ingestion is successful (daily ingestion completed for the past 2 weeks; excluding WAITING_STUCKED issues) (expected result: 0 results) ############################################# tmp_pg_check_4 = postgres_fetch_data_slz(sql_queries.pg_check_4) print(f"CHECK_4: {tmp_pg_check_4}") if tmp_pg_check_4: result_dict["CHECK_4"] = {} for item in tmp_pg_check_4: date = item['REPORT_DATE'].strftime("%Y-%m-%d") uow = item['UNIT_OF_WORK_ID'] cs_id = item['CONTENT_STATUS_ID'] run_ids = item['RUN_IDS'] result_dict["CHECK_4"][date] = {'UOW ID': uow, 'CS ID': cs_id ,'RUN IDs': run_ids} full_log["CHECK_4"] = result_dict["CHECK_4"] else: full_log["CHECK_4"] = {'OK': 'Query returned 0 results'} ### Check 5 SLZ status: DAILY artist_ingestion is successful (daily ingestion completed for the past 2 weeks; excluding WAITING_STUCKED issues) (expected result: 0 results) ############################################# tmp_pg_check_5 = postgres_fetch_data_slz(sql_queries.pg_check_5) print(f"CHECK_5: {tmp_pg_check_5}") if tmp_pg_check_5: result_dict["CHECK_5"] = {'FAILED': 'Query did not return 0 results'} full_log["CHECK_5"] = result_dict["CHECK_5"] else: full_log["CHECK_5"] = {'OK': 'Query returned 0 results'} return result_dict, full_log def check_9_to_28() -> dict: result_dict = {} full_log = {} ### Check 8 SNOWFLAKE status - S3 → RAW (expected result: 0 results) ############################################# tmp_sf_check_8 = snowflake_fetch_data(sql_queries.sf_check_8) print(f"CHECK_8: {tmp_sf_check_8}") if tmp_sf_check_8: result_dict["CHECK_8"] = {'FAILED': 'Query did not return 0 results'} full_log["CHECK_8"] = result_dict["CHECK_8"] else: full_log["CHECK_8"] = {'OK': 'Query returned 0 results'} ### Check 9 SNOWFLAKE status: SLZ → RAW - BRANDWATCH_QUERY_META (expected result: Counts match between steps) ################################################ tmp_sf_check_9 = snowflake_fetch_data(sql_queries.sf_check_9) tmp_pg_check_9 = postgres_fetch_data_slz(sql_queries.pg_check_9) sf_check_9_count = int(tmp_sf_check_9[0]['count']) pg_check_9_count = int(tmp_pg_check_9[0]['count']) print(f"SRC_CHECK_9: - {sf_check_9_count}") print(f"DEST_CHECK_9: - {pg_check_9_count}") full_log["CHECK_9"] = {'SRC_CHECK_9': sf_check_9_count, 'DEST_CHECK_9': pg_check_9_count} if sf_check_9_count != pg_check_9_count: result_dict["CHECK_9"] = full_log["CHECK_9"] ### Check 10 SNOWFLAKE status: SLZ → RAW - BRANDWATCH_ANALYSIS (expected result: Counts match between steps) ################################################ tmp_sf_check_10 = snowflake_fetch_data(sql_queries.sf_check_10) tmp_pg_check_10 = postgres_fetch_data_slz(sql_queries.pg_check_10) sf_check_10_count = int(tmp_sf_check_10[0]['count']) pg_check_10_count = int(tmp_pg_check_10[0]['count']) print(f"SRC_CHECK_10: - {sf_check_10_count}") print(f"DEST_CHECK_10: - {pg_check_10_count}") full_log["CHECK_10"] = {'SRC_CHECK_10': sf_check_10_count, 'DEST_CHECK_10': pg_check_10_count} if sf_check_10_count != pg_check_10_count: result_dict["CHECK_10"] = full_log["CHECK_10"] ### Check 11 SNOWFLAKE status: SLZ → RAW - BRANDWATCH_QUERY_LOG (expected result: Counts match between steps) ################################################ tmp_sf_check_11 = snowflake_fetch_data(sql_queries.sf_check_11) tmp_pg_check_11 = postgres_fetch_data_slz(sql_queries.pg_check_11) sf_check_11_count = int(tmp_sf_check_11[0]['count']) pg_check_11_count = int(tmp_pg_check_11[0]['count']) print(f"SRC_CHECK_11: - {sf_check_11_count}") print(f"DEST_CHECK_11: - {pg_check_11_count}") full_log["CHECK_11"] = {'SRC_CHECK_11': sf_check_11_count, 'DEST_CHECK_11': pg_check_11_count} if sf_check_11_count != pg_check_11_count: result_dict["CHECK_11"] = full_log["CHECK_11"] ### Check 12 SNOWFLAKE status: SLZ → RAW - BRANDWATCH_QUERY_RUN (expected result: Counts match between steps) ################################################ tmp_sf_check_12 = snowflake_fetch_data(sql_queries.sf_check_12) tmp_pg_check_12 = postgres_fetch_data_slz(sql_queries.pg_check_12) sf_check_12_count = int(tmp_sf_check_12[0]['count']) pg_check_12_count = int(tmp_pg_check_12[0]['count']) print(f"SRC_CHECK_12: - {sf_check_12_count}") print(f"DEST_CHECK_12: - {pg_check_12_count}") full_log["CHECK_12"] = {'SRC_CHECK_12': sf_check_12_count, 'DEST_CHECK_12': pg_check_12_count} if sf_check_12_count != pg_check_12_count: result_dict["CHECK_12"] = full_log["CHECK_12"] ### Check 13 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_QUERY_META (expected result: Counts match) ################################################ tmp_sf_check_13 = snowflake_fetch_data(sql_queries.sf_check_13) sf_check_13_dest = int(tmp_sf_check_13[0]['count']) sf_check_13_src = int(tmp_sf_check_13[1]['count']) print(f"DEST_CHECK_13: - {sf_check_13_dest}") print(f"SRC_CHECK_13: - {sf_check_13_src}") full_log["CHECK_13"] = {'DEST_CHECK_13': sf_check_13_dest, 'SRC_CHECK_13': sf_check_13_src} if sf_check_13_dest != sf_check_13_src: result_dict["CHECK_13"] = full_log["CHECK_13"] ### Check 14 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_ANALYSIS (expected result: Counts match) ################################################ tmp_sf_check_14 = snowflake_fetch_data(sql_queries.sf_check_14) sf_check_14_dest = int(tmp_sf_check_14[0]['count']) sf_check_14_src = int(tmp_sf_check_14[1]['count']) print(f"DEST_CHECK_14: - {sf_check_14_dest}") print(f"SRC_CHECK_14: - {sf_check_14_src}") full_log["CHECK_14"] = {'DEST_CHECK_14': sf_check_14_dest, 'SRC_CHECK_14': sf_check_14_src} if sf_check_14_dest != sf_check_14_src: result_dict["CHECK_14"] = full_log["CHECK_14"] ### Check 15 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_EMOTION (expected result: Counts match) ################################################ tmp_sf_check_15 = snowflake_fetch_data(sql_queries.sf_check_15) sf_check_15_dest = int(tmp_sf_check_15[0]['count']) sf_check_15_src = int(tmp_sf_check_15[1]['count']) print(f"DEST_CHECK_15: - {sf_check_15_dest}") print(f"SRC_CHECK_15: - {sf_check_15_src}") full_log["CHECK_15"] = {'DEST_CHECK_15': sf_check_15_dest, 'SRC_CHECK_15': sf_check_15_src} if sf_check_15_dest != sf_check_15_src: result_dict["CHECK_15"] = full_log["CHECK_15"] ### Check 16 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_LOCATION (expected result: Counts match) ################################################ tmp_sf_check_16 = snowflake_fetch_data(sql_queries.sf_check_16) sf_check_16_dest = int(tmp_sf_check_16[0]['count']) sf_check_16_src = int(tmp_sf_check_16[1]['count']) print(f"DEST_CHECK_16: - {sf_check_16_dest}") print(f"SRC_CHECK_16: - {sf_check_16_src}") full_log["CHECK_16"] = {'DEST_CHECK_16': sf_check_16_dest, 'SRC_CHECK_16': sf_check_16_src} if sf_check_16_dest != sf_check_16_src: result_dict["CHECK_16"] = full_log["CHECK_16"] ### Check 17 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_PLATFORM (expected result: Counts match) ################################################ tmp_sf_check_17 = snowflake_fetch_data(sql_queries.sf_check_17) sf_check_17_dest = int(tmp_sf_check_17[0]['count']) sf_check_17_src = int(tmp_sf_check_17[1]['count']) print(f"DEST_CHECK_17: - {sf_check_17_dest}") print(f"SRC_CHECK_17: - {sf_check_17_src}") full_log["CHECK_17"] = {'DEST_CHECK_17': sf_check_17_dest, 'SRC_CHECK_17': sf_check_17_src} if sf_check_17_dest != sf_check_17_src: result_dict["CHECK_17"] = full_log["CHECK_17"] ### Check 18 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_SENTIMENT (expected result: Counts match) ################################################ tmp_sf_check_18 = snowflake_fetch_data(sql_queries.sf_check_18) sf_check_18_dest = int(tmp_sf_check_18[0]['count']) sf_check_18_src = int(tmp_sf_check_18[1]['count']) print(f"DEST_CHECK_18: - {sf_check_18_dest}") print(f"SRC_CHECK_18: - {sf_check_18_src}") full_log["CHECK_18"] = {'DEST_CHECK_18': sf_check_18_dest, 'SRC_CHECK_18': sf_check_18_src} if sf_check_18_dest != sf_check_18_src: result_dict["CHECK_18"] = full_log["CHECK_18"] ### Check 19 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_QUERY_LOG (expected result: Counts match) ################################################ tmp_sf_check_19 = snowflake_fetch_data(sql_queries.sf_check_19) sf_check_19_dest = int(tmp_sf_check_19[0]['count']) sf_check_19_src = int(tmp_sf_check_19[1]['count']) print(f"DEST_CHECK_19: - {sf_check_19_dest}") print(f"SRC_CHECK_19: - {sf_check_19_src}") full_log["CHECK_19"] = {'DEST_CHECK_19': sf_check_19_dest, 'SRC_CHECK_19': sf_check_19_src} if sf_check_19_dest != sf_check_19_src: result_dict["CHECK_19"] = full_log["CHECK_19"] ### Check 20 SNOWFLAKE status: RAW → MAIN - BRANDWATCH_QUERY_RUN (expected result: Counts match) ################################################ tmp_sf_check_20 = snowflake_fetch_data(sql_queries.sf_check_20) sf_check_20_dest = int(tmp_sf_check_20[0]['count']) sf_check_20_src = int(tmp_sf_check_20[1]['count']) print(f"DEST_CHECK_20: - {sf_check_20_dest}") print(f"SRC_CHECK_20: - {sf_check_20_src}") full_log["CHECK_20"] = {'DEST_CHECK_20': sf_check_20_dest, 'SRC_CHECK_20': sf_check_20_src} if sf_check_20_dest != sf_check_20_src: result_dict["CHECK_20"] = full_log["CHECK_20"] ### Check 21 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_QUERY_META (expected result: Counts match) ################################################ tmp_sf_check_21 = snowflake_fetch_data(sql_queries.sf_check_21) sf_check_21_dest = int(tmp_sf_check_21[0]['count']) sf_check_21_src = int(tmp_sf_check_21[1]['count']) print(f"DEST_CHECK_21: - {sf_check_21_dest}") print(f"SRC_CHECK_21: - {sf_check_21_src}") full_log["CHECK_21"] = {'DEST_CHECK_21': sf_check_21_dest, 'SRC_CHECK_21': sf_check_21_src} if sf_check_21_dest != sf_check_21_src: result_dict["CHECK_21"] = full_log["CHECK_21"] ### Check 22 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_ANALYSIS (expected result: Counts match) ################################################ tmp_sf_check_22 = snowflake_fetch_data(sql_queries.sf_check_22) sf_check_22_dest = int(tmp_sf_check_22[0]['count']) sf_check_22_src = int(tmp_sf_check_22[1]['count']) print(f"DEST_CHECK_22: - {sf_check_22_dest}") print(f"SRC_CHECK_22: - {sf_check_22_src}") full_log["CHECK_22"] = {'DEST_CHECK_22': sf_check_22_dest, 'SRC_CHECK_22': sf_check_22_src} if sf_check_22_dest != sf_check_22_src: result_dict["CHECK_22"] = full_log["CHECK_22"] ### Check 23 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_EMOTION (expected result: Counts match) ################################################ tmp_sf_check_23 = snowflake_fetch_data(sql_queries.sf_check_23) sf_check_23_dest = int(tmp_sf_check_23[0]['count']) sf_check_23_src = int(tmp_sf_check_23[1]['count']) print(f"DEST_CHECK_23: - {sf_check_23_dest}") print(f"SRC_CHECK_23: - {sf_check_23_src}") full_log["CHECK_23"] = {'DEST_CHECK_23': sf_check_23_dest, 'SRC_CHECK_23': sf_check_23_src} if sf_check_23_dest != sf_check_23_src: result_dict["CHECK_23"] = full_log["CHECK_23"] ### Check 24 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_LOCATION (expected result: Counts match) ################################################ tmp_sf_check_24 = snowflake_fetch_data(sql_queries.sf_check_24) sf_check_24_dest = int(tmp_sf_check_24[0]['count']) sf_check_24_src = int(tmp_sf_check_24[1]['count']) print(f"DEST_CHECK_24: - {sf_check_24_dest}") print(f"SRC_CHECK_24: - {sf_check_24_src}") full_log["CHECK_24"] = {'DEST_CHECK_24': sf_check_24_dest, 'SRC_CHECK_24': sf_check_24_src} if sf_check_24_dest != sf_check_24_src: result_dict["CHECK_24"] = full_log["CHECK_24"] ### Check 25 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_PLATFORM (expected result: Counts match) ################################################ tmp_sf_check_25 = snowflake_fetch_data(sql_queries.sf_check_25) sf_check_25_dest = int(tmp_sf_check_25[0]['count']) sf_check_25_src = int(tmp_sf_check_25[1]['count']) print(f"DEST_CHECK_25: - {sf_check_25_dest}") print(f"SRC_CHECK_25: - {sf_check_25_src}") full_log["CHECK_25"] = {'DEST_CHECK_25': sf_check_25_dest, 'SRC_CHECK_25': sf_check_25_src} if sf_check_25_dest != sf_check_25_src: result_dict["CHECK_25"] = full_log["CHECK_25"] ### Check 26 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_SENTIMENT (expected result: Counts match) ################################################ tmp_sf_check_26 = snowflake_fetch_data(sql_queries.sf_check_26) sf_check_26_dest = int(tmp_sf_check_26[0]['count']) sf_check_26_src = int(tmp_sf_check_26[1]['count']) print(f"DEST_CHECK_26: - {sf_check_26_dest}") print(f"SRC_CHECK_26: - {sf_check_26_src}") full_log["CHECK_26"] = {'DEST_CHECK_26': sf_check_26_dest, 'SRC_CHECK_26': sf_check_26_src} if sf_check_26_dest != sf_check_26_src: result_dict["CHECK_26"] = full_log["CHECK_26"] ### Check 27 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_QUERY_LOG (expected result: Counts match) ################################################ tmp_sf_check_27 = snowflake_fetch_data(sql_queries.sf_check_27) sf_check_27_dest = int(tmp_sf_check_27[0]['count']) sf_check_27_src = int(tmp_sf_check_27[1]['count']) print(f"DEST_CHECK_27: - {sf_check_27_dest}") print(f"SRC_CHECK_27: - {sf_check_27_src}") full_log["CHECK_27"] = {'DEST_CHECK_27': sf_check_27_dest, 'SRC_CHECK_27': sf_check_27_src} if sf_check_27_dest != sf_check_27_src: result_dict["CHECK_27"] = full_log["CHECK_27"] ### Check 28 SNOWFLAKE status: MAIN → EXP - BRANDWATCH_QUERY_RUN (expected result: Counts match) ################################################ tmp_sf_check_28 = snowflake_fetch_data(sql_queries.sf_check_28) sf_check_28_dest = int(tmp_sf_check_28[0]['count']) sf_check_28_src = int(tmp_sf_check_28[1]['count']) print(f"DEST_CHECK_28: - {sf_check_28_dest}") print(f"SRC_CHECK_28: - {sf_check_28_src}") full_log["CHECK_28"] = {'DEST_CHECK_28': sf_check_28_dest, 'SRC_CHECK_28': sf_check_28_src} if sf_check_28_dest != sf_check_28_src: result_dict["CHECK_28"] = full_log["CHECK_28"] return result_dict, full_log ################# run the all steps ###################### def run_steps() -> dict: message_dict = {'name':"[PROD] Brandwatch (daily)",'status':"",'date':"", 'info':"", 'details':"", 'full_log':""} # set date according to sql searching date, currently it's `today` message_dict["date"] = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime("%Y-%m-%d") result_dict_1_to_2, monitoring_1_to_2_log = check_1_to_2() result_dict_3_to_5, monitoring_3_to_5_log = check_3_to_5() result_dict_9_to_28, monitoring_9_to_28_log = check_9_to_28() message_dict['full_log'] = {**monitoring_1_to_2_log, **monitoring_3_to_5_log, **monitoring_9_to_28_log} message_dict['details'] = {**result_dict_1_to_2, **result_dict_3_to_5, **result_dict_9_to_28} if result_dict_1_to_2: postgres_update_failure_count(failure_count_increase) message_dict['status'] = "FAILED" message_dict['info'] = "Ingestion issues" elif result_dict_3_to_5: postgres_update_failure_count(failure_count_increase) message_dict['status'] = "FAILED" message_dict['info'] = "Data is partially ingested" elif result_dict_9_to_28: postgres_update_failure_count(failure_count_increase) message_dict['status'] = "FAILED" message_dict['info'] = "Partially ingested to Snowflake" else: postgres_update_failure_count(failure_count_to_zero) message_dict['status'] = "OK" message_dict['info'] = "Ingested to Snowflake" return message_dict if __name__ == '__main__': # # check command line parameters and print or email result parser = create_arg_parser() arg_list = parser.parse_args() if len(sys.argv[1:]) > 0: result = run_steps() with open('/tmp/brandwatch_daily_log.json', 'w') as json_file: json.dump(result, json_file, indent=4) if arg_list.recipients: recipients = arg_list.recipients[0] send_mail(recipients=recipients, message=json.dumps(result, indent=4), status=result['status']) if arg_list.print: print(json.dumps(result, indent=4)) else: parser.print_help()