check_utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import re
  2. from utils.es_query import get_es
  3. from utils.execptions import (
  4. CustomAccountPrivilegeError,
  5. CustomCheckError
  6. )
  7. __all__ = ['CheckText', 'CheckTask']
  8. class CheckContent:
  9. def __init__(self):
  10. self.sensitive_words = {
  11. '正式会员', '账户充值', 'VIP会员查阅', '>(注册)<', '>(登录)<', '高级会员',
  12. '标准会员', '点击支付'
  13. }
  14. @staticmethod
  15. def check_text_length(val: str):
  16. if len(val) == 0:
  17. raise CustomCheckError(code=10101, reason='文本内容为空')
  18. elif not re.findall(r'[\u4e00-\u9fa5]', val, re.S):
  19. raise CustomCheckError(code=10102, reason='不存在中文字符')
  20. @staticmethod
  21. def check_content(val: str):
  22. if val.count("部分文件可能不支持在线浏览"):
  23. raise CustomCheckError(code=10103, reason='文件不支持在线浏览')
  24. @staticmethod
  25. def check_account_privilege(val: str):
  26. if val.count("高级会员"):
  27. raise CustomAccountPrivilegeError
  28. elif "本招标项目仅供正式会员查阅" in val:
  29. raise CustomAccountPrivilegeError
  30. def check_sensitive_word(self, val: str):
  31. total = set()
  32. for word in self.sensitive_words:
  33. result = re.search(word, val)
  34. if result is not None:
  35. total.add(word)
  36. if len(total) > 0:
  37. raise CustomCheckError(code=10104, reason='详情内容包含敏感词')
  38. def __check(self, text):
  39. self.check_sensitive_word(text)
  40. self.check_text_length(text)
  41. self.check_content(text)
  42. self.check_account_privilege(text)
  43. def __call__(self, text: str, *args, **kwargs):
  44. self.__check(text)
  45. class CheckPrePareRequest:
  46. def __init__(self):
  47. self.crawl_keywords = {
  48. '招标', '流标', '评标', '询价', '中标候选人', '抽签', '谈判', '中选', '意见征询',
  49. '更正公告', '废标', '补遗', '议价', '邀请', '资格预审', '竞标', '变更', '遴选',
  50. '磋商', '项目', '评审', '询比', '开标', '澄清', '比选', '中止', '采购', '竟价',
  51. '招投标', '拟建', '成交', '中标', '竞争性谈判', '工程', '验收公告', '更正',
  52. '单一来源', '变更公告', '合同', '违规', '评判', '监理', '竞价', '答疑',
  53. '终止', '系统'
  54. }
  55. @staticmethod
  56. def check_es_cache(title: str, publish_time: int, rows: dict):
  57. """
  58. :param title: 标题
  59. :param publish_time: 发布时间的时间戳(l_np_publishtime)
  60. :param rows: 采集内容
  61. """
  62. retrieved_result = get_es(title, publish_time)
  63. if retrieved_result != 0:
  64. '''es查询数据结果'''
  65. rows['count'] = retrieved_result
  66. raise CustomCheckError(code=10105, reason='标题内容已存在es')
  67. def check_crawl_title(self, title: str):
  68. for keyword in self.crawl_keywords:
  69. valid_keyword = re.search(keyword, title)
  70. if valid_keyword is not None:
  71. break
  72. else:
  73. raise CustomCheckError(code=10106, reason='标题未检索到采集关键词', title=title)
  74. def __check(self, rows: dict):
  75. title, publish_time = rows['title'], rows['l_np_publishtime']
  76. self.check_crawl_title(title)
  77. self.check_es_cache(title, publish_time, rows)
  78. def __call__(self, rows: dict, *args, **kwargs):
  79. self.__check(rows)
  80. CheckText = CheckContent()
  81. CheckTask = CheckPrePareRequest()