pipeline.py 4.3 KB

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