accountServer.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. """
  2. @author: luojunhui
  3. """
  4. import requests
  5. from applications.articleTools import ArticleDBTools
  6. class AccountServer(object):
  7. """
  8. 获取标题和公众号文章的相关性
  9. """
  10. def __init__(self, mysql_client, params):
  11. self.account_name_list = None
  12. self.sim_type = None
  13. self.interest_type = None
  14. self.min_time = None
  15. self.max_time = None
  16. self.rate = None
  17. self.title_list = None
  18. self.params = params
  19. self.AT = ArticleDBTools(mysql_client)
  20. async def request_for_nlp(self, title_list, account_interest, account_weight):
  21. """
  22. nlp process
  23. """
  24. headers = {"Content-Type": "application/json"}
  25. url = "http://localhost:6060/nlp"
  26. body = {
  27. "data": {
  28. "text_list_a": title_list,
  29. "text_list_b": account_interest,
  30. "score_list_b": account_weight,
  31. "symbol": 1,
  32. },
  33. "function": (
  34. "similarities_cross_mean"
  35. if self.sim_type == "mean"
  36. else "similarities_cross_avg"
  37. ),
  38. }
  39. response = requests.post(url=url, headers=headers, json=body)
  40. return response
  41. def checkParams(self):
  42. """
  43. 校验传参
  44. :return:
  45. """
  46. try:
  47. self.title_list = self.params["text_list"]
  48. self.account_name_list = self.params.get("account_nickname_list", [])
  49. self.rate = self.params.get("rate", 0.1)
  50. self.max_time = self.params.get("max_time")
  51. self.min_time = self.params.get("min_time")
  52. self.interest_type = self.params.get("interest_type", "top")
  53. self.sim_type = self.params.get("sim_type", "mean")
  54. return None
  55. except Exception as e:
  56. response = {"error": "Params error", "detail": str(e)}
  57. return response
  58. async def getAccountInterest(
  59. self,
  60. account_name,
  61. method,
  62. rate=None,
  63. msg_type=None,
  64. index_list=None,
  65. min_time=None,
  66. max_time=None,
  67. ):
  68. """
  69. 获取账号的兴趣类型
  70. :param account_name:
  71. :param max_time:
  72. :param min_time:
  73. :param index_list:
  74. :param msg_type:
  75. :param keys_dict:
  76. :param rate:
  77. :param gh_id:
  78. :param method:
  79. :return:
  80. """
  81. good_df, bad_df = await self.AT.get_good_bad_articles(
  82. account_name=account_name,
  83. method=method,
  84. msg_type=msg_type,
  85. index_list=index_list,
  86. min_time=min_time,
  87. max_time=max_time,
  88. rate=rate,
  89. )
  90. view_count_list = good_df["show_view_count"]
  91. title_list = good_df["title"]
  92. return title_list, view_count_list
  93. async def getEachAccountScoreList(self, account_name):
  94. """
  95. 获取和单个账号的相关性分数
  96. :return:
  97. """
  98. # try:
  99. account_interest, account_weight = await self.getAccountInterest(
  100. account_name=account_name,
  101. method=self.interest_type,
  102. )
  103. response = await self.request_for_nlp(
  104. title_list=self.title_list,
  105. account_interest=account_interest,
  106. account_weight=account_weight,
  107. )
  108. res = response.json()
  109. sim_key = "score_list_mean" if self.sim_type == "mean" else "score_list_avg"
  110. return {
  111. "score_list": res[sim_key],
  112. "text_list_max": res["text_list_max"],
  113. }
  114. # except Exception as e:
  115. # print(e)
  116. # return {
  117. # "score_list": [0] * len(self.title_list),
  118. # "text_list_max": self.title_list,
  119. # }
  120. async def getAccountListScoreList(self):
  121. """
  122. 获取AccountList中每一个账号的相关性分数
  123. :return:
  124. """
  125. response = {}
  126. for accountName in self.account_name_list:
  127. if response.get(accountName):
  128. continue
  129. else:
  130. response[accountName] = await self.getEachAccountScoreList(account_name=accountName)
  131. return response
  132. async def deal(self):
  133. """
  134. Deal Function
  135. :return:
  136. """
  137. return (
  138. self.checkParams() if self.checkParams() else await self.getAccountListScoreList()
  139. )