record.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import time
  2. from uuid import uuid4
  3. from applications.log import logging
  4. class Record(object):
  5. """
  6. 搜索接口处理逻辑
  7. """
  8. def __init__(self, params, mysql_client, config):
  9. self.flow_pool_level = None
  10. self.content_id = None
  11. self.account_name = None
  12. self.contents = None
  13. self.title = None
  14. self.gh_id = None
  15. self.publish_flag = None
  16. self.params = params
  17. self.mysql_client = mysql_client
  18. self.article_match_video_table = config.article_match_video_table
  19. self.article_text_table = config.article_text_table
  20. self.trace_id = "search-{}-{}".format(str(uuid4()), str(int(time.time())))
  21. def check_params(self):
  22. """
  23. 检查请求params
  24. :return:
  25. """
  26. try:
  27. self.gh_id = self.params['ghId']
  28. self.title = self.params['title'].split("@@")[-1].replace("'", "")
  29. self.contents = self.params['content'].replace("'", "")
  30. self.account_name = self.params['accountName'].replace("'", "")
  31. self.content_id = self.params['articleId']
  32. self.flow_pool_level = self.params['flowPoolLevelTag']
  33. self.publish_flag = self.params.get('publishFlag', 1)
  34. logging(
  35. code="1001",
  36. info="搜索视频内容接口请求成功, 参数校验成功",
  37. port="title_to_search",
  38. trace_id=self.trace_id,
  39. data=self.params
  40. )
  41. return None
  42. except Exception as e:
  43. result = {
  44. "status": "fail",
  45. "code": 1,
  46. "message": str(e),
  47. "info": "params check error"
  48. }
  49. logging(
  50. code="4001",
  51. info="搜索视频内容接口请求成功, 参数校验失败",
  52. port="title_to_search",
  53. trace_id=self.trace_id,
  54. data=self.params
  55. )
  56. return result
  57. async def input_into_article_match_video_table(self):
  58. """
  59. 把数据插入待处理队列
  60. :return:
  61. """
  62. request_time = int(time.time())
  63. insert_sql = f"""
  64. INSERT INTO {self.article_match_video_table}
  65. (trace_id, content_id, flow_pool_level, gh_id, account_name, request_timestamp, publish_flag)
  66. VALUES
  67. (%s, %s, %s, %s, %s, %s, %s);
  68. """
  69. await self.mysql_client.async_insert(
  70. sql=insert_sql,
  71. params=(
  72. self.trace_id,
  73. self.content_id,
  74. self.flow_pool_level,
  75. self.gh_id,
  76. self.account_name,
  77. request_time,
  78. self.publish_flag
  79. )
  80. )
  81. logging(
  82. code="1001",
  83. info="请求文章存储到 long_articles_match_videos中",
  84. function="Record",
  85. trace_id=self.trace_id
  86. )
  87. async def input_into_article_text_table(self):
  88. """
  89. :return:
  90. """
  91. insert_sql = f"""
  92. INSERT INTO {self.article_text_table} (content_id, article_title, article_text)
  93. values (%s, %s, %s);
  94. """
  95. try:
  96. await self.mysql_client.async_insert(
  97. sql=insert_sql,
  98. params=(
  99. self.content_id,
  100. self.title,
  101. self.contents
  102. )
  103. )
  104. logging(
  105. code="1002",
  106. info="请求文章存储到 long_articles_text中",
  107. function="Record",
  108. trace_id=self.trace_id
  109. )
  110. except Exception as e:
  111. logging(
  112. code="1002",
  113. info="请求文章 id 已经存储在 long_article_text中 {}".format(e),
  114. function="Record",
  115. trace_id=self.trace_id
  116. )
  117. async def deal(self):
  118. """
  119. deal
  120. :return:
  121. """
  122. params_error = self.check_params()
  123. if params_error:
  124. return params_error
  125. else:
  126. # 记录数据
  127. await self.input_into_article_match_video_table()
  128. await self.input_into_article_text_table()
  129. res = {
  130. "status": "success input to article queue",
  131. "code": 0,
  132. "traceId": self.trace_id
  133. }
  134. return res