search_schedule.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. """
  2. @author: luojunhui
  3. 调用接口在微信内搜索视频
  4. """
  5. import json
  6. import asyncio
  7. import time
  8. from applications.search import *
  9. from applications.static.config import gh_id_dict
  10. from applications.functions.log import logging
  11. from applications.functions.video_item import VideoProducer
  12. from applications.functions.async_etl import AsyncETL
  13. from applications.functions.mysql import select_sensitive_words
  14. from applications.functions.kimi import KimiServer
  15. class SearchABTest(object):
  16. """
  17. 搜索策略实验方案
  18. """
  19. ori_title = None
  20. article_summary = None
  21. article_keys = None
  22. gh_id = None
  23. trace_id = None
  24. def __init__(self, info, gh_id):
  25. SearchABTest.set_class_properties(info, gh_id)
  26. @classmethod
  27. def set_class_properties(cls, info, gh_id):
  28. """
  29. 初始化搜索策略实验类
  30. :param info: kimi 挖掘的基本信息
  31. :param gh_id: 公众号账号 id
  32. :return:
  33. """
  34. cls.ori_title = info["ori_title"]
  35. cls.article_summary = info["content_title"]
  36. cls.article_keys = info["content_keys"]
  37. cls.trace_id = info["trace_id"]
  38. cls.gh_id = gh_id
  39. @classmethod
  40. async def base_line(cls):
  41. """
  42. 兜底策略
  43. """
  44. result = await SearchMethod().search_v0(
  45. text=cls.article_keys[0],
  46. trace_id=cls.trace_id
  47. )
  48. if result:
  49. return result
  50. else:
  51. sub_result = await SearchMethod().search_v0(
  52. text=cls.article_keys[1],
  53. trace_id=cls.trace_id)
  54. if sub_result:
  55. return sub_result
  56. else:
  57. return await SearchMethod().search_v0(
  58. text=cls.article_keys[2],
  59. trace_id=cls.trace_id
  60. )
  61. @classmethod
  62. async def ab_0(cls):
  63. """
  64. 默认原标题搜索
  65. :return:
  66. """
  67. search_result = await SearchMethod().search_v0(
  68. text=cls.ori_title,
  69. trace_id=cls.trace_id
  70. )
  71. if search_result:
  72. return search_result
  73. else:
  74. return await cls.base_line()
  75. @classmethod
  76. async def ab_1(cls):
  77. """
  78. 使用 content_summary搜索
  79. :return:
  80. """
  81. search_result = await SearchMethod().search_v0(
  82. text=cls.article_summary,
  83. trace_id=cls.trace_id
  84. )
  85. if search_result:
  86. return search_result
  87. else:
  88. return await cls.ab_0()
  89. @classmethod
  90. async def ab_2(cls):
  91. """
  92. 使用文本关键词搜索
  93. :return:
  94. """
  95. search_result = await SearchMethod().search_v0(
  96. text=cls.article_keys[0],
  97. trace_id=cls.trace_id
  98. )
  99. if search_result:
  100. return search_result
  101. else:
  102. return await cls.base_line()
  103. @classmethod
  104. async def ab_3(cls):
  105. """
  106. 使用文本关键词搜索
  107. :return:
  108. """
  109. search_result = await SearchMethod().search_v0(
  110. text=cls.article_keys[1],
  111. trace_id=cls.trace_id
  112. )
  113. if search_result:
  114. return search_result
  115. else:
  116. return await cls.base_line()
  117. @classmethod
  118. async def ab_4(cls):
  119. """
  120. 使用文本关键词搜索
  121. :return:
  122. """
  123. search_result = await SearchMethod().search_v0(
  124. text=cls.article_keys[2],
  125. trace_id=cls.trace_id
  126. )
  127. if search_result:
  128. return search_result
  129. else:
  130. return await cls.base_line()
  131. class SearchMethod(object):
  132. """
  133. 搜索召回模式
  134. """
  135. s_words = select_sensitive_words()
  136. @classmethod
  137. async def search_v0(cls, text, trace_id):
  138. """
  139. 搜索顺序-wx --> baidu --> xigua
  140. 一共需要返回三条视频
  141. :return:
  142. """
  143. wx_result = []
  144. if wx_result:
  145. return {"platform": "wx_search", "result": wx_result[0]}
  146. else:
  147. logging(
  148. code="7001",
  149. info="通过微信搜索失败---{}".format(text),
  150. trace_id=trace_id,
  151. )
  152. # 微信搜不到的话,采用好看视频搜索
  153. time.sleep(1)
  154. baidu_result = hksp_search(key=text, sensitive_words=cls.s_words)
  155. if baidu_result:
  156. return {"platform": "baidu_search", "result": baidu_result[0]}
  157. else:
  158. # 若好看视频未搜到,则采用西瓜搜索
  159. logging(
  160. code="7001",
  161. info="通过baidu搜索失败---{}".format(text),
  162. trace_id=trace_id,
  163. )
  164. return None
  165. # xigua_result = xigua_search(keyword=text, sensitive_words=cls.s_words)
  166. # if xigua_result:
  167. # return {"platform": "xg_search", "result": xigua_result[0]}
  168. # else:
  169. # logging(
  170. # code="7001",
  171. # info="通过西瓜搜索失败---{}, 启用兜底方式".format(text),
  172. # trace_id=trace_id,
  173. # )
  174. # return None
  175. async def video_sender(video_obj, user, trace_id, platform):
  176. """
  177. 异步处理微信 video_obj
  178. 公众号和站内账号一一对应
  179. :param platform:
  180. :param user:
  181. :param trace_id:
  182. :param video_obj:
  183. :return:
  184. """
  185. # ETL_MQ = MQ(topic_name="topic_crawler_etl_prod")
  186. Video = VideoProducer()
  187. if platform == "xg_search":
  188. mq_obj = Video.xg_video_producer(
  189. video_obj=video_obj,
  190. user=user,
  191. trace_id=trace_id,
  192. )
  193. elif platform == "baidu_search":
  194. mq_obj = Video.baidu_video_producer(
  195. video_obj=video_obj,
  196. user=user,
  197. trace_id=trace_id,
  198. )
  199. elif platform == "wx_search":
  200. mq_obj = Video.wx_video_producer(
  201. video_obj=video_obj,
  202. user=user,
  203. trace_id=trace_id,
  204. )
  205. else:
  206. mq_obj = {}
  207. AE = AsyncETL(video_obj=mq_obj)
  208. video_id = await AE.etl_deal()
  209. logging(
  210. code="6002",
  211. info="视频下载完成",
  212. data=mq_obj,
  213. trace_id=trace_id
  214. )
  215. return video_id
  216. async def search_videos(params, trace_id, gh_id, mysql_client):
  217. """
  218. search and send msg to ETL
  219. :param mysql_client:
  220. :param params:
  221. :param gh_id: 通过账号 id 来控制实验策略
  222. :param trace_id:
  223. :return:
  224. """
  225. K = KimiServer()
  226. kimi_info = await K.search_kimi_schedule(params=params)
  227. print(json.dumps(kimi_info, ensure_ascii=False, indent=4))
  228. kimi_title = kimi_info['k_title']
  229. content_title = kimi_info['content_title'].replace("'", "").replace('"', "")
  230. content_keys = json.dumps(kimi_info['content_keys'], ensure_ascii=False)
  231. update_kimi_sql = f"""
  232. UPDATE long_articles_video SET
  233. kimi_title = '{kimi_title}',
  234. kimi_summary = '{content_title}',
  235. kimi_keys = '{content_keys}'
  236. WHERE trace_id = '{trace_id}';
  237. """
  238. await mysql_client.async_insert(update_kimi_sql)
  239. kimi_info["trace_id"] = trace_id
  240. SearchAB = SearchABTest(info=kimi_info, gh_id=gh_id)
  241. recall_obj_1 = await SearchAB.ab_1()
  242. await asyncio.sleep(3)
  243. recall_obj_2 = await SearchAB.ab_2()
  244. await asyncio.sleep(3)
  245. recall_obj_3 = await SearchAB.ab_3()
  246. recall_list = [recall_obj_1, recall_obj_2, recall_obj_3]
  247. un_empty_list = [i for i in recall_list if i]
  248. if len(un_empty_list) < 3:
  249. await asyncio.sleep(3)
  250. recall_obj_4 = await SearchAB.ab_4()
  251. if recall_obj_4:
  252. un_empty_list.append(recall_obj_4)
  253. # 逐条下载,逐条写表
  254. if un_empty_list:
  255. for index, recall_obj in enumerate(un_empty_list, 1):
  256. platform = recall_obj["platform"]
  257. recall_video = recall_obj["result"]
  258. if recall_video:
  259. logging(
  260. code="7002",
  261. info="视频搜索成功, 搜索平台为--{}".format(platform),
  262. trace_id=trace_id,
  263. data=recall_video,
  264. )
  265. video_id = await video_sender(
  266. video_obj=recall_video,
  267. user=gh_id_dict.get(gh_id),
  268. trace_id=trace_id,
  269. platform=platform,
  270. )
  271. update_id_sql = f"""
  272. UPDATE long_articles_video
  273. SET
  274. recall_video_id{index} = {video_id}
  275. WHERE
  276. trace_id = '{trace_id}'
  277. """
  278. await mysql_client.async_insert(update_id_sql)
  279. else:
  280. logging(
  281. code="7003",
  282. info="视频搜索失败, 被敏感词过滤",
  283. trace_id=trace_id
  284. )