import os import sys import pandas as pd from jinja2 import Template from openpyxl import Workbook from loguru import logger from config import Params, emailParams, LogParams import data import content from djagitit.mailing import html from djagitit.mailing import smtp global end_date end_date = data.get_end_date()['end_date'].min() global start_date start_date = (pd.to_datetime(end_date) - pd.Timedelta(days=6)) def process_country_html(country=None, is_draft=False): logger.info("Process country html") try: results = [] for client in Params.clients: results.append(content.process_client(country['country_code'], client, country['partners'])) email_html = Template(open('../html/email.html').read() ).render( is_draft=is_draft, start_date=start_date.strftime('%m/%d'), end_date=end_date.strftime('%m/%d'), flag=country['flag'], logo_sme=html.Logos.sonymusic_redwhite(), results=results, ) logger.success("Process country html OK") return email_html except Exception as e: logger.exception(f"Failed to process country html: {e}") raise e def process_country_attachments(country=None): logger.info("Process country attachments") try: df_tracks = data.get_country_weekly_top100_tracks(country['country_code'], country['partners']) df_projects = data.get_country_weekly_top100_projects(country['country_code'], country['partners']) df_artists = data.get_country_weekly_top100_artists(country['country_code'], country['partners']) output_file = f'../tmp/weekly_tops100_{country["country_code"]}.xlsx' # Create workbook wb = Workbook() # Create all worksheets content.create_worksheet(wb, 'Top 50 Tracks', df_tracks, 5) # Streams column is index 5 content.create_worksheet(wb, 'Top 100 Projects', df_projects, 4) # Streams column is index 4 content.create_worksheet(wb, 'Top 100 Artists', df_artists, 3) # Streams column is index 3 wb.save(output_file) logger.success("Process country attachments OK") return output_file except Exception as e: logger.exception(f"Failed to process country attachments: {e}") raise e def process_country(country=None, is_draft=False, write_html=False, send_email=False): email_params = emailParams(country) email_html = process_country_html(country, is_draft) attachment_file = process_country_attachments(country) if send_email: logger.info("Send email") status = smtp.send_newsletter( subject=email_params['subject'], html=email_html, sender=email_params['sender'], bcc=email_params['bcc'], sender_alias=email_params['sender_alias'], reply_to=email_params['reply_to'], attachments=[attachment_file] ) if status == {}: logger.success("Send email OK") else: logger.error(f"Send email failed with status: {status}") if write_html: try: logger.info("Write html") with open(f'../tmp/email_{country["country_code"]}.html', 'w') as f: f.write(email_html) logger.success("Write html OK") except Exception as e: logger.exception(f"Failed to write html: {e}") raise e logger.info("Remove attachment file") os.remove(attachment_file) logger.success("Remove attachment file OK") def main(): logger.add( f"{os.getenv('LOGDIR')}/{LogParams.log_file}", rotation=LogParams.log_rotation, retention=LogParams.log_retention, compression=LogParams.log_compression, level=LogParams.log_level ) logger.info("Starting WEEKLY-SUMMARY-STREAMING-CEA...") logger.info(f"Arguments: {sys.argv[1:]}") # Check if too many arguments if len(sys.argv) > 3: logger.error(f"Too many arguments: {sys.argv[1:]}") sys.exit(1) # Check if write_html is provided write_html = False if len(sys.argv) > 2: arg = sys.argv[2].lower() if arg in ['w', 'write', 'y', 'yes']: write_html = True else: logger.error(f"Invalid argument: {arg}. Please use 'w', 'write', 'y', 'yes' to write html.") sys.exit(1) # Check if send_email is provided send_email = True if len(sys.argv) > 1: arg = sys.argv[1].lower() if arg in ['n', 'no']: send_email = False elif arg in ['s', 'send', 'y', 'yes']: send_email = True else: logger.error(f"Invalid argument: {arg}. Please use 's', 'send', 'y', 'yes' to send email or 'n', 'no' to not send email.") sys.exit(1) for country in Params.countries: logger.info(f"Processing country: {country['country_name']} ({country['country_code']})") try: process_country(country, write_html=write_html, send_email=send_email) logger.success(f"Successfully processed country {country['country_name']} ({country['country_code']})") except Exception as e: logger.exception(f"Failed to process country {country['country_name']} ({country['country_code']}): {e}") logger.info("WEEKLY-SUMMARY-STREAMING-CEA completed.") if __name__ == '__main__': main()