Jelajahi Sumber

Merge branch '2024-10-10-luojunhui-bugfix' of Server/title_with_video into 2024-09-23newDbTasks

luojunhui 6 bulan lalu
induk
melakukan
f34b419c55
2 mengubah file dengan 57 tambahan dan 27 penghapusan
  1. 24 7
      applications/db/__init__.py
  2. 33 20
      server/api/get_off_videos.py

+ 24 - 7
applications/db/__init__.py

@@ -58,15 +58,32 @@ class AsyncMySQLClient(object):
         :param sql:
         :return:
         """
-        async with self.mysql_pool.acquire() as coon:
-            async with coon.cursor() as cursor:
+        async with self.mysql_pool.acquire() as conn:
+            async with conn.cursor() as cursor:
                 try:
                     await cursor.execute(sql, params)
                     affected_rows = cursor.rowcount
-                    await coon.commit()
+                    await conn.commit()
+                    return affected_rows
+                except Exception as e:
+                    await conn.rollback()
+                    raise
+
+    async def async_insert_many(self, sql, params_list):
+        """
+        :param sql:
+        :param params_list:
+        :return:
+        """
+        async with self.mysql_pool.acquire() as conn:
+            async with conn.cursor() as cursor:
+                try:
+                    await cursor.executemany(sql, params_list)
+                    affected_rows = cursor.rowcount
+                    await conn.commit()
                     return affected_rows
                 except Exception as e:
-                    await coon.rollback()
+                    await conn.rollback()
                     raise
 
     async def __aenter__(self):
@@ -128,7 +145,7 @@ class TaskMySQLClient(object):
         :param sql:
         :return:
         """
-        async with self.mysql_pool.acquire() as coon:
-            async with coon.cursor() as cursor:
+        async with self.mysql_pool.acquire() as conn:
+            async with conn.cursor() as cursor:
                 await cursor.execute(sql, params)
-                await coon.commit()
+                await conn.commit()

+ 33 - 20
server/api/get_off_videos.py

@@ -4,6 +4,8 @@
 import json
 import time
 
+from applications.log import logging
+
 
 class GetOffVideos(object):
     """
@@ -44,29 +46,40 @@ class GetOffVideos(object):
         result = await self.mysql_client.async_select(sql=select_sql)
         if result:
             video_list = json.loads(result[0][0])
-            for video in video_list:
-                video_id = video['videoId']
-                try:
-                    update_sql = f"""
+            info_list = [
+                (
+                    item['videoId'],
+                    int(time.time()),
+                    1,
+                    self.trace_id
+                )
+                for item in video_list
+            ]
+            try:
+                insert_sql = f"""
                     INSERT INTO {self.get_off_videos}
                     (video_id, publish_time, video_status, trace_id)
                     values 
-                    (%s, %s, %s, %s);
-                    """
-                    await self.mysql_client.async_insert(
-                        sql=update_sql,
-                        params=(video_id, int(time.time()), 1, self.trace_id)
-                    )
-                    return {
-                        "status": "success",
-                        "traceId": self.trace_id
-                    }
-                except Exception as e:
-                    return {
-                        "status": "fail",
-                        "traceId": self.trace_id,
-                        "msg": "insert fail---{}".format(e)
-                    }
+                    (%s, %s, %s, %s);"""
+                await self.mysql_client.async_insert_many(
+                    sql=insert_sql,
+                    params_list=info_list
+                )
+                return {
+                    "status": "success",
+                    "traceId": self.trace_id
+                }
+            except Exception as e:
+                logging(
+                    code="4123",
+                    info="自动下架视频处理失败: {}".format(e),
+                    function="get_off_videos"
+                )
+                return {
+                    "status": "fail",
+                    "traceId": self.trace_id,
+                    "msg": "insert fails, {}".format(e)
+                }
         else:
             return {
                 "status": "fail",