import re

import requests

UA = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 ' \
     'Safari/537.36 '


def http_get(url):
    headers = {'User-Agent': UA}
    rsp = requests.get(url, proxies={'http': '', 'https': ''}, headers=headers)
    return rsp


def http_post(url, data, headers=None):
    if headers:
        rsp = requests.post(url, data=data, headers=headers)
    else:
        rsp = requests.post(url, data=data, headers={'User-Agent': UA})
    return rsp


def http_put(url, data):
    # 必须指定Content-Type为application/json才能成功请求
    headers = {'User-Agent': UA,
               "Content-Type": "application/json"}
    rsp = requests.put(url, data=data, headers=headers)
    return rsp


def filter_html_tags(text):
    regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"
    regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"
    # regEx_span = "<span\s*[^>]*>|<\/span>"
    # regEx_a = "<a\s*[^>]*>|<\/a>"
    regEx_html = "<(?!(br|img|\/?table|\/?tbody|\/?tr|\/?td)\s*/?)[^>]+>"  # 需要保留img、br、表格标签
    result = text
    result = re.sub(regEx_script, "", result)
    result = re.sub(regEx_style, "", result)
    # result = re.sub(regEx_span, "", result)
    # result = re.sub(regEx_a, "", result)
    result = re.sub(regEx_html, "", result)
    return result.strip()


def del_html_img(url_content, img_filter_list):
    if len(img_filter_list) == 0:
        return url_content
    # 删除正文中包含的已过滤图片img标签
    pattern = r'<img[^>]*data-src="([^"]*)"[^>]*>'
    matches = re.findall(pattern, url_content)
    for data_src in matches:
        if data_src in img_filter_list:
            # 根据data-src属性的值删除整个img标签
            url_content = re.sub(r'<img[^>]*data-src="{}"[^>]*>'.format(re.escape(data_src)), '', url_content)
    return url_content
