forked from ssideProject/ETF-history-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (74 loc) · 2.87 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
# -*- coding: utf-8 -*-
from chalice import Chalice
import sys
import json
import logging
import os
import time
from datetime import date, datetime, timedelta
app = Chalice(app_name='turtle-history-api')
@app.lambda_function()
def handler(event, context):
message = 'Hello {} {}!'.format(event['first_name'],
event['last_name'])
return {
'message' : message
}
@app.route('/')
def index():
return {'hello': 'world'}
@app.route('/meta')
def call_meta():
return app.current_request.to_dict()
@app.route('/history/{ticker}')
def history_tickers(ticker=None):
from chalicelib.downloader import Downloader
'''
ticker를 받아서 history를 보여준다.
https://aws.github.io/chalice/tutorials/basicrestapi.html 사용법
start_date=20010505
end_date=20201010
'''
if ticker is None:
return {'error':'Ticker is None...'}
today = datetime.now()
params = app.current_request.query_params if app.current_request.query_params else {}
start_date = convert_string_to_date(str(params['start'])) if 'start' in params else today - timedelta(weeks=52)
end_date = convert_string_to_date(params['end']) if 'end' in params else today
downloader = Downloader()
histries = downloader.fetchh_etf_dict(ticker=ticker, start_date=start_date, end_date=end_date)
return {'result': 'success', 'list':histries}
def convert_string_to_date(str_date:str, form='%Y%m%d'):
str_date = ''.join(str_date.split('.')) if str_date.split('.') else str_date
str_date = ''.join(str_date.split('-')) if str_date.split('-') else str_date
str_date = ''.join(str_date.split('/')) if str_date.split('/') else str_date
date_obj = datetime.strptime(str_date, form)
return date_obj
@app.route('/alarm', methods=['GET'])
def alarm_tickers():
from chalicelib.msg_maker import MsgMaker
from chalicelib.alarm_service import alarm_to_slack
'''
us-east-1 시간이 적용된다. 하루의 장이 마감이 되면
tickerList 읽어서 Slack으로 알람을 쏴준다.
'''
ticker_str = os.environ['tickerList'] # `{"AGG":200,"BND":200}`
ticker_list = json.loads(ticker_str)
msg_maker = MsgMaker()
for key, value in ticker_list.items():
bind_content = msg_maker.make_today_MA(key, value)
alarm_to_slack(bind_content)
return {'result': 'success'}
def schedule_tickers():
from chalicelib.msg_maker import MsgMaker
from chalicelib.alarm_service import alarm_to_slack
'''
cron(min > hour > day(일) > month > weekday(요일))
'''
ticker_str = os.environ['tickerList'] # `{"AGG":200,"BND":200}`
ticker_list = json.loads(ticker_str)
msg_maker = MsgMaker()
for key, value in ticker_list.items():
bind_content = msg_maker.make_today_MA(key, value)
alarm_to_slack(bind_content)
return {'result': 'success'}