From 019a1419d1d481b1e5a3a56aeb4be6b158c4082e Mon Sep 17 00:00:00 2001 From: icurfer Date: Thu, 3 Oct 2024 21:26:06 +0900 Subject: [PATCH] =?UTF-8?q?=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=20=EB=B0=8F=20db=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 12 +++++++-- package/GetConfig.py | 18 +++++++++++--- package/MariaDB.py | 57 +++++++++++++++++++++++++++++++++++++++++++ package/__init__.py | 3 ++- package/convert_md.py | 2 +- 5 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 package/MariaDB.py diff --git a/main.py b/main.py index 9b52362..f380354 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,12 @@ -import package as pkg +from package import GetConfig +from package import MariaDB + +# 2024-10-03 환경 변수 호출 +config = GetConfig.GetConfig() +dict_data = config.get_config_as_dict() + +# 2024-10-03 db에서 url정보 호출 +db = MariaDB.MariaDB(dict_data) +url = db.fetch_data_from_mariadb()['url'] -url = pkg.get_url.fetch_data_from_mariadb() \ No newline at end of file diff --git a/package/GetConfig.py b/package/GetConfig.py index de93462..8dc35f3 100644 --- a/package/GetConfig.py +++ b/package/GetConfig.py @@ -5,14 +5,24 @@ load_dotenv(r'./.env.dev') class GetConfig: def __init__(self): - self.host = os.getenv('DB_HOST') - self.user = os.getenv('DB_USER') - self.password = os.getenv('DB_PASSWORD') - self.database = os.getenv('DB_NAME') + self.db_host = os.getenv('DB_HOST') + self.db_user = os.getenv('DB_USER') + self.db_pw = os.getenv('DB_PASSWORD') + self.db_database = os.getenv('DB_NAME') + self.openai_api_key = os.getenv('OPEN_API_KEY') + self.wp_url = os.getenv('WP_URL') + self.wp_user = os.getenv('WP_USER') + self.wp_api_key = os.getenv('WP_API_KEY') + self.wp_post_style = os.getenv('WP_POST_STYLE') def show_config(self): for key, value in self.__dict__.items(): print(f"{key.upper()}: {value}") + + def get_config_as_dict(self): + # 인스턴스 속성을 딕셔너리로 반환 + return self.__dict__ + if __name__ == "__main__": # 결과 확인 diff --git a/package/MariaDB.py b/package/MariaDB.py new file mode 100644 index 0000000..90a52a7 --- /dev/null +++ b/package/MariaDB.py @@ -0,0 +1,57 @@ +import mysql.connector +import os + +class MariaDB: + + def __init__(self, dict): + self.db_host = dict['db_host'] + self.db_user = dict['db_user'] + self.db_pw = dict['db_pw'] + self.db_database = dict['db_database'] + + def show_config(self): + for key, value in self.__dict__.items(): + print(f"{key.upper()}: {value}") + + def fetch_data_from_mariadb(self): + try: + # 데이터베이스 연결 + connection = mysql.connector.connect( + host=self.db_host, + user=self.db_user, + password=self.db_pw, + database=self.db_database + ) + + # 커서 생성 + cursor = connection.cursor(dictionary=True) + + # 쿼리 실행 + query = "SELECT * FROM healty_url_source ORDER BY idx DESC LIMIT 1;" + cursor.execute(query) + + # 결과 가져오기 + result = cursor.fetchone() + return result + + except mysql.connector.Error as err: + print(f"Error: {err}") + finally: + if connection.is_connected(): + cursor.close() + connection.close() + +if __name__ == "__main__": + + import GetConfig + + config = GetConfig.GetConfig() + # config 잘 가져오는지 확인 + config_dict = config.get_config_as_dict() + + # MariaDB 테스트 + dbg = MariaDB(config_dict) + # dbg.show_config() + url = dbg.fetch_data_from_mariadb() + print(url['url']) + \ No newline at end of file diff --git a/package/__init__.py b/package/__init__.py index 8ba67e1..ce27c6e 100644 --- a/package/__init__.py +++ b/package/__init__.py @@ -1 +1,2 @@ -from . import get_url \ No newline at end of file +from . import GetConfig +from . import MariaDB \ No newline at end of file diff --git a/package/convert_md.py b/package/convert_md.py index f94cf98..6f9ecf0 100644 --- a/package/convert_md.py +++ b/package/convert_md.py @@ -2,7 +2,7 @@ import os from dotenv import load_dotenv import markdown import package.translate_article as ta -import ../openai/open_ai as oa +import package.open_ai as oa # .env 파일에서 API 키 로드 load_dotenv(r'./.env.dev')