forked from DanielleSucher/BookQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
174 lines (145 loc) · 5.13 KB
/
app.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
from flask import Flask, request
import twilio.twiml
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.mail import Mail, Message
from random import randrange
import re
app = Flask(__name__)
app.config.update(
SQLALCHEMY_DATABASE_URI=os.environ['DATABASE_URL'],
MAIL_SERVER=os.environ.get('MAILGUN_SMTP_SERVER', 'smtp.mailgun.org'),
MAIL_PORT=os.environ.get('MAILGUN_SMTP_PORT', 587),
MAIL_USERNAME=os.environ.get('MAILGUN_SMTP_LOGIN', None),
MAIL_PASSWORD=os.environ.get('MAILGUN_SMTP_PASSWORD', None),
DEFAULT_MAIL_SENDER="[email protected]")
db = SQLAlchemy(app)
mail = Mail(app)
@app.route("/sms", methods=['GET', 'POST'])
def sms():
from_number = request.values.get('From', None)
user = find_or_create_user(from_number)
text_content = request.form['Body'].lower()
if re.search("email:", text_content):
update_user_email(user, text_content)
message = "Your email has been saved."
elif text_content.startswith('list'):
if text_content == 'list':
message = list_categories(user)
else:
list_cat = text_content.strip('list').strip()
message = list_books_for_category(user, list_cat)
elif text_content.startswith('del'):
book = text_content.strip('del').strip()
mark_book_as_read(user, book)
message = "deleted" + book
else:
book_and_cat = request.form['Body'].partition['***']
add_new_boot(user, book_and_cat[0], book_and_cat[2].strip())
message = "Your book has been added."
resp = twilio.twiml.Response()
resp.sms(message)
return str(resp)
# email routes
# Category should be on second line if email.
@app.route("/book", methods=['GET', 'POST'])
def book():
user = find_user_from_email_headers(request)
if user:
book_cat = request.form['body-plain'].split('\n')
if book_cat.length > 1:
add_new_book(user, book_cat[0], book_cat[1])
else:
add_new_book(user, book_cat[0], "NOCAT")
if user.email:
message = Message("Your book has been added.",
recipients=[user.email])
mail.send(message)
return 'ok'
@app.route("/list", methods=['GET', 'POST'])
def list():
user = find_user_from_email_headers(request)
if user and user.email:
message = Message("Your current books in BookQueue",
recipients=[user.email])
message.body = booklist_message(user)
mail.send(message)
return 'ok'
# shared functions for routes
def add_new_book(user, book_info, cat):
if cat:
book = Book(user.id, book_info, cat)
else:
book = Book(user.id, book_info, "NOCAT")
db.session.add(book)
db.session.commit()
def list_categories(user):
cats = Set()
msg = "Categories: "
books = Book.query.filter(Book.user_id == user.id, Book.done == false).all()
for book in books:
cats.add(book.category)
for cat in cats:
msg += cat + "\n"
return msg
def list_books_for_category(user, cat):
msg = ""
books = Book.query.filter(
Book.user_id == user.id, Book.category == cat, Book.done == false).all()
for book in books:
msg += book + "\n"
return msg
def booklist_message(user):
msg = ""
books = Book.query.filter(Book.user_id == user.id, Book.done == false).all()
for book in books:
msg += book.info + "\n"
return msg
def mark_book_as_read(user, book):
book = Book.query.filter(Book.user_id == user.id, Book.info == book)
book.done = True
db.session.commit()
def find_or_create_user(from_number):
query = User.query.filter(User.phonenumber == from_number)
if int(query.count()) > 0:
return query.first()
else:
user = User(from_number, None)
db.session.add(user)
db.session.commit()
return user
def find_user_from_email_headers(request):
from_email = request.form['sender'].lower()
query = User.query.filter(User.email == from_email)
if int(query.count()) > 0:
return query.first()
else:
return None
def update_user_email(user, text_content):
user.email = re.search("(?<=email:).+", text_content).group(0)
db.session.commit()
# models
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
phonenumber = db.Column(db.String(20), unique=True)
email = db.Column(db.String(120), unique=True, nullable=True)
def __init__(self, phonenumber, email):
self.phonenumber = phonenumber
self.email = email
def __repr__(self):
return '<phone_number %r>' % self.phonenumber
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer)
info = db.Column(db.String(240))
category = db.Column(db.String(120))
done = db.Column(db.Boolean)
def __init__(self, user_id, info, category):
self.user_id = user_id
self.info = info
self.category = category
def __repr__(self):
return '<info %r>' % self.info
if __name__ == '__main__':
port = int(os.environ.get('PORT', 33507))
app.run(host='0.0.0.0', port=port)