pipeline.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """
  2. @author: luojunhui
  3. """
  4. import time
  5. import requests
  6. from applications.functions import title_sim_v2_by_list, is_bad
  7. class LongArticlesPipeline(object):
  8. """
  9. Long articles Pipeline
  10. """
  11. @classmethod
  12. def get_titles(cls, account_name, index_list):
  13. """
  14. :param account_name:
  15. :param index_list:
  16. :return:
  17. """
  18. print("开始请求")
  19. print(account_name)
  20. url = "http://localhost:6060/title_list"
  21. response = requests.request(
  22. "POST",
  23. url=url,
  24. headers={},
  25. json={
  26. "account_name": account_name,
  27. "index_list": index_list,
  28. "min_time": None,
  29. "max_time": None,
  30. "msg_type": "9"
  31. }
  32. )
  33. print(response.status_code)
  34. print(response.text)
  35. print(response.json())
  36. return response.json()['title_list']
  37. @classmethod
  38. def history_title(cls, account_nickname):
  39. """
  40. 判断是否存储
  41. :param account_nickname:
  42. :return:
  43. """
  44. # if "【1】" in plan_name or "【2】" in plan_name:
  45. index_list_1 = [1, 2]
  46. index_list_2 = [1, 2, 3, 4, 5, 6, 7, 8]
  47. print("开始请求")
  48. account_title_list_1 = cls.get_titles(
  49. account_nickname,
  50. index_list=index_list_1
  51. )
  52. print(account_title_list_1)
  53. account_title_list_2 = cls.get_titles(
  54. account_nickname,
  55. index_list=index_list_2
  56. )
  57. res = {
  58. "rule_1": account_title_list_1,
  59. "rule_2": account_title_list_2
  60. }
  61. return res
  62. @classmethod
  63. def history_exists(cls, title, account_title_list):
  64. """
  65. 判断文章是否历史已发布
  66. :param title:
  67. :param account_title_list:
  68. :return:
  69. """
  70. sim_res = title_sim_v2_by_list(title, account_title_list)
  71. if sim_res:
  72. return True
  73. return False
  74. @classmethod
  75. def article_safe(cls, title):
  76. """
  77. 判断文章是否安全
  78. """
  79. url = "http://61.48.133.26:8177/sensitive/is_sensitive"
  80. body = {
  81. "text": title
  82. }
  83. response = requests.post(
  84. url=url,
  85. json=body,
  86. headers={"Content-Type": "application/json"}
  87. )
  88. return response.json()['is_sensitive']
  89. @classmethod
  90. def article_bad(cls, title, account_nickname):
  91. """
  92. 判断该文章是否为劣质文章
  93. :param title:
  94. :param account_nickname:
  95. :return:
  96. """
  97. return is_bad(title)
  98. @classmethod
  99. def deal(cls, article_obj, account_name, history_title_dict):
  100. """
  101. :param history_title_dict:
  102. :param account_name:
  103. :param article_obj:
  104. :return:
  105. """
  106. a = time.time()
  107. article_bad_flag = cls.article_bad(
  108. title=article_obj['title'],
  109. account_nickname=account_name
  110. )
  111. b = time.time()
  112. print("历史低质量文章:", b - a)
  113. if article_bad_flag:
  114. response = {
  115. "filterReason": "历史表现差的文章",
  116. "status": True
  117. }
  118. return response
  119. else:
  120. c = time.time()
  121. plan_name = article_obj['producePlanName']
  122. if "【1】" in plan_name or "【2】" in plan_name:
  123. history_title_list = history_title_dict['rule_1']
  124. else:
  125. history_title_list = history_title_dict['rule_2']
  126. history_exists_flag = cls.history_exists(
  127. title=article_obj['title'],
  128. account_title_list=history_title_list
  129. )
  130. d = time.time()
  131. print("历史已经发布文章:", d - c)
  132. if history_exists_flag:
  133. response = {
  134. "filterReason": "历史已发布文章",
  135. "status": True
  136. }
  137. return response
  138. else:
  139. e = time.time()
  140. safe_flag = cls.article_safe(title=article_obj['title'])
  141. f = time.time()
  142. print("安全:", f - e)
  143. if safe_flag:
  144. response = {
  145. "filterReason": "安全违规",
  146. "status": True
  147. }
  148. return response
  149. else:
  150. return False