# -*- coding: utf-8 -*-
import hashlib
import logging as logger
import os
import re
import tarfile
import time
import uuid
from urllib import parse

import requests

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 getTimeStamp2(timeStr, parseRule):
    reg_pattern_dict = {
        r"\D*(\d+[\/\-年\.]\d+[\/\-月\.]\d+)\D*(\d*[:時]?\d*[:分]?\d*)?\D*": ["%Y-%m-%d %H:%M:%S"],
        r"\D*(\d+年 \d+月 \d+)\D*(\d*[:時]?\d*[:分]?\d*)?\D*": ["%Y-%m-%d %H:%M:%S"],
        r"\D*(\d+[/\-\.月]\d+[/\-\.日]\d{4})\D*(\d*:?\d*:?\d*)?\D*": ["%m-%d-%Y %H:%M:%S"],
        r"([a-zA-Z]+[\.]? \d+[,\.]? \d{4})\D*(\d*:?\d*:?\d*)?\D*": ["%B %d- %Y %H:%M:%S", "%b %d- %Y %H:%M:%S",
                                                                    "%b- %d- %Y %H:%M:%S", "%B %d %Y %H:%M:%S",
                                                                    "%b %d %Y %H:%M:%S", "%b-%d-%Y %H:%M:%S",
                                                                    "%B-%d-%Y %H:%M:%S"],
        r"(\d+ [a-zA-Z]+[,\.]? \d{4})\D*(\d*:?\d*:?\d*)?\D*": ["%d %B- %Y %H:%M:%S", "%d %b- %Y %H:%M:%S",
                                                               "%d %B %Y %H:%M:%S", "%d %b %Y %H:%M:%S",
                                                               "%d-%B-%Y %H:%M:%S", "%d-%b-%Y %H:%M:%S"],
        r"\d{10}": ["%Y-%m-%d %H:%M:%S"],
        r"\d{13}": ["%Y-%m-%d %H:%M:%S"],
        r"\D*([A-Z][a-z]+\s\d+\w+,\s\d{4})\s()\D*": ["%b %drd- %Y %H:%M:%S", "%b %dth- %Y %H:%M:%S",
                                                     "%b %dnd- %Y %H:%M:%S", "%b %dst- %Y %H:%M:%S"]
    }
    if parseRule:
        reg_pattern_dict = parseRule
    time_stamp = 0
    timeStr = transform_full_width_number(timeStr)
    for key, value in reg_pattern_dict.items():
        format_date = getftime2(key, timeStr)
        if format_date is not None:
            for dateFormateStr in value:
                try:
                    time_stamp = time.mktime(
                        time.strptime(format_date, dateFormateStr))
                    break
                except:
                    pass
        if time_stamp != 0:
            break
    return int(time_stamp * 1000)


def transferTimeZone(timestamp, timeZone):
    if timestamp != 0:
        return int(timestamp + (8 - timeZone) * 60 * 60 * 1000)
    else:
        return 0


def getftime2(pattern, timestr):
    result = re.search(pattern, timestr)
    if result is not None:
        ts = result.group(0).strip()
        try:
            # 对直接是时间戳的日期进行识别
            tn = int(ts)
            if len(ts) >= 10:
                if len(ts) == 13:
                    ts = ts[:10]
                return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(ts)))
        except:
            pass
        date = result.group(1)
        timet = result.group(2)
        date = date.replace('/', '-').replace(".", "-").replace(",", "-").replace("年", "-").replace("月", "-").replace(
            "日", "").replace(' ', '-').replace('--', '-')
        date_array = date.split('-')
        for i in range(len(date_array)):
            if (date_array[i].endswith('st') or date_array[i].endswith('nd') or date_array[i].endswith('rd') or
                date_array[i].endswith('th')) and date_array[i] != 'August':
                date_array[i] = date_array[i][:-2]
        try:
            year = int(date_array[0])
            if '民國' in timestr or '主体' in timestr or '民国' in timestr or (year > 100 and year < 120):
                year += 1911
            if '令和' in timestr:
                year += 2018
            if 20 < year < 30:  # 部分韩国站点的年份只有两位，比如2023年就是23
                year += 2000
            date_array[0] = str(year)
        except ValueError:
            pass
        date = '-'.join(date_array)
        if timet is None or timet.strip() == '':
            timet = '00:00:00'
        else:
            timet = timet.replace("時", ":").replace("分", ":")
            timeList = timet.split(':')
            l = len(timeList)
            if l > 0 and len(timestr) < 100 and (
                    timestr.upper().find('PM') > 0 or timestr.upper().find('P.M.') > 0):  # 12小时制转为24小时制
                timeList[0] = str(int(timeList[0]) + 12)
            while l < 3:
                timeList.append('00')
                l += 1
            timet = ':'.join(timeList)
        return date + ' ' + timet
    else:
        return None


def mZip(fromPath, toPath):
    parentDir = os.path.abspath(os.path.dirname(fromPath) + os.path.sep + ".")
    with tarfile.open(toPath, 'w:gz') as zipFile:
        for root, dirs, files in os.walk(fromPath, topdown=True):
            for name in files:
                try:
                    absPath = os.path.join(root, name)  # 文件绝对路径
                    arcname = os.path.relpath(absPath, parentDir)  # 文件相对路径
                    if not os.path.exists(absPath):
                        continue
                    zipFile.add(absPath, arcname)
                except:
                    pass


def mUnZip(fromPath, toPath):
    if toPath is None:
        end = fromPath.rfind('/')
        if end < 0:
            end = fromPath.rfind('\\')
        toPath = fromPath[0:end]
    tar = tarfile.open(fromPath, 'r:gz')
    tar.extractall(toPath)
    tar.close()


def buildUrl(current_url, allowed_domains, target_url):
    build_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 as e:
            print(allowed_domains)
            print(parsed_target_url[1])
        if target_url_fields[1] == '':
            return build_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]
    build_url = parse.urlunparse(target_url_fields)
    build_url = filter_brackets(build_url)
    return build_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'
    try:
        os.system('wget -q -O %s/%s %s' % (target_path, file_name, url))
        logger.info('使用wget下载文件成功')
        return file_name
    except:
        pass


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


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


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


if __name__ == '__main__':
    # a = ['  令和4年6月9日', 'www.kcna.kp (主体111.6.6.)', '民國111年06月09日 ', 'Jun. 9, 2022', '111年 06月 21日']
    a = ['/j/press/news/2022/12/02a.pdf']
    for _ in a:
        # print(getTimeStamp2(_, {r"(\d{4}-\d{2}-\d{2})\D*(\d{2}:\d{2}:\d{2})*\D*": ['%Y-%m-%d %H:%M:%S']}))
        print(getTimeStamp2(_, None))
# c = 'http://www.cicir.ac.cn/NEW/opinionsList.html'
# a = 'cicir.ac.cn'
# t = 'opinion.html?id=8f9488f1-6012-4ea9-8a0f-e7e2b9f3bb79'
# b = buildUrl(c, a, t)
# print(b)
# print(generate_file_name_from_url(
#     'https://csbaonline.org/uploads/documents/CSBA8337_(Innovating_For_Great_Power_Competition_Report)_web2.pdf'))
