import hashlib
import logging
import re

dvids_tags_in_use = [
    'exercise',
    'training',
    'interoperability',
    'partnerships',
    'activation',
    'deactivation',
    'deployment',
    'retire',
    'change of command',
    'renovations',
]

OTHERS_TAG = 'JSC_OTHERS'


def get_legal_file_name(filename):
    for _ in ['"', '/', '\\', '|', ':', '?', '<', '>', '*']:
        filename = filename.replace(_, "-")
    return filename


def has_tags_in_use(list_array):
    for item in list_array:
        if item.lower() in dvids_tags_in_use:
            return True
    return False


def get_str_md5(param_str):
    if isinstance(param_str, str):
        param_str = param_str.encode("utf-8")
    m = hashlib.md5()
    m.update(param_str)
    return m.hexdigest()


def trim_space(input_str):
    input_str = input_str.replace("\r\n", " ").replace("\t", " ")
    result = re.sub(' {2,}', ' ', input_str)
    return result


def transform_full_width_number(input_str):
    d = {
        '０': '0',
        '１': '1',
        '２': '2',
        '３': '3',
        '４': '4',
        '５': '5',
        '６': '6',
        '７': '7',
        '８': '8',
        '９': '9'
    }
    for _ in d:
        input_str = input_str.replace(_, d[_])
    return input_str


# 文本过滤词校验
def check_keywords(content, include=None, exclude=None):
    if content is None or len(content) <= 0:
        logging.info("文本为空，校验不通过")
        return False
    if exclude and len(exclude) > 0:
        # 首先判断排除词，只要出现排除词，校验就不通过，返回 False
        for exclude_word in exclude:
            if len(exclude_word) > 0 and exclude_word in content:
                if not content.startswith("http"):
                    logging.info(f"在文本中检测到排除词“{exclude_word}”，校验不通过")
                return False
    if include and len(include) > 0:
        # 如果通过排除词校验，再进行包含词校验，出现包含词，返回 True
        for include_word in include:
            if len(include_word) > 0 and include_word in content:
                logging.debug(f"在文本中检测到包含词“{include_word}”，校验通过")
                return True
        if not content.startswith("http"):
            logging.info(f"在文本“{content[:50]}...”中未找到任何包含词，校验不通过")
        return False
    return True


if __name__ == "__main__":
    print(check_keywords(
        content="中央电视台新闻频道上午直播了新型运载火箭发射的实况",
        # content="张三在王思聪的直播间里刷了一个火箭",
        include=["火箭", "直播"],
        exclude=["王思聪", "直播间"]
    ))
