import datetime
import json
import traceback

import pandas as pd
import requests

from common_func import NpEncoder, get_db_engine, get_logger, mysql_replace_into
from config.url import get_video_info_from_kuaishou_kaiyan
from get_material_and_script.get_script_from_tengxunyun import GetScriptFromTengXunYunServer


class GetMaterialFromKuaishouKaiyan(object):
    def __init__(self, query_word, logger, db_engine):
        self.query_word = query_word  # 查询词
        self.logger = logger
        self.db_engine = db_engine
        self.video_df = pd.DataFrame()

    def get_video_basic_info(self):
        """
        依据关键词调用快手开眼，获取视频列表，得到视频基本信息如：视频时长、宽度、高度、标题、url链接等
        """
        # 1 请求快手开眼快创接口获取视频基本信息
        page_num = 1
        page_size = 10
        total_size = 0
        self.logger.info(f"查询词：{self.query_word}, 调用快手开眼快创视频接口，开始执行...")
        while True:
            # 分页获取
            self.logger.info(f"查询词：{self.query_word}, 调用快手开眼快创视频接口，分页获取第 {page_num} 页")
            request_data = {"inspiredSortTypeId": 0,
                            "platformSourceId": 0,
                            "formatId": 0,
                            "pageNum": page_num,
                            "pageSize": page_size,
                            "queryCond": self.query_word,
                            "isFromWeb": False,
                            "accountType": 0,
                            "campaignType": 0,
                            "firstIndustryIds": [],
                            "secondIndustryIds": []}
            try:
                request = requests.post(url=get_video_info_from_kuaishou_kaiyan,
                                        headers={'Content-Type': 'application/json'},
                                        data=json.dumps(request_data, cls=NpEncoder)
                                        )
                response_data = json.loads(request.text)
                if response_data.get('msg') == 'OK' and response_data.get('data').get('inspiredAds'):
                    video_page_df = pd.DataFrame(response_data.get('data').get('inspiredAds'))
                    # 从 mainMvUrls 字段提取视频的播放链接
                    video_page_df['video_url'] = video_page_df['mainMvUrls'].apply(lambda x: x[0]['url'])
                    self.video_df = self.video_df.append(video_page_df)

                    total_size = response_data.get('data').get('totalSize')
                if page_num * page_size > total_size:
                    break
                else:
                    page_num += 1
            except:
                self.logger.error(f"查询词：{self.query_word}, 调用快手开眼快创视频接口，获取第{page_num}页时发生异常信息:{traceback.format_exc()}")

        self.logger.info(f"查询词：{self.query_word}, 调用快手开眼快创视频接口，结束执行，共 {total_size} 个视频！")

        # 2 获取到的信息存入数据库
        # 2-1 数据字段类型的处理，方便入库
        # mainMvUrls: list to  str
        # coverThumbnailUrls:  list to str
        # headUrls:  list to str
        if not self.video_df.empty and not self.video_df[~self.video_df.photoId.isnull()].empty:
            self.video_df = self.video_df[self.video_df.photoId.isnull()]
            self.video_df[['mainMvUrls', 'coverThumbnailUrls', 'headUrls', 'photoId']] = \
                self.video_df[['mainMvUrls', 'coverThumbnailUrls', 'headUrls', 'photoId']].astype(str)

            self.video_df.rename(columns={'photoId': 'signature'}, inplace=True)

            # 2-2 添加查询词和日期
            self.video_df['query_word'] = self.query_word
            self.video_df['stat_date'] = datetime.datetime.today().strftime('%Y-%m-%d')

            # 2-3 写入数据库, 表中以 signature + query_word + stat_date 作为联合唯一键，写入数据库时如果唯一键重复，则 replace_into
            try:
                self.video_df.to_sql(name="ctop_ai_material_info_from_kuaishou_kaiyan",
                                     con=self.db_engine,
                                     if_exists='append',
                                     index=False,
                                     chunksize=1000,
                                     method=mysql_replace_into)
            except:
                self.logger.error(traceback.format_exc())

    def get_script_from_teng_xun_yun(self):
        get_script_ins = GetScriptFromTengXunYunServer(logger=self.logger,
                                                       db_engine=self.db_engine,
                                                       task_df=self.video_df[['signature', 'video_url']],
                                                       task_ids=None)
        get_script_ins.submit_task()
        if get_script_ins.task_ids:
            get_script_ins.get_result()


if __name__ == '__main__':
    db_info = {'user': 'hcst',
               'password': 'hcst@2020',
               'host': '139.186.165.84',
               'port': 3306,
               'database': 'db_ai_word'}
    db_engine = get_db_engine(db_info)

    logger = get_logger(log_file_name="/data/pythonProject/video_to_word/logs/get_video_from_kuaishou_kaiyan.log",
                        log_name='get_video_from_kuaishou_kaiyan_logger')
    logger.info("get_video_from_kuaishou_kaiyan started! id of logger is: %s" % id(logger))

    inst = GetMaterialFromKuaishouKaiyan(query_word='电商', logger=logger, db_engine=db_engine)
    inst.get_video_basic_info()

# 2147483647
# 57795440082
