|
@@ -2,7 +2,7 @@ import os
|
|
|
import sys
|
|
|
import traceback
|
|
|
import uuid
|
|
|
-from datetime import date
|
|
|
+from datetime import date, datetime
|
|
|
from datetime import timedelta
|
|
|
from io import BytesIO
|
|
|
from typing import Optional, List
|
|
@@ -13,6 +13,7 @@ import yaml
|
|
|
from fastapi import APIRouter
|
|
|
from fastapi.responses import StreamingResponse
|
|
|
from loguru import logger
|
|
|
+from pangres import upsert
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
curr_path = os.path.abspath(os.path.dirname(__file__))
|
|
@@ -20,6 +21,7 @@ project_root_path = curr_path[:curr_path.find("video_to_word") + len("video_to_w
|
|
|
sys.path.append(project_root_path)
|
|
|
|
|
|
from config.url_and_db import toutiao_static_video_url, ai_word_engine
|
|
|
+from sqlalchemy import VARCHAR
|
|
|
|
|
|
from time_task.get_material_and_script_by_query_word import get_material_and_script
|
|
|
|
|
@@ -87,11 +89,11 @@ class ScriptConfigLst(BaseModel):
|
|
|
class QueryWordTaskInfoLst(BaseModel):
|
|
|
start_date: Optional[date] = Field(date.today() + timedelta(days=-30), description="开始日期-用于查询")
|
|
|
end_date: Optional[date] = Field(date.today(), description="结束日期-用于查询")
|
|
|
- search_word: Optional[str] = Field('', description="关键词-用于查询")
|
|
|
+ search_word: Optional[str] = Field('', description="关键词/推荐词-用于查询")
|
|
|
page_num: int = Field(1, description="第几页")
|
|
|
page_size: int = Field(10, description="每页的大小")
|
|
|
config_id: Optional[str] = Field('', description="脚本配置id")
|
|
|
- source_code: Optional[List[int]] = Field([0], description="数据来源编码{1:'内部创意', 2:'巨量创意', 3:'开眼快创'}")
|
|
|
+ source_code: Optional[List[int]] = Field([], description="数据来源编码{1:'内部创意', 2:'巨量创意', 3:'开眼快创'}")
|
|
|
|
|
|
|
|
|
class QueryWordAndRecommendedWordPair(BaseModel):
|
|
@@ -114,6 +116,21 @@ class AddScriptConfig(BaseModel):
|
|
|
}
|
|
|
|
|
|
|
|
|
+class DeleteScriptConfig(BaseModel):
|
|
|
+ config_id: str = Field(..., description="脚本配置id")
|
|
|
+ operator: str = Field(..., description="操作者")
|
|
|
+ user_id: str = Field(..., description="user_id")
|
|
|
+
|
|
|
+ class Config:
|
|
|
+ schema_extra = {
|
|
|
+ "example": {
|
|
|
+ "config_id": "71951bcb-0ef7-4ce0-9be5-c8aaf3128ab7",
|
|
|
+ "operator": "管理员",
|
|
|
+ "user_id": "e9ca23d68d884d4ebb19d07889727dae"
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@logger.catch
|
|
|
@router.post('/export_script_file/',
|
|
|
description="导出文件",
|
|
@@ -246,22 +263,25 @@ def get_script_config_lst(item: ScriptConfigLst):
|
|
|
def get_query_word_task_info_lst(item: QueryWordTaskInfoLst):
|
|
|
try:
|
|
|
end_date = item.end_date + timedelta(days=1)
|
|
|
- source_code_lst = item.source_code * 2 if len(item.source_code) == 1 else item.source_code
|
|
|
+ source_code_lst = [-1, -2] if len(item.source_code) == 0 else (item.source_code * 2 if len(item.source_code) == 1 else item.source_code)
|
|
|
df = pd.DataFrame()
|
|
|
if item.config_id != '':
|
|
|
- sql = f"select distinct(query_word) query_word from ctop_ai_script_query_word_config where config_id = '{item.config_id}'"
|
|
|
- query_word_lst = list(pd.read_sql(sql, ai_word_engine).query_word.values)
|
|
|
- if len(query_word_lst) > 0:
|
|
|
- query_word_lst = query_word_lst * 2 if len(query_word_lst) == 1 else query_word_lst
|
|
|
- sql = f"select * from ctop_ai_query_word_task_record where query_word in {tuple(query_word_lst)}" \
|
|
|
+ sql = f"select query_word, recommended_word from ctop_ai_script_query_word_config where config_id = '{item.config_id}'"
|
|
|
+ config_df = pd.read_sql(sql, ai_word_engine)
|
|
|
+ query_word_set = set(config_df['query_word'].values)
|
|
|
+ recommended_word_set = set(config_df['recommended_word'].values)
|
|
|
+ word_lst = list(query_word_set.union(recommended_word_set))
|
|
|
+ if len(word_lst) > 0:
|
|
|
+ word_lst = word_lst * 2 if len(word_lst) == 1 else word_lst
|
|
|
+ sql = f"select * from ctop_ai_query_word_task_record where query_word in {tuple(word_lst)}" \
|
|
|
f"and stat_date >= '{item.start_date}' and stat_date < '{end_date}' " \
|
|
|
- f"and ('{item.source_code}' = '[0]' or source_code in {tuple(source_code_lst)}) " \
|
|
|
+ f"and ('{item.source_code}' = '[]' or source_code in {tuple(source_code_lst)}) " \
|
|
|
f"and ('{item.search_word}' = '' or query_word = '{item.search_word}')"
|
|
|
df = pd.read_sql(sql, ai_word_engine)
|
|
|
else:
|
|
|
sql = f"select * from ctop_ai_query_word_task_record where " \
|
|
|
f"stat_date >= '{item.start_date}' and stat_date < '{end_date}' " \
|
|
|
- f"and ('{item.source_code}' = '[0]' or source_code in {tuple(source_code_lst)}) " \
|
|
|
+ f"and ('{item.source_code}' = '[]' or source_code in {tuple(source_code_lst)}) " \
|
|
|
f"and ('{item.search_word}' = '' or query_word = '{item.search_word}')"
|
|
|
df = pd.read_sql(sql, ai_word_engine)
|
|
|
if not df.empty:
|
|
@@ -345,6 +365,55 @@ def add_script_config(item: AddScriptConfig):
|
|
|
|
|
|
|
|
|
@logger.catch
|
|
|
+@router.post('/delete_script_config/',
|
|
|
+ description="删除脚本配置",
|
|
|
+ summary='删除脚本配置',
|
|
|
+ response_model=BaseResponse)
|
|
|
+def delete_script_config(item: DeleteScriptConfig):
|
|
|
+ try:
|
|
|
+ sql = f"select * from ctop_ai_script_query_word_config where config_id = '{item.config_id}'"
|
|
|
+ config_df = pd.read_sql(sql, ai_word_engine)
|
|
|
+
|
|
|
+ column_type = {'config_id': VARCHAR(36),
|
|
|
+ 'query_word': VARCHAR(36),
|
|
|
+ 'recommended_word': VARCHAR(36)}
|
|
|
+
|
|
|
+ update_config_df = config_df.copy(deep=True)
|
|
|
+ update_config_df['end_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
+ update_config_df.set_index(['config_id', 'query_word', 'recommended_word', 'operate_type'], drop=True, inplace=True)
|
|
|
+
|
|
|
+ upsert(engine=ai_word_engine,
|
|
|
+ df=update_config_df,
|
|
|
+ table_name='ctop_ai_script_query_word_config',
|
|
|
+ if_row_exists='update',
|
|
|
+ dtype=column_type)
|
|
|
+
|
|
|
+ add_config_df = config_df.copy(deep=True)
|
|
|
+ add_config_df['operate_type'] = 3
|
|
|
+ add_config_df['user_id'] = item.user_id
|
|
|
+ add_config_df['operator'] = item.operator
|
|
|
+ add_config_df['start_time'] = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
+ add_config_df.drop(labels='end_time', axis=1, inplace=True)
|
|
|
+
|
|
|
+ add_config_df.set_index(['config_id', 'query_word', 'recommended_word', 'operate_type'], drop=True, inplace=True)
|
|
|
+ upsert(engine=ai_word_engine,
|
|
|
+ df=add_config_df,
|
|
|
+ table_name='ctop_ai_script_query_word_config',
|
|
|
+ if_row_exists='update',
|
|
|
+ dtype=column_type)
|
|
|
+
|
|
|
+ logger.info(f"request body: {item}, code:0, message: delete_script_config success")
|
|
|
+ return {"code": 0,
|
|
|
+ "message": "delete success",
|
|
|
+ "success": True}
|
|
|
+ except:
|
|
|
+ logger.error(f"request body: {item}, code:-1, message: delete_script_config fail {traceback.format_exc()}")
|
|
|
+ return {"code": -1,
|
|
|
+ "message": traceback.format_exc(),
|
|
|
+ "success": False}
|
|
|
+
|
|
|
+
|
|
|
+@logger.catch
|
|
|
@router.post('/get_material_and_script_time_task/',
|
|
|
response_model=BaseResponse,
|
|
|
description="获取素材和脚本任务",
|
|
@@ -361,3 +430,10 @@ def get_material_and_script_time_task():
|
|
|
return {"code": -1,
|
|
|
"success": False,
|
|
|
"message": f"{date.today().strftime('%Y-%m-%d')},获取素材和脚本任务执行发生异常 .{traceback.format_exc()}"}
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ req = DeleteScriptConfig(config_id='4113ec60-4b92-4a4a-9dc5-417a29df9b65',
|
|
|
+ operator='隋炎均',
|
|
|
+ user_id='f75b91a1a23946688ab1d93a65d0a435')
|
|
|
+ delete_script_config(req)
|