Преглед изворни кода

Add config and common/database

StrayWarrior пре 5 месеци
родитељ
комит
de8726b499
5 измењених фајлова са 104 додато и 0 уклоњено
  1. 0 0
      config/__init__.py
  2. 21 0
      config/dev.py
  3. 16 0
      config/prod.py
  4. 0 0
      src/common/__init__.py
  5. 67 0
      src/common/database.py

+ 0 - 0
config/__init__.py


+ 21 - 0
config/dev.py

@@ -0,0 +1,21 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+# vim:fenc=utf-8
+#
+# Copyright © 2024 StrayWarrior <i@straywarrior.com>
+
+
+class Config:
+    MYSQL_LONG_ARTICLES = {
+        'host': 'rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
+        'port': 3306,
+        'user': 'changwen_admin',
+        'password': 'changwen@123456',
+        'db': 'long_articles',
+        'charset': 'utf8mb4'
+    }
+
+    CLARK_ROBOT = {
+        'webhook': 'https://open.feishu.cn/open-apis/bot/v2/hook/93787b70-33d3-42c1-beae-c09310c9b38b',
+        'keyword': '外部流量数据任务报警'
+    }

+ 16 - 0
config/prod.py

@@ -0,0 +1,16 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+# vim:fenc=utf-8
+#
+# Copyright © 2024 StrayWarrior <i@straywarrior.com>
+
+
+class Config:
+    MYSQL_LONG_ARTICLES = {
+        'host': 'rm-bp14529nwwcw75yr1ko.mysql.rds.aliyuncs.com',
+        'port': 3306,
+        'user': 'changwen_admin',
+        'password': 'changwen@123456',
+        'db': 'long_articles',
+        'charset': 'utf8mb4'
+    }

+ 0 - 0
src/common/__init__.py


+ 67 - 0
src/common/database.py

@@ -0,0 +1,67 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+# vim:fenc=utf-8
+#
+# Copyright © 2024 StrayWarrior <i@straywarrior.com>
+
+import pymysql
+
+class MySQLManager:
+    def __init__(self, config):
+        self.config = config
+
+    def select(self, sql):
+        """
+        sql: SQL to execute, string
+        """
+        conn = pymysql.connect(**self.config)
+        cursor = conn.cursor()
+        cursor.execute(sql)
+        data = cursor.fetchall()
+        # do not handle exception
+        cursor.close()
+        conn.close()
+        return data
+
+    def execute(self, sql):
+        conn = pymysql.connect(**self.config)
+        cursor = conn.cursor()
+        try:
+            cursor.execute(sql)
+            affected_rows = cursor.rowcount
+            conn.commit()
+            return affected_rows
+        except Exception as e:
+            conn.rollback()
+            raise e
+        finally:
+            conn.close()
+
+    def batch_insert(self, table, data, columns=None):
+        """
+        table: table name, string
+        data: data, list[tuple] or list[dict]
+        columns: column names, list, required if data is list[tuple]
+        """
+        if data is None or len(data) == 0:
+            return
+        conn = pymysql.connect(**self.config)
+        try:
+            if isinstance(data[0], dict):
+                keys = data[0].keys()
+                columns_str = ','.join(keys)
+                placeholders_str = ','.join([f'%({key})s' for key in keys])
+            else:
+                if len(data[0]) != len(columns):
+                    raise Exception("data length != column length")
+                columns_str = ','.join(columns)
+                placeholders_str = ','.join(['%s'] * len(data[0]))
+            with conn.cursor() as cursor:
+                sql_str = f"INSERT INTO {table} ({columns_str}) VALUES ({placeholders_str})"
+                cursor.executemany(sql_str, data)
+                conn.commit()
+        except pymysql.MySQLError as e:
+            print(f"Error in batch_insert: {e}")
+            conn.rollback()
+            raise e
+        conn.close()