-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (65 loc) · 2.15 KB
/
app.py
File metadata and controls
79 lines (65 loc) · 2.15 KB
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
from aws_boto3 import *
from stocks import *
from InvalidRequest import InvalidRequest
def msg_handler(phone_number, msg):
split_msg = msg.split()
return_msg = ""
try:
if len(split_msg) == 1:
return_msg = one_arg_handler(phone_number, split_msg[0])
elif len(split_msg) == 2:
return_msg = two_arg_handler(phone_number, split_msg)
else:
raise InvalidRequest
except InvalidRequest:
return_msg = "Invalid request"
return return_msg
def one_arg_handler(phone_number, arg):
return_msg = ""
if arg[0] == "$":
ticker = arg[1:]
return_msg = retrieve_stock_text(ticker)
elif arg == "Show":
return_msg = show_handler(phone_number)
else:
raise InvalidRequest
return return_msg
def two_arg_handler(phone_number, arg):
user_request = arg[0]
request_info = arg[1]
return_msg = ""
if user_request == "Show":
return_msg = show_handler(phone_number)
elif user_request == "Add" and request_info[0] == "$":
return_msg = add_handler(phone_number, request_info[1:])
elif user_request == "Remove" and request_info[0] == "$":
return_msg = remove_handler(phone_number, request_info[1:])
else:
raise InvalidRequest
return return_msg
def add_handler(phone_number, msg):
print(phone_number)
stock_info = price(msg)
if not stock_info:
raise InvalidRequest
update_add(phone_number, msg)
return "Added ${} to your watchlist".format(msg)
def remove_handler(phone_number, msg):
remove(phone_number, msg)
return "Removed ${} to your watchlist".format(msg)
def show_handler(phone_number):
watchlist_printout = ""
watchlist = retrieve(phone_number)
if not watchlist:
return "Nothing in watchlist."
for stock in watchlist:
watchlist_printout += retrieve_stock_text(stock) + "\n"
return watchlist_printout
def retrieve_stock_text(ticker):
stock_info = price(ticker)
msg = ""
if not stock_info:
msg = "Invalid request"
else:
msg = "${}: Ask: ${}, Bid: ${}".format(ticker, stock_info[0], stock_info[1])
return msg