# -*- coding: utf-8 -*-
import json
import re
import threading
import time
import uuid

import fitz
import redis
import requests
from bs4 import BeautifulSoup
from obs import ObsClient
from retry import retry
from elasticsearch import Elasticsearch
from base import BaseCore

baseCore = BaseCore.BaseCore()
log = baseCore.getLogger()
baseCore = BaseCore.BaseCore()

cnx_ = baseCore.cnx_
cursor_ = cnx_.cursor()

lock = threading.Lock()
pathType_ = 'QYResearchReport/'
taskType = '企业研报/东方财富网'

pool = redis.ConnectionPool(host="114.115.236.206", port=6379, password='clbzzsn', db=6)

obsClient = ObsClient(
        access_key_id='VEHN7D0TJ9316H8AHCAV',  # 你的华为云的ak码
        secret_access_key='heR353lvSWVPNU8pe2QxDtd8GDsO5L6PGH5eUoQY',  # 你的华为云的sk
        server='https://obs.cn-north-1.myhuaweicloud.com'  # 你的桶的地址
    )

class EsMethod(object):

    def __init__(self):
        # 创建Elasticsearch对象，并提供账号信息
        self.es = Elasticsearch(['http://114.116.19.92:9700'], http_auth=('elastic', 'zzsn9988'), timeout=300)
        self.index_name = 'researchreportdata'

    def queryatt(self,index_name,id):
       body = {
               "query": {
                "match": {
                       "id": id
                   }
               }

       }

       filter_path = ['hits.hits._id',
                      'hits.total.value',
                      'hits.hits._source.title',
                      'hits.hits._source.origin',
                      'hits.hits._source.sourceAddress',
                      'hits.hits._source.createDate',
                      'hits.hits._source.publishDate',
                      ]  # 字段2
       result = self.es.search(index=index_name
                               , doc_type='_doc'
                               , filter_path=filter_path
                               , body=body)
       # log.info(result)
       return result

    def updateaunn(self,index_name,id,u_publishDate):
        body = {
            'doc': {
                'publishDate':u_publishDate
            }
        }
        result = self.es.update(index=index_name
                                ,id=id
                                ,body=body)
        log.info('更新结果:%s' % result)


if __name__ == "__main__":
    esMethod = EsMethod()
    redis_conn = redis.Redis(connection_pool=pool)
    while True:
        id_ = redis_conn.lpop('YanBao:up')

        # id = "23112104300"
        if id:
            pass
        else:
            log.info('已无数据')
        id = id_.decode()
        result_ = esMethod.queryatt(index_name=esMethod.index_name, id=id)
        result = result_['hits']['hits'][0]
        num = 0
        publishDate = result['_source']['publishDate']
        u_publishDate = '2023-08-31' #+ publishDate.split('T')[1]
        esMethod.updateaunn(esMethod.index_name, str(id), u_publishDate)
