#! /usr/bin/env python # -*- coding: utf-8 -*- import sys from datetime import datetime import json import datetime sys.path.insert(0, ".") import boto3 from botocore.exceptions import ClientError from tracker.json_utils import JsonException from tracker.logger import get_logger from tracker import db db.setup_session() logger = get_logger(__name__) # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. SENDER = "Whitelist Notification " # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. RECIPIENT = "joel.leslie@gmail.com" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable, and the # ConfigurationSetName=CONFIGURATION_SET argument below. # CONFIGURATION_SET = "ConfigSet" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. AWS_REGION = "eu-west-1" # The subject line for the email. SUBJECT = "Whitelist Alerts for {}".format(datetime.date.today().strftime("%e %b")) # The email body for recipients with non-HTML email clients. BODY_TEXT = ( "Amazon SES Test (Python)\r\n" "This email was sent with Amazon SES using the " "AWS SDK for Python (Boto)." ) # The HTML body of the email. root_html_template = open("./scripts/branded.html", "r").read() alerts_html_template = """
{alertRows}

{artistName}

""" alert_html_template = """ {alertImg} {alertText} """ alert_images = { "sc": """SoundCloud""", "in": """Instagram""", "spy": """Spotify""", "wl": """Whitelist""", "tw": """Twitter""", } # The character encoding for the email. CHARSET = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client("ses", region_name=AWS_REGION) def run_query(username): from api import models arids = [ r[0] for r in db.ROSession.execute( """select arid from whitelisted_artists where username = :username and is_live""", params=dict(username=username), ).fetchall() ] artists = [ r['artist'] for r in models.artists( "a", arids, include_alerts=True, include_media=False, only_alerts_for_today=True, use_new_setf=True) if r['artist']['recentAlerts'] ] return artists def make_alert_row(alert): img = alert_images.get(alert["type"]) or alert_images["wl"] print(alert, img) return alert_html_template.format(alertImg=img, alertText=alert["words"]) def make_html_body(artists): return root_html_template.format( date=datetime.date.today().strftime("%e %b"), alertsBody="".join( [ alerts_html_template.format( artistUrl="https://bot.whtlst.in/#/artist/a/{}".format(artist["arid"]), artistName=artist["name"], alertRows="".join([make_alert_row(a) for a in artist["recentAlerts"]]), ) for artist in artists ] ), ) joel = 'joel@whtlst.in' if __name__ == "__main__": artists = run_query("rob") import pdb pdb.set_trace() body_html = make_html_body(artists) try: # Provide the contents of the email. response = client.send_email( Destination={"ToAddresses": ['rob@whtlst.in', joel]}, Message={ "Body": { "Html": {"Charset": CHARSET, "Data": body_html}, "Text": {"Charset": CHARSET, "Data": BODY_TEXT}, }, "Subject": {"Charset": CHARSET, "Data": SUBJECT}, }, Source=SENDER, # If you are not using a configuration set, comment or delete the # following line # ConfigurationSetName=CONFIGURATION_SET, ) # Display an error if something goes wrong. except ClientError as e: print(e.response["Error"]["Message"]) else: print("Email sent! Message ID:"), print(response["MessageId"])