import os
import re
import uuid
from pathlib import PurePosixPath
from urllib import parse
import logging as logger
from urllib.parse import urlparse, unquote

import requests

from PhotoSpiders.utils.string_utils import get_str_md5

default_headers = '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': default_headers}
    rsp = requests.get(url, proxies={'http': '', 'https': ''}, headers=headers)
    return rsp


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


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


def get_cookies_from_response(response):
    cookies = {}
    set_cookie = response.headers.get('set-cookie')
    if not set_cookie or len(set_cookie) == 0:
        return cookies
    cookie_list = set_cookie.split(';')
    for cookie_str in cookie_list:
        if len(cookie_str.split('=')) == 2:
            cookies[cookie_str.split('=')[0].strip()] = cookie_str.split('=')[
                1].strip()
    return cookies


def build_url(current_url, allowed_domains, target_url):
    result_url = ''
    parsed_current_url = parse.urlparse(current_url)
    parsed_target_url = parse.urlparse(target_url)
    target_url_fields = ['', '', '', '', '', '']
    # 1、设置url schema
    if len(parsed_target_url[0]) > 0:
        target_url_fields[0] = parsed_target_url[0]
    else:
        target_url_fields[0] = parsed_current_url[0]
    # 2、判断域名是否符合要求
    if len(parsed_target_url[1]) > 0:
        try:
            for domain in allowed_domains:
                # 判断url是否为当前站点链接，只对当前站点链接进行爬取
                if parsed_target_url[1].find(domain) < 0:
                    continue
                else:
                    target_url_fields[1] = parsed_target_url[1]
        except Exception:
            print(allowed_domains)
            print(parsed_target_url[1])
        if target_url_fields[1] == '':
            return result_url
    else:
        target_url_fields[1] = parsed_current_url[1]
    # 3、设置url的path
    if len(parsed_target_url[1]) > 0:
        # 目标url的域名存在，说明该url是一个绝对地址，否则为相对地址
        target_url_fields[2] = parsed_target_url[2]
    else:
        current_url_path = parsed_current_url[2]
        if current_url_path.find('.') > 0:
            current_url_path = current_url_path[0:current_url_path.rfind('/')]
        if not current_url_path.endswith('/'):
            current_url_path += '/'
        if parsed_target_url[2] is None or len(parsed_target_url[2]) == 0:
            target_url_fields[2] = ''
        elif parsed_target_url[2].startswith('/'):
            target_url_fields[2] = parsed_target_url[2]
        elif parsed_target_url[2].startswith('.'):
            target_url_fields[2] = current_url_path + parsed_target_url[2][2:]
        else:
            target_url_fields[2] = current_url_path + parsed_target_url[2]
    # 4、设置其它属性
    if len(parsed_target_url[3]) > 0:
        target_url_fields[3] = parsed_target_url[3]
    if len(parsed_target_url[4]) > 0:
        target_url_fields[4] = parsed_target_url[4]
    if len(parsed_target_url[5]) > 0:
        target_url_fields[5] = parsed_target_url[5]
    result_url = parse.urlunparse(target_url_fields)
    result_url = filter_brackets(result_url)
    return result_url


def filter_brackets(url_text):
    url_text = url_text.replace('(', '%28').replace(')', '%29')
    return url_text


def custom_download(url):
    target_path = r'/usr/local/temp_file/full'
    # file_name = generate_file_name_from_url(url) + '.pdf'
    file_name = parse_file_name_from_url(url)
    try:
        os.system('wget -q -O %s/%s %s' % (target_path, file_name, url))
        logger.info('使用wget下载文件成功')
        return file_name
    except:
        pass


def parse_file_name_from_url(url):
    original_file_name = PurePosixPath(urlparse(url).path).name
    # 原始URL中的文件名可能包含带有百分号的字符，比如空格会变成%20之类的，要用下面的方法进行还原
    original_file_name = unquote(original_file_name)
    # 假如有以下非法的字符，即不能出现在文件路径的字符，需要替换成合法字符
    for unsupported_char in ['/', '\\', ':', '*', '?', '<', '>', '|', '"']:
        if unsupported_char in original_file_name:
            original_file_name.replace(unsupported_char, "-")
    # 对于长度太短的文件，为了防止重名，根据他的原始URL生成一个MD5，取前六位插入到文件名之前
    if len(original_file_name) < 5:
        original_file_name = get_str_md5(url)[:6] + '-' + original_file_name
    return original_file_name


def generate_file_name_from_url(url):
    uuid_file_name = uuid.uuid3(uuid.NAMESPACE_URL, url)
    return str(uuid_file_name).replace('-', '') + '00000000'


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)\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()


if __name__ == "__main__":
    result = parse_file_name_from_url(
        "https://www.rand.org/content/dam/rand/pubs/research_reports/RRA1500/RRA1500-2/RAND_RRA1500-2.pdf")
    print(result)
