process.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. @author: luojunhui
  3. 对请求进行操作
  4. """
  5. import os
  6. from applications.log import logging
  7. from applications.functions.ask_kimi import ask_kimi
  8. from applications.match_alg import best_choice
  9. from applications.functions.common import *
  10. class ProcessParams(object):
  11. """
  12. Params Analysis
  13. """
  14. def __init__(self, t_id):
  15. self.trace_id = t_id
  16. def get_params(self, data):
  17. """
  18. "accountName": "公众号名称",
  19. "content": "文章正文",
  20. "title": "文章标题",
  21. "cover": "封面链接"
  22. :param data:
  23. :return: title
  24. """
  25. logging(
  26. code="1002",
  27. info="处理请求参数",
  28. function="get_params",
  29. trace_id=self.trace_id,
  30. data=data
  31. )
  32. return data
  33. def ask_kimi_and_save_to_local(self, title):
  34. """
  35. save file to local
  36. :param title:
  37. :return:
  38. """
  39. save_path = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  40. if os.path.exists(save_path):
  41. logging(
  42. code="1002",
  43. info="该 video 信息已经挖掘完成---{}".format(title),
  44. function="ask_kimi_and_save_to_local",
  45. trace_id=self.trace_id,
  46. )
  47. return
  48. else:
  49. os.makedirs(os.path.dirname(save_path), exist_ok=True)
  50. if not title:
  51. result = {}
  52. else:
  53. result = ask_kimi(title)
  54. logging(
  55. code="1002",
  56. info="kimi-result",
  57. data=result,
  58. trace_id=self.trace_id,
  59. function="ask_kimi_and_save_to_local"
  60. )
  61. with open(save_path, "w", encoding="utf-8") as f:
  62. f.write(json.dumps(result, ensure_ascii=False))
  63. def deal(self, data):
  64. """执行代码"""
  65. params = self.get_params(data)
  66. title = params['title']
  67. # account_name = params['accountName']
  68. # ghId = params['ghId']
  69. title_p = os.path.join(os.getcwd(), 'applications', 'static', "titles", "{}.json".format(title))
  70. if os.path.exists(title_p):
  71. logging(
  72. code="1002",
  73. info="该标题已经被 kimi 处理过,跳过请求 kimi 操作--- {}".format(title),
  74. function="process",
  75. trace_id=self.trace_id
  76. )
  77. else:
  78. self.ask_kimi_and_save_to_local(title)
  79. with open(title_p, encoding="utf-8") as f:
  80. params_obj = json.loads(f.read())
  81. best_video_id = best_choice(
  82. params_obj=params_obj,
  83. trace_id=self.trace_id,
  84. request_param=params
  85. )
  86. logging(
  87. code="1002",
  88. info="best video_id --{}".format(best_video_id),
  89. function="process",
  90. trace_id=self.trace_id
  91. )
  92. if best_video_id:
  93. print(best_video_id)
  94. response = request_for_info(best_video_id)
  95. productionCover = response['data'][0]['shareImgPath']
  96. productionName = response["data"][0]['title']
  97. videoUrl = response['data'][0]['videoPath']
  98. user_id = response['data'][0]['user']['uid']
  99. programAvatar = "/static/logo.png"
  100. programId = "wx69c36def517d687a"
  101. programName = "票圈最惊奇"
  102. source = "Web"
  103. root_share_id, productionPath = create_gzh_path(video_id=best_video_id, shared_uid=user_id)
  104. logging(
  105. code="1002",
  106. info="root_share_id --{}, productionPath -- {}".format(root_share_id, productionPath),
  107. function="process",
  108. trace_id=self.trace_id
  109. )
  110. result = {
  111. "productionCover": productionCover,
  112. "productionName": productionName,
  113. "programAvatar": programAvatar,
  114. "programId": programId,
  115. "programName": programName,
  116. "source": source,
  117. "rootShareId": root_share_id,
  118. "productionPath": productionPath,
  119. "videoUrl": videoUrl
  120. }
  121. logging(
  122. code="2000",
  123. info="统计 root_share_id && video_id",
  124. function="process",
  125. trace_id=self.trace_id,
  126. data={
  127. "rootShareId": root_share_id,
  128. "videoId": best_video_id
  129. }
  130. )
  131. else:
  132. result = {
  133. "productionCover": None,
  134. "productionName": None,
  135. "programAvatar": None,
  136. "programId": None,
  137. "programName": None,
  138. "source": None,
  139. "rootShareId": None,
  140. "productionPath": None,
  141. "videoUrl": None
  142. }
  143. logging(
  144. code="1002",
  145. info="返回结果",
  146. function="process",
  147. data=result,
  148. trace_id=self.trace_id
  149. )
  150. return result