From e0515b25546b978db1b3e38ade65c98beeec3602 Mon Sep 17 00:00:00 2001 From: schneefux Date: Tue, 30 Sep 2014 18:58:24 +0200 Subject: Move app_utils.py from client/modules to client/ app_utils.py does not have a WORDS constant and thus does not belong into that folder --- client/app_utils.py | 124 ++++++++++++++++++++++++++++++++++++++++++++ client/modules/app_utils.py | 124 -------------------------------------------- jasper.py | 6 ++- 3 files changed, 128 insertions(+), 126 deletions(-) create mode 100644 client/app_utils.py delete mode 100644 client/modules/app_utils.py diff --git a/client/app_utils.py b/client/app_utils.py new file mode 100644 index 0000000..cea6881 --- /dev/null +++ b/client/app_utils.py @@ -0,0 +1,124 @@ +# -*- coding: utf-8-*- +import smtplib +from email.MIMEText import MIMEText +import urllib2 +import re +import requests +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'] + \ + ",

Here are your top headlines:" + BODY + BODY += "
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 ", 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)) diff --git a/client/modules/app_utils.py b/client/modules/app_utils.py deleted file mode 100644 index cea6881..0000000 --- a/client/modules/app_utils.py +++ /dev/null @@ -1,124 +0,0 @@ -# -*- coding: utf-8-*- -import smtplib -from email.MIMEText import MIMEText -import urllib2 -import re -import requests -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'] + \ - ",

Here are your top headlines:" + BODY - BODY += "
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 ", 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)) diff --git a/jasper.py b/jasper.py index 5f08e68..7223469 100755 --- a/jasper.py +++ b/jasper.py @@ -10,6 +10,10 @@ import argparse from client.diagnose import Diagnostics from client import vocabcompiler, tts, stt, jasperpath + +# Add jasperpath.LIB_PATH to sys.path +sys.path.append(jasperpath.LIB_PATH) + from client.conversation import Conversation parser = argparse.ArgumentParser(description='Jasper Voice Control Center') @@ -25,8 +29,6 @@ else: # Change CWD to jasperpath.LIB_PATH os.chdir(jasperpath.LIB_PATH) -# Add jasperpath.LIB_PATH to sys.path -sys.path.append(jasperpath.LIB_PATH) class Jasper(object): def __init__(self): -- cgit v1.3.1