from bs4 import BeautifulSoup
import requests,time,re
from base import BaseCore
# import pandas as pd

baseCore = BaseCore.BaseCore()
cnx = baseCore.cnx
cursor = baseCore.cursor
cnx_ = baseCore.cnx_
cursor_ = baseCore.cursor_
log = baseCore.getLogger()
taskType = '500强专利'
# headers = {
#     "Cookie":"currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2Fdata%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%25202021%25202020%25202019%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26rnd%3D1663641959596; PGS=10; _pk_id.93.72ee=ee83303e45a089a1.1663061058.; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=cn_EP; _pk_ses.93.72ee=1; LevelXLastSelectedDataSource=EPODOC; menuCurrentSearch=%2F%2Fworldwide.espacenet.com%2FsearchResults%3FAB%3D%26AP%3D%26CPC%3D%26DB%3DEPODOC%26IC%3D%26IN%3D%26PA%3Dapple%26PD%3D2022%26PN%3D%26PR%3D%26ST%3Dadvanced%26Submit%3D%E6%A3%80%E7%B4%A2%26TI%3D%26locale%3Dcn_EP; currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26Submit%3D%25E6%25A3%2580%25E7%25B4%25A2; JSESSIONID=qnLt7d5QgBtpUGjMmHuD-kwJ.espacenet_levelx_prod_2",
#     "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
# }
# df_all = pd.read_excel('D:\\kkwork\\jupyter\\专利数量\\t1.xlsx')
# for i in range(2022,1890,-1):
#     df_all[f'{i}'] = ''

# df_all['Espacenet专利检索'] = ''
headers = {
    "Cookie": "currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2Fdata%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%25202021%25202020%25202019%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26rnd%3D1663641959596; PGS=10; _pk_id.93.72ee=ee83303e45a089a1.1663061058.; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=cn_EP; _pk_ses.93.72ee=1; LevelXLastSelectedDataSource=EPODOC; menuCurrentSearch=%2F%2Fworldwide.espacenet.com%2FsearchResults%3FAB%3D%26AP%3D%26CPC%3D%26DB%3DEPODOC%26IC%3D%26IN%3D%26PA%3Dapple%26PD%3D2022%26PN%3D%26PR%3D%26ST%3Dadvanced%26Submit%3D%E6%A3%80%E7%B4%A2%26TI%3D%26locale%3Dcn_EP; currentUrl=https%3A%2F%2Fworldwide.espacenet.com%2FsearchResults%3Fsubmitted%3Dtrue%26locale%3Dcn_EP%26DB%3DEPODOC%26ST%3Dadvanced%26TI%3D%26AB%3D%26PN%3D%26AP%3D%26PR%3D%26PD%3D2022%26PA%3Dapple%26IN%3D%26CPC%3D%26IC%3D%26Submit%3D%25E6%25A3%2580%25E7%25B4%25A2; JSESSIONID=qnLt7d5QgBtpUGjMmHuD-kwJ.espacenet_levelx_prod_2",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
}

def name_handle(english_name_):
    if 'INC.' in english_name_ or 'LTD.' in english_name_ or 'CO.' in english_name_ \
            or 'CORP.' in english_name_ or 'GMBH' in english_name_ \
            or ' AG' in english_name_ or 'SARL' in english_name_ or 'S.A.' in english_name_ \
            or 'PTY' in english_name_ or 'LLC' in english_name_ or 'LLP' in english_name_ \
            or ' AB' in english_name_ or ' NV' in english_name_ or 'N.V.' in english_name_ \
            or 'A.S.' in english_name_ or ' SA' in english_name_ or ',Limited' in english_name_ \
            or ' SE' in english_name_ or ' LPC' in english_name_ or 'S.P.A.' in english_name_:
        english_name = english_name_.replace('INC.', '').replace('LTD.', '').replace('CO.', '').replace('CORP.', '') \
            .replace('GMBH', '').replace(' AG', '').replace('SARL', '').replace('S.A.', '').replace('PTY', '') \
            .replace('LLC', '').replace('LLP', '').replace(' AB', '').replace(' NV', '').replace(',', '') \
            .replace('A.S.', '').replace(' SA', '').replace(',Limited', '').replace(' SE', '').replace(' PLC', '') \
            .replace('N.V.', '').replace('S.P.A.', '').rstrip()
        return english_name
    else:
        english_name = english_name_
        return english_name

if __name__ == '__main__':
    while True:
        start_time = time.time()
        # 根据从Redis中拿到的社会信用代码,在数据库中获取对应基本信息
        social_code = baseCore.redicPullData('ZhuanLi:gwSocial_code')
        # social_code = '9111000071093123XX'
        # 判断 如果Redis中已经没有数据，则等待
        if social_code == None:
            # time.sleep(20)
            break
        start = time.time()
        try:
            data = baseCore.getInfomation(social_code)
            if len(data) != 0:
                pass
            else:
                # 数据重新塞入redis
                baseCore.rePutIntoR('ZhuanLi:gwSocial_code', social_code)
                continue
            id = data[0]
            com_name = data[1]
            xydm = data[2]
            english_name_ = data[5]
            place = data[6]
            if place == 1:
                log.info(f'{com_name}--国内')
                baseCore.rePutIntoR('ZhuanLi_500:zgSocial_code',social_code)
                continue
            if english_name_:
                pass
            else:
                query = f"select * from sys_base_enterprise where social_credit_code ='{xydm}'"
                cursor_.execute(query)
                reslut = cursor_.fetchone()
                english_name_ = reslut[32]
                # todo:将该字段更新到144企业库
                update_ = f"update EnterpriseInfo set EnglishName='{english_name_}' where SocialCode='{xydm}' "
                cursor.execute(update_)
                cnx.commit()
            english_name_ = english_name_.upper()
            english_name = name_handle(english_name_)
            num_zhuanli = 0
            # url1 = f'https://worldwide.espacenet.com/data/searchResults?ST=singleline&locale=cn_EP&submitted=true&DB=&query={com_name}&rnd=' + str(
            #     int(float(time.time()) * 1000))
            #
            # res1 = requests.get(url1, headers=headers)
            # soup1 = BeautifulSoup(res1.content, 'html.parser')
            #
            # num_text = soup1.find('p', {'class': 'numResultsFoundMsg'}).text
            #
            # try:
            #     zhuanli = re.findall("约(.*?)个", num_text)[0].replace(',', '')
            # except:
            #     zhuanli = re.findall("多于(.*?)个", num_text)[0].replace(',', '')
            # if zhuanli:
            for year in range(2023, 1900, -1):
                url = f'https://worldwide.espacenet.com/data/searchResults?submitted=true&locale=cn_EP&DB=EPODOC&ST=advanced&TI=&AB=&PN=&AP=&PR=&PD={year}&PA={english_name}&IN=&CPC=&IC=&rnd=' + str(
                    int(float(time.time()) * 1000))
                # url = 'https://worldwide.espacenet.com/data/searchResults?submitted=true&locale=cn_EP&DB=EPODOC&ST=advanced&TI=&AB=&PN=&AP=&PR=&PD=2022&PA=APPLE&IN=&CPC=&IC=&rnd=1703643229331'
                ip = baseCore.get_proxy()
                res = requests.get(url, headers=headers, proxies=ip)
                soup = BeautifulSoup(res.content, 'html.parser')

                num_text = soup.find('p', {'class': 'numResultsFoundMsg'}).text
                try:
                    try:
                        zhuanli = int(re.findall("约(.*?)个", num_text)[0].replace(',', ''))
                    except:
                        zhuanli = int(re.findall("多于(.*?)个", num_text)[0].replace(',', ''))
                except:
                    zhuanli = int(re.findall("找到(.*?)个", num_text)[0].replace(',', ''))
                if zhuanli == 0:
                    dic_info = {
                        'com_name': com_name,
                        'social_code': social_code,
                    }
                    # 插入数据库表中
                    selectSql = f"select count(1) from zhuanli_500 where social_code='{xydm}' "
                    cursor.execute(selectSql)
                    count = cursor.fetchone()[0]
                    if count > 0:
                        log.info(f"{com_name}-----已经存在--{year}--无专利信息")
                        break
                    else:
                        values_tuple = tuple(dic_info.values())
                        # log.info(f"{gpdm}-------{companyname}---新增")
                        insertSql = f"insert into zhuanli_500(com_name,social_code) values (%s,%s)"
                        cursor.execute(insertSql, values_tuple)
                        cnx.commit()
                        log.info(f"{com_name}------新增----无专利信息")
                    break
                dic_info = {
                    'com_name': com_name,
                    'social_code': social_code,
                    'year': year,
                    'num': zhuanli
                }
                # 插入数据库表中
                selectSql = f"select count(1) from zhuanli_500 where social_code='{xydm}' and year='{year}' "
                cursor.execute(selectSql)
                count = cursor.fetchone()[0]
                if count > 0:
                    log.info(f"{com_name}-------{year}---已经存在")
                    continue
                else:
                    values_tuple = tuple(dic_info.values())
                    # log.info(f"{gpdm}-------{companyname}---新增")
                    insertSql = f"insert into zhuanli_500(com_name,social_code,year,num) values (%s,%s,%s,%s)"
                    cursor.execute(insertSql, values_tuple)
                    cnx.commit()
                    log.info(f"{com_name}-------{year}---新增")
        except:
            log.info("error!{}".format(social_code))
            baseCore.rePutIntoR('ZhuanLi:gwSocial_code', social_code)
            continue