Skip to content

Commit

Permalink
working on #44
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiAgarwal committed Mar 18, 2017
1 parent 5957291 commit 80b58fa
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
37 changes: 37 additions & 0 deletions analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,40 @@ def find_best_posts(social_posts):
top_headlines = get_top_headlines(headlines)

return (top_tweets, top_instagram_posts, top_headlines, (len(instagram_posts), len(tweets), len(headlines)))


def find_best_email(emails):
if 'hits' in emails and 'hits' in emails['hits']:
number_of_emails = emails['hits']['total']
emailIdToEmail = {}

data = {
'id': [],
'opens': [],
'clicks': [],
'email': []
}

for email in emails['hits']['hits']:
single_email = email['_source']['data']

emailIdToEmail[single_email['Id']] = single_email
data['id'].append(single_email['Id'])
data['opens'].append(single_email['Opened'])
data['clicks'].append(single_email['Clicked'])
data['email'].append(single_email['To'])

email_data = pd.DataFrame(
data, columns=['id', 'opens', 'clicks', 'email'])
best_emails = email_data.sort_values(['clicks', 'opens'], ascending=[0, 0])
diverse_emails = best_emails.drop_duplicates('email')

max_emails = 6
if len(diverse_emails) < 6:
max_emails = len(diverse_emails)

top_emails = []
for index, row in diverse_emails[:max_emails].iterrows():
top_emails.append(emailIdToEmail[row['id']])

return top_emails
30 changes: 16 additions & 14 deletions daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@ def media_lists_to_social_posts(media_lists):


def get_email_analytics(user):
print es.get_emails_from_user_id(user)
emails = es.get_emails_from_user_id(user)
return analysis.find_best_email(emails)


def process():
# Get the mapping of user id -> list
users_to_media_lists = es.users_to_lists()
user_id = 5749563331706880
# Go through each user id
for user_id in users_to_media_lists.keys():
print 'Processing user', user_id
user = users_to_media_lists[user_id][0]
media_lists = users_to_media_lists[user_id][1]
# If the user has media lists
if len(media_lists) > 0:
# Process all the media lists for a particular user
print user['Email']
media_lists_to_best_social_posts = media_lists_to_social_posts(
media_lists)
email_analytics = get_email_analytics(user['Id'])
# send_email.email_user_daily_feed(
# user, media_lists, media_lists_to_best_social_posts)
# for user_id in users_to_media_lists.keys():
print 'Processing user', user_id
user = users_to_media_lists[user_id][0]
media_lists = users_to_media_lists[user_id][1]
# If the user has media lists
if len(media_lists) > 0:
# Process all the media lists for a particular user
print user['Email']
media_lists_to_best_social_posts = media_lists_to_social_posts(
media_lists)
email_analytics = get_email_analytics(user['Id'])
send_email.email_user_daily_feed(
user, media_lists, media_lists_to_best_social_posts, email_analytics)

process()
12 changes: 12 additions & 0 deletions email_templates/category_headline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
category_headline = '''<table width="600" border="0" cellpadding="0" cellspacing="0" class="force-row" style="width: 600px;">
<tr>
<td class="content-wrapper" style="padding-left:24px;padding-right:24px">
<br>
<div class="title" style="font-family:Helvetica, Arial, sans-serif;font-size:24px;font-weight:600;color:#374550">{0}</div>
</td>
</tr>
'''


def format_category_headline(category):
return category_headline.format(category)
Empty file added email_templates/email.py
Empty file.
8 changes: 4 additions & 4 deletions es.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ def get_contacts_from_list_id(list_id):


def get_emails_from_user_id(user_id):
date_from = moment.now().subtract(days=1).replace(
hours=0, minutes=0, seconds=0).locale("US/Eastern").timezone("Europe/London").format('YYYY-MM-DDTHH:mm:ss')
date_from = moment.now().locale("US/Eastern").timezone("Europe/London").subtract(days=1).replace(
hours=0, minutes=0, seconds=0).format('YYYY-MM-DDTHH:mm:ss')
date_to = moment.now().locale(
"US/Eastern").timezone("Europe/London").format('YYYY-MM-DDTHH:mm:ss')

query = {
'size': 5000,
'from': 0,
'sort': [{
'data.Created': {
'order': 'desc',
Expand Down Expand Up @@ -121,8 +123,6 @@ def get_emails_from_user_id(user_id):
}
}

print query

res = es.search(index='emails', doc_type='email', body=query)
return res

Expand Down
11 changes: 9 additions & 2 deletions send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from email_templates.news_headlines import format_headlines
from email_templates.instagram import format_instagram_posts
from email_templates.twitter import format_twitter_posts
from email_templates.category_headline import format_category_headline
from email_templates.construct_email import construct_email
from email_templates.fallbacks import no_lists, no_social_posts
from email_templates.general import format_count
Expand All @@ -30,7 +31,8 @@ def send_sendgrid_email(email, html):

# Set the subject
subject = "Your Daily NewsAI update"
to_email = Email(email)
to_email = Email('[email protected]')
# to_email = Email(email)
content = Content("text/html", html)
mail = Mail(
Email(SENDGRID_SENDER, "NewsAI"), subject, to_email, content)
Expand All @@ -43,7 +45,7 @@ def send_sendgrid_email(email, html):
return


def email_user_daily_feed(user, media_lists, posts):
def email_user_daily_feed(user, media_lists, posts, emails):
all_html = ''
counts = []
for media_list in media_lists:
Expand Down Expand Up @@ -98,6 +100,11 @@ def email_user_daily_feed(user, media_lists, posts):
# If the users has lists to share
if len(counts) > 0:
all_html += format_count(counts)
all_html = format_category_headline('Social') + all_html

if len(emails) > 0:
all_html = format_category_headline('Email Analytics') + all_html

final_html = construct_email(all_html)
send_sendgrid_email(user['Email'], final_html)
return
Expand Down

0 comments on commit 80b58fa

Please sign in to comment.