pipeline.py 4.3 KB

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