-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
233 lines (201 loc) Β· 6.9 KB
/
app.py
File metadata and controls
233 lines (201 loc) Β· 6.9 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
from flask import Flask, request, jsonify
import yfinance as yf
from flasgger import Swagger
from datetime import datetime, timedelta
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from openai_service import get_response
from dateutil import parser
from news_crawler import store_latest_news
from news_crawler import fetch_latest_news
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={
r"/*": {
"origins": [
"http://localhost:3000",
"https://easystogy.store",
"https://easy-stock-techping.netlify.app"
]
}
})
swagger = Swagger(app)
COMPANY_TICKERS = {
"μΌμ±μ μ": "005930.KS",
"νλμ°¨": "005380.KS",
"LGμλμ§μ루μ
": "373220.KQ"
}
STOCK_FIELDS = {
"μκ°": "Open",
"μ΅κ³ κ°": "High",
"μ΅μ κ°": "Low",
"μ’
κ°": "Close",
"κ±°λλ": "Volume"
}
NEWS_TICKERS = {
"μΌμ±μ μ": "005930",
"LGμλμ§μ루μ
": "373220",
"SKνμ΄λμ€": "000660",
"νλμ°¨": "005380",
"λΉμμ΄μΉμμ΄": "083650"
}
@app.route('/stock-info', methods=['POST'])
def get_stock_info():
"""
μ£Όμ μ 보λ₯Ό μ²λ¦¬νλ API
---
parameters:
- name: body
in: body
required: true
schema:
type: object
properties:
company_name:
type: string
description: νμ¬λͺ
example: μΌμ±μ μ
stock_field:
type: string
description: μ£Όμ μ 보 νλ (μκ°, μ’
κ° λ±)
example: μ’
κ°
date:
type: string
description: λ μ§ (YYYY-MM-DD)
example: "2023-12-01"
responses:
200:
description: μ£Όμ λ°μ΄ν°
400:
description: μλͺ»λ μμ²
404:
description: λ°μ΄ν° μμ
"""
try:
data = request.json
company_name = data.get("company_name")
stock_field = data.get("stock_field")
date = data.get("date")
print(f"νμ¬λͺ
: {company_name}, μ£Όμ μ 보: {stock_field}, λ μ§: {date}")
if company_name not in COMPANY_TICKERS:
return jsonify({"error": f"μ§μνμ§ μλ νμ¬μ
λλ€: {company_name}"}), 400
if stock_field not in STOCK_FIELDS:
return jsonify({"error": f"μ§μνμ§ μλ μ 보μ
λλ€: {stock_field}"}), 400
try:
start_date = parser.parse(date).date()
formatted_date = start_date.strftime("%Yλ
%mμ %dμΌ")
end_date = start_date + timedelta(days=1)
end_date_str = end_date.strftime("%Y-%m-%d")
except Exception as e:
return jsonify({"error": f"λ μ§ νμμ΄ μλͺ»λμμ΅λλ€: {str(e)}"}), 400
ticker = COMPANY_TICKERS[company_name]
stock_data = yf.download(ticker, start=date, end=end_date_str)
if stock_data.empty:
return jsonify({"error": f"ν΄λΉ λ μ§μ λν μ£Όμ μ λ³΄κ° μμ΅λλ€: {date}"}), 404
field = STOCK_FIELDS[stock_field]
value = stock_data.iloc[0][field]
value = float(value)
if stock_field == "κ±°λλ":
return jsonify({"message": f"{formatted_date}, {company_name}μ {stock_field}μ {value:,.0f}μ£Όμ
λλ€."})
else:
return jsonify({"message": f"{formatted_date}, {company_name}μ {stock_field}λ {value:.0f}μμ
λλ€."})
except Exception as e:
return jsonify({"error": f"μλ² μλ¬: {str(e)}"}), 500
@app.route('/ask', methods=['POST'])
def ask():
"""
μ¬μ©μ μ§λ¬Έμ μ²λ¦¬νλ API
---
parameters:
- name: body
in: body
required: true
schema:
type: object
properties:
prompt:
type: string
description: μ¬μ©μ μ§λ¬Έ
example: "PBRμ΄λ 무μμΈκ°μ?"
responses:
200:
description: OpenAI μλ΅
400:
description: μλͺ»λ μμ²
500:
description: μλ² μλ¬
"""
try:
data = request.json
prompt = data.get("prompt", "")
if not prompt:
return jsonify({"error": "μ§λ¬Έμ΄ λΉμ΄μμ΅λλ€."}), 400
response = get_response(prompt)
return jsonify({"response": response})
except Exception as e:
return jsonify({"error": str(e)}), 500
from news_crawler import fetch_latest_news # μ΄λ―Έ μ μλμ΄ μλ ν¨μ
@app.route('/latest-news', methods=['GET'])
def latest_news():
"""
μ’
λͺ©λ³ μ΅μ λ΄μ€ 5κ°λ₯Ό λ€μ΄λ² κΈμ΅ λ΄μ€ APIμμ μ‘°ννλ API
---
parameters:
- name: ticker
in: query
type: string
required: true
enum: ["μΌμ±μ μ", "LGμλμ§μ루μ
", "SKνμ΄λμ€", "νλμ°¨", "λΉμμ΄μΉμμ΄"]
description: μ’
λͺ© μ΄λ¦
example: "μΌμ±μ μ"
responses:
200:
description: λ΄μ€ 리μ€νΈ λ°ν
400:
description: μ ν¨νμ§ μμ ticker
500:
description: μλ² μλ¬
"""
try:
ticker_name = request.args.get("ticker")
if not ticker_name or ticker_name not in NEWS_TICKERS:
return jsonify({"error": f"μ§μνμ§ μλ tickerμ
λλ€: {ticker_name}"}), 400
ticker_code = NEWS_TICKERS[ticker_name]
news_items = fetch_latest_news(ticker_code)
return jsonify({"ticker": ticker_name, "news": news_items}), 200
except Exception as e:
return jsonify({"error": f"μλ² μλ¬: {str(e)}"}), 500
@app.route('/crawl-news', methods=['GET'])
def crawl_news():
"""
μ΅μ λ΄μ€λ₯Ό ν¬λ‘€λ§νκ³ νΉμ tickerλ³λ‘ νν°λ§νλ API
---
parameters:
- name: ticker
in: query
type: string
required: false
enum: ["μΌμ±μ μ", "LGμλμ§μ루μ
", "SKνμ΄λμ€", "νλμ°¨", "λΉμμ΄μΉμμ΄"]
description: "νΉμ μ’
λͺ©μ λ΄μ€λ§ κ°μ Έμ€λ €λ©΄ μ ννμΈμ"
example: "μΌμ±μ μ"
responses:
200:
description: ν¬λ‘€λ§ μλ£ λ° ν΄λΉ ticker λ΄μ€ λ°ν
400:
description: μ ν¨νμ§ μμ ticker κ° μ
λ ₯
500:
description: μλ² μλ¬ λ°μ
"""
try:
ticker_name = request.args.get("ticker")
if ticker_name and ticker_name not in NEWS_TICKERS:
return jsonify({"error": f"Invalid ticker name: {ticker_name}"}), 400
tickers_to_fetch = {ticker_name: NEWS_TICKERS[ticker_name]} if ticker_name else NEWS_TICKERS
news_data = store_latest_news(tickers_to_fetch)
return jsonify({"message": "λ΄μ€ ν¬λ‘€λ§ μλ£", "news": news_data}), 200
except Exception as e:
print(f"π₯ ERROR: {e}")
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run('0.0.0.0',port=5000,debug=True)