# 获取详情页
import time

import redis
from bs4 import BeautifulSoup
r = redis.Redis(host="114.116.90.53", port=6380, password='clbzzsn', db=6)


def getDetailmsg( detailmsg):
    try:
        detailurl = detailmsg['detailUrl']
        title = detailmsg['title']
        content, contentWithTag = self.extractorMsg(detailurl, title)
        contentWithTag = self.rmTagattr(contentWithTag, detailurl)
    except Exception as e:
        content = ''
        contentWithTag = ''

    currentdate = self.getNowDate()
    kword = self.searchkw
    publishDate = detailmsg['publishTag']
    publishDate = publishDate + ''
    # publishtime=self.paserTime(publishtime)
    # publishDate=publishtime.strftime("%Y-%m-%d %H:%M:%S")
    detailmsg = {
        'title': detailmsg['title'],
        'source': detailmsg['sourceTag'],
        'detailurl': detailurl,
        'content': content,
        'contentHtml': contentWithTag,
        'publishtime': publishDate,
        'currentdate': currentdate,
        'kword': kword
    }
    return detailmsg


def getProcessitem(bdetail):
    nowDate = self.getNowDate()
    content = bdetail['content']
    if content != '':
        processitem = {
            "sid": self.sid,
            "source": "4",
            "title": bdetail['title'],
            "content": bdetail['content'],
            "contentWithtag": bdetail['contentHtml'],
            "origin": bdetail['source'],
            "publishDate": bdetail['publishtime'],
            "sourceAddress": bdetail['detailurl'],
            "createDate": nowDate
        }

    return processitem

# 将列表数据插入到表中 baidu_search_result
def itemInsertToTable(items):
    itemdata = []
    conx, cursorM = connMysql()
    companyinfo = item
    social_code = str(companyinfo.split('|')[0])
    ch_name = companyinfo.split('|')[1]
    en_name = companyinfo.split('|')[2]
    rank = bangdan_name + '|' + str(companyinfo.split('|')[3])
    for item in items:
        nowtime = getNowDate()
        data = (social_code, en_name, ch_name, rank, item['title'], item['content'], item['detailurl'], item['publishtime'], item['source'], nowtime)
        itemdata.append(data)

    sql = "INSERT into Company_layoff_copy1 (企业信用代码,企业英文名称,企业中文名称,所在榜单排名,标题,内容,链接,发布时间,来源,创建时间) VALUES (%s, %s,%s, %s, %s, %s, %s, %s, %s, %s)"
    cursorM.executemany(sql, itemdata)
    logger.info("数据插入数据库成功！")
    # 定义插入数据的SQL语句
    # 执行插入操作
    conx.commit()
    closeSql(conx, cursorM)


def get_detail_html():
    while True:
        # todo:从redis中读取数据
            title = detailmsg['title']
            detailUrl = detailmsg['detailUrl']
            logger.info("%s:%s开始解析详情数据\n" % (title, detailUrl))
            try:
                # try:
                #     self.driver.get(detailUrl)
                # except Exception as e:
                #     self.driver.quit()
                #     self.driver=self.createDriver()
                #     self.driver.get(detailUrl)
                # response = self.driver.page_source
                try:
                    bdetail = getDetailmsg(detailmsg)
                    # 'content':content,
                    # 'contentHtml':contentWithTag,
                    content = bdetail['content']
                    contentHtml = bdetail['contentHtml']
                    if len(content) < 100:
                        continue
                    soup = BeautifulSoup(contentHtml, "html.parser")
                    # 查找所有带有class属性的元素
                    elements_with_class = soup.find_all(class_=True)
                    # 循环遍历元素并去掉class属性
                    for element in elements_with_class:
                        del element.attrs["class"]
                    contentHtml = str(soup)
                    bdetail['content'] = content
                    bdetail['contentHtml'] = contentHtml
                except Exception as e:
                    logger.info('详情解析失败')
                    continue

                processitem = getProcessitem(bdetail)
                # uniqueCode = self.baseCore.getUniqueCode('GG', '195', self.threadId)
                # processitem['uniqueCode'] = uniqueCode
                try:
                    # flg = self.sendkafka(processitem)
                    flg = True
                    if flg:
                        r.sadd('pygoogle_' + 'google', processitem['sourceAddress'])
                        # 插入数据库
                        try:
                            items = []
                            items.apitemInsertToTablepend(bdetail)
                            (items)
                        except Exception as e:
                            logger.info(f"插入数据库失败！{bdetail['kword']}===={e}")
                        # self.logger.info(f"放入kafka成功！{bdetail['kword']}===={detailUrl}")
                        # self.sendMonitor(processitem)
                except Exception as e:
                    logger.info(f"{e}{bdetail['kword']}===={detailUrl}")

                # 关闭当前新窗口
                # self.driver.close()
                time.sleep(1)
            except Exception as e:
                time.sleep(5)
                logger.info("详情页解析异常！" + detailUrl)
        else:
            break
            # time.sleep(5)