-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (52 loc) · 1.7 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
import yfinance as yf
import logging
from twilio.twiml.messaging_response import MessagingResponse
from flask import Flask, request
from datetime import date
app = Flask(__name__)
@app.route("/sms", methods=["POST"])
def sms() -> str:
number = request.form.get["From"]
stock_symbol = request.form.get["Body"]
logging.info(f"received request from {number}: {stock_symbol}")
message = handle_sms(stock_symbol)
return message
@app.route("/", methods=["GET"])
def render_get() -> str:
get_slash='OK'
return get_slash
@app.route("/", methods=["POST"])
def render_post() -> str:
return "OK"
def handle_sms(stock_symbol: str) -> str:
date, open, close, high, low = get_stock_price(stock_symbol)
message = message_structure(
date,
stock_symbol,
open,
close,
high,
low,
)
resp = MessagingResponse()
resp.message(message)
return str(resp)
def get_stock_price(company: str):
tickers = yf.Ticker(company)
company_hist = tickers.history(period="1d")
prices = company_hist[["Open", "High", "Low", "Close"]]
stock_date = date.today()
open = prices["Open"].values[0]
close = prices["Close"].values[0]
high = prices["High"].values[0]
low = prices["Low"].values[0]
return stock_date, open, close, high, low
def message_structure(
stock_date: date, ticker: str, open: float, close: float, high: float, low: float
) -> str:
strdate = stock_date.strftime("%y-%m-%d")
message = f"Date:{strdate}\nCompany:{ticker}\nOpen:{open:.2f}\nClose:{close:.2f}\nHigh:{high:.2f}\nLow:{low:.2f}"
return message
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
app.app_context().push()