1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# -*- coding: utf-8-*-
import smtplib
from email.MIMEText import MIMEText
import urllib2
import re
from pytz import timezone
def sendEmail(SUBJECT, BODY, TO, FROM, SENDER, PASSWORD, SMTP_SERVER):
"""Sends an HTML email."""
for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8':
try:
BODY.encode(body_charset)
except UnicodeError:
pass
else:
break
msg = MIMEText(BODY.encode(body_charset), 'html', body_charset)
msg['From'] = SENDER
msg['To'] = TO
msg['Subject'] = SUBJECT
SMTP_PORT = 587
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.starttls()
session.login(FROM, PASSWORD)
session.sendmail(SENDER, TO, msg.as_string())
session.quit()
def emailUser(profile, SUBJECT="", BODY=""):
"""
sends an email.
Arguments:
profile -- contains information related to the user (e.g., email
address)
SUBJECT -- subject line of the email
BODY -- body text of the email
"""
def generateSMSEmail(profile):
"""
Generates an email from a user's phone number based on their carrier.
"""
if profile['carrier'] is None or not profile['phone_number']:
return None
return str(profile['phone_number']) + "@" + profile['carrier']
if profile['prefers_email'] and profile['gmail_address']:
# add footer
if BODY:
BODY = profile['first_name'] + \
",<br><br>Here are your top headlines:" + BODY
BODY += "<br>Sent from your Jasper"
recipient = profile['gmail_address']
if profile['first_name'] and profile['last_name']:
recipient = profile['first_name'] + " " + \
profile['last_name'] + " <%s>" % recipient
else:
recipient = generateSMSEmail(profile)
if not recipient:
return False
try:
if 'mailgun' in profile:
user = profile['mailgun']['username']
password = profile['mailgun']['password']
server = 'smtp.mailgun.org'
else:
user = profile['gmail_address']
password = profile['gmail_password']
server = 'smtp.gmail.com'
sendEmail(SUBJECT, BODY, recipient, user,
"Jasper <jasper>", password, server)
return True
except:
return False
def getTimezone(profile):
"""
Returns the pytz timezone for a given profile.
Arguments:
profile -- contains information related to the user (e.g., email
address)
"""
try:
return timezone(profile['timezone'])
except:
return None
def generateTinyURL(URL):
"""
Generates a compressed URL.
Arguments:
URL -- the original URL to-be compressed
"""
target = "http://tinyurl.com/api-create.php?url=" + URL
response = urllib2.urlopen(target)
return response.read()
def isNegative(phrase):
"""
Returns True if the input phrase has a negative sentiment.
Arguments:
phrase -- the input phrase to-be evaluated
"""
return bool(re.search(r'\b(no(t)?|don\'t|stop|end)\b', phrase,
re.IGNORECASE))
def isPositive(phrase):
"""
Returns True if the input phrase has a positive sentiment.
Arguments:
phrase -- the input phrase to-be evaluated
"""
return bool(re.search(r'\b(sure|yes|yeah|go)\b', phrase, re.IGNORECASE))
|