"""
@author: luojunhui
获取腾讯账号的 detail_info
"""
import requests
import json
import time

import schedule
from tqdm import tqdm
from datetime import datetime, timedelta

from applications.decoratorApi import retryOnTimeout
from applications import longArticlesMySQL, Functions


class getTencentAccountReadDetail(object):
    """
    获取腾讯平台前一天所有公众号群发文章的阅读
    """
    lam = longArticlesMySQL()

    @classmethod
    def generateAccountList(cls):
        """

        :return:
        """
        with open("../config/accountInfoV0914.json", encoding="utf-8") as f:
            account_list = json.loads(f.read())
        account_list = [
            i for i in account_list if i.get('follower_count') is not None
        ]
        return account_list

    @classmethod
    @retryOnTimeout()
    def getAccountAccessToken(cls, ghId):
        """
        通过 ghId获取账号的 accessToken
        :param ghId:
        :return:
        """
        url = "http://aigc-api.cybertogether.net/aigc/publish/account/getAccessTokenByGhId?ghId={}".format(ghId)
        response = requests.get(url, headers={"Content-Type": "Application/json"}, timeout=10)
        return response.text

    @classmethod
    @retryOnTimeout()
    def getAccountYesterdayReadInfo(cls, accessToken, daysCount):
        """
        获取账号昨天当天所有群发文章的阅读
        :param accessToken:
        :return:
        """
        yesterday = (datetime.today() - timedelta(days=daysCount)).strftime('%Y-%m-%d')
        print(yesterday)
        tencentUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token={}".format(accessToken)
        response = requests.post(
            url=tencentUrl,
            json={
                "begin_date": yesterday,
                "end_date": yesterday
            },
            timeout=10
        )
        print(response.json())
        return response.json()

    @classmethod
    def saveIntoDataBase(cls, infoTupleList):
        """
        :param infoTupleList:
        :param infoTuple:
        :return:
        info = (
                ghId,
                accountName,
                item['ref_date'],
                item['user_source'],
                item['msgid'].split("_")[0],
                item['msgid'].split("_")[1],
                item['title'],
                item['int_page_read_user'],
                item['int_page_read_count'],
                item['ori_page_read_user'],
                item['ori_page_read_count'],
                item['share_user'],
                item['share_count'],
                item['add_to_fav_user'],
                item['add_to_fav_count']
            )
        """
        insertSql = f"""
        INSERT INTO account_daily_read_info
        (ghId, accountName, refDate, userSource, msgId, position, title, readUser, readCount, oriPageReadUser, oriPageReadCount, shareUser, shareCount, favUser, favCount)
        values 
        (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
        """
        cls.lam.insertMany(
            sql=insertSql,
            params_list=infoTupleList
        )

    @classmethod
    def deal(cls):
        """
        main function
        :return:
        """
        accountList = cls.generateAccountList()
        for account in tqdm(accountList):
            try:
                accountName = account['name']
                ghId = account['ghId']
                accessToken = cls.getAccountAccessToken(ghId=ghId)
                detailList = cls.getAccountYesterdayReadInfo(accessToken=accessToken, daysCount=1).get('list', [])
                if detailList:
                    infoList = []
                    for item in detailList:
                        info = (
                            ghId,
                            accountName,
                            item['ref_date'],
                            item['user_source'],
                            item['msgid'].split("_")[0],
                            item['msgid'].split("_")[1],
                            item['title'],
                            item['int_page_read_user'],
                            item['int_page_read_count'],
                            item['ori_page_read_user'],
                            item['ori_page_read_count'],
                            item['share_user'],
                            item['share_count'],
                            item['add_to_fav_user'],
                            item['add_to_fav_count']
                        )
                        infoList.append(info)
                    cls.saveIntoDataBase(infoTupleList=infoList)
            except Exception as e:
                log = {
                    "error": str(e),
                    "msg": "更新账号文章失败",
                    "accountInfo": account
                }
                print(log)


def job():
    """
    job task
    :return:
    """
    g = getTencentAccountReadDetail()
    g.deal()


if __name__ == '__main__':
    schedule.every().day.at("08:15").do(Functions().job_with_thread, job)

    while True:
        schedule.run_pending()
        time.sleep(1)
