import time from uuid import uuid4 from applications.log import logging class Record(object): """ 搜索接口处理逻辑 """ def __init__(self, params, mysql_client, config): self.flow_pool_level = None self.content_id = None self.account_name = None self.contents = None self.title = None self.gh_id = None self.params = params self.mysql_client = mysql_client self.article_match_video_table = config.article_match_video_table self.article_text_table = config.article_text_table self.trace_id = "search-{}-{}".format(str(uuid4()), str(int(time.time()))) def check_params(self): """ 检查请求params :return: """ try: self.gh_id = self.params['ghId'] self.title = self.params['title'].split("@@")[-1].replace("'", "") self.contents = self.params['content'].replace("'", "") self.account_name = self.params['accountName'].replace("'", "") self.content_id = self.params['articleId'] self.flow_pool_level = self.params['flowPoolLevelTag'] logging( code="1001", info="搜索视频内容接口请求成功, 参数校验成功", port="title_to_search", trace_id=self.trace_id, data=self.params ) return None except Exception as e: result = { "status": "fail", "code": 1, "message": str(e), "info": "params check error" } logging( code="4001", info="搜索视频内容接口请求成功, 参数校验失败", port="title_to_search", trace_id=self.trace_id, data=self.params ) return result async def input_into_article_match_video_table(self): """ 把数据插入待处理队列 :return: """ request_time = int(time.time()) insert_sql = f""" INSERT INTO {self.article_match_video_table} (trace_id, content_id, flow_pool_level, gh_id, account_name, request_timestamp) VALUES (%s, %s, %s, %s, %s, %s); """ await self.mysql_client.async_insert( sql=insert_sql, params=( self.trace_id, self.content_id, self.flow_pool_level, self.gh_id, self.account_name, request_time ) ) logging( code="1001", info="请求文章存储到 long_articles_match_videos中", function="Record", trace_id=self.trace_id ) async def input_into_article_text_table(self): """ :return: """ insert_sql = f""" INSERT INTO {self.article_text_table} (content_id, article_title, article_text) values (%s, %s, %s); """ try: await self.mysql_client.async_insert( sql=insert_sql, params=( self.content_id, self.title, self.contents ) ) logging( code="1002", info="请求文章存储到 long_articles_text中", function="Record", trace_id=self.trace_id ) except Exception as e: logging( code="1002", info="请求文章 id 已经存储在 long_article_text中 {}".format(e), function="Record", trace_id=self.trace_id ) async def deal(self): """ deal :return: """ params_error = self.check_params() if params_error: return params_error else: # 记录数据 await self.input_into_article_match_video_table() await self.input_into_article_text_table() res = { "status": "success input to article queue", "code": 0, "traceId": self.trace_id } return res