-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfear.py
More file actions
62 lines (44 loc) · 1.45 KB
/
Copy pathfear.py
File metadata and controls
62 lines (44 loc) · 1.45 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
from datetime import datetime
import fear_and_greed
import mysql.connector
from mysql.connector import Error
from dotenv import load_dotenv
import os
load_dotenv()
# 공포와 탐욕 지수 가져오는 함수
def get_fr_gd():
fg = fear_and_greed.get()
fg_sc = float(fg[0])
fg_sc = round(fg_sc, 2)
fg_st = fg[1]
return (fg_sc, fg_st)
def save_to_db(score, status):
connection = None
try:
db_host = os.getenv("DB_HOST")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
db_name = os.getenv("DB_NAME")
# MySQL 연결 설정
connection = mysql.connector.connect(
host=db_host,
user=db_user,
password=db_password,
database=db_name
)
cursor = connection.cursor()
today = datetime.now().strftime("%Y-%m-%d")
sql = "INSERT INTO fear_db (date, fear_greed_score, status) VALUES (%s, %s, %s)"
values = (today, score, status)
cursor.execute(sql, values)
connection.commit()
print(f"Data saved: {today}, {score}, {status}")
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
if connection and connection.is_connected():
cursor.close()
connection.close()
# 공포와 탐욕 지수 가져오기 및 DB 저장
fg_score, fg_status = get_fr_gd()
save_to_db(fg_score, fg_status)