# -*- coding: utf-8 -*-
# @Author: lierqiang
# @Time: 2023/4/06
import json
import os
import shutil
import sys
import time
import requests
from hashlib import md5
from common.mq import MQ
sys.path.append(os.getcwd())
from douyin.douyin_recommend import get_xb
from common.common import Common
from common.feishu import Feishu
from common.scheduling_db import MysqlHelper
from common.publish import Publish
from common.userAgent import get_random_user_agent
from common.public import random_title, get_config_from_mysql, download_rule


class DouyinrecommendScheduling:
    platform = "抖音"

    @classmethod
    def video_title(cls, log_type, crawler, env, title):
        title_split1 = title.split(" #")
        if title_split1[0] != "":
            title1 = title_split1[0]
        else:
            title1 = title_split1[-1]

        title_split2 = title1.split(" #")
        if title_split2[0] != "":
            title2 = title_split2[0]
        else:
            title2 = title_split2[-1]

        title_split3 = title2.split("@")
        if title_split3[0] != "":
            title3 = title_split3[0]
        else:
            title3 = title_split3[-1]

        video_title = title3.strip().replace("\n", "") \
                          .replace("/", "").replace("抖音", "").replace(" ", "") \
                          .replace(" ", "").replace("&NBSP", "").replace("\r", "") \
                          .replace("#", "").replace(".", "。").replace("\\", "") \
                          .replace(":", "").replace("*", "").replace("？", "") \
                          .replace("?", "").replace('"', "").replace("<", "") \
                          .replace(">", "").replace("|", "").replace("@", "").replace('"', '').replace("'", '')[:40]
        if video_title.replace(" ", "") == "" or video_title == "。。。" or video_title == "...":
            return random_title(log_type, crawler, env, text='title')
        else:
            return video_title

    @classmethod
    def get_videoList(cls, log_type, crawler, rule_dict, our_uid, env):
        mq = MQ(topic_name="topic_crawler_etl_" + env)
        for page in range(1, 101):
            Common.logger(log_type, crawler).info(f"正在抓取第{page}页\n")
            Common.logging(log_type, crawler, env, f"正在抓取第{page}页\n")
            try:
                aweme_pc_rec_raw_data = '%7B%22videoPrefer%22:%7B%22fsn%22:%5B%5D,%22like%22:%5B%5D,%22halfMin%22:%5B%227188684310696742200%22,%224380080926896941%22%5D,%22min%22:%5B%5D%7D,%22seo_info%22:%22https:%2F%2Fwww.douyin.com%2F%22,%22is_client%22:false,%22ff_danmaku_status%22:1,%22danmaku_switch_status%22:1%7D'
                f_url = 'https://www.douyin.com/aweme/v1/web/tab/feed/?device_platform=webapp&aid=6383&channel=channel_pc_web&tag_id=&share_aweme_id=&count=10&refresh_index={page}&video_type_select=1&aweme_pc_rec_raw_data={aweme_pc_rec_raw_data}&globalwid=&pull_type=2&min_window=0&ug_source=&creative_id=&pc_client_type=1&version_code=170400&version_name=17.4.0&cookie_enabled=true&screen_width=1440&screen_height=900&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=109.0.0.0&browser_online=true&engine_name=Blink&engine_version=109.0.0.0&os_name=Mac+OS&os_version=10.15.7&cpu_core_num=8&device_memory=8&platform=PC&downlink=10&effective_type=4g&round_trip_time=50&webid=7219223873342260736&msToken=Sh6bVLWZUEZ3ruIHq1L3iUXnr1GT5yklyo-XZRO7lNgsFvpYq0C7tcu5Z4Jv9DrMESZ9kGVhwKT4ftFDkBL11ZGPUxth2ToA4M4q-qs2MK9ctR7GhwFkGw=='.format(
                    page=page, aweme_pc_rec_raw_data=aweme_pc_rec_raw_data)
                headers = {
                    'cookie': 'ttwid=1%7CI2Xp275XabSiVJ9GAmfLtqbtqturVSIS2yLbXVkAHnQ%7C1680856567%7Cfd36579475157e2303e36e5fd75cdea4ebad78c20da989be0590305f169242ca; douyin.com; strategyABtestKey=%221680856567.817%22; passport_csrf_token=6a28a7b1e1ad38570cc5ee39deaf587a; passport_csrf_token_default=6a28a7b1e1ad38570cc5ee39deaf587a; s_v_web_id=verify_lg6aloex_WG2LmlFk_kbA6_4tMb_BZlD_iuKxRvsMvrg7; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtY2xpZW50LWNzciI6Ii0tLS0tQkVHSU4gQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0tXHJcbk1JSUJEakNCdFFJQkFEQW5NUXN3Q1FZRFZRUUdFd0pEVGpFWU1CWUdBMVVFQXd3UFltUmZkR2xqYTJWMFgyZDFcclxuWVhKa01Ga3dFd1lIS29aSXpqMENBUVlJS29aSXpqMERBUWNEUWdBRWNFZitKV2tDb3l4OHhLekNnY0hReEVaRFxyXG5hN29maHJhUG9rMkQ5b1RaRGRvbDJuTVhaTis5dGJFclV0cVdUcm81ck4zekFyWTFLaXIzRlRUR2ZQUXRmS0FzXHJcbk1Db0dDU3FHU0liM0RRRUpEakVkTUJzd0dRWURWUjBSQkJJd0VJSU9kM2QzTG1SdmRYbHBiaTVqYjIwd0NnWUlcclxuS29aSXpqMEVBd0lEU0FBd1JRSWhBUFZaSGNFdW5HeGtBZFNmQXJ1MmdWb1RHbFhINkhsa1prRzZNc1pyR2hBL1xyXG5BaUJsV2NpM3h5SDk2UnJlTXpPSy8xVmFJQUNuTWUyU0RodUJIY2ZZaE80OWtRPT1cclxuLS0tLS1FTkQgQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0tXHJcbiJ9; csrf_session_id=a5355d954d3c63ed1ba35faada452b4d; ttcid=585ca9110e2345a09b4499dc543ec39959; odin_tt=d5fbc530c34bf8d4ea8cf8030c85a3f70202ee34dad7fb5b9ef7ff299d20e38ed01c852f02f2f3f864f2a45060480717f72083b58cdbc204c988edbf997fda7c; xgplayer_user_id=64984931555; SEARCH_RESULT_LIST_TYPE=%22single%22; pwa2=%222%7C1%22; download_guide=%223%2F20230407%22; __ac_nonce=0642fff310028297fec16; __ac_signature=_02B4Z6wo00f01M3AeJAAAIDDWRYjnPVAtVzN4HwAAFdlurQp2aR1Npvb7RYeaCY4fZs3DkMFlu7-Obn7zsvc34whBQesvTIc2p8nV-1crQtTacaxSYqP8nwNk3WqH.tkNdKQaUMw6sCC2.xZ15; VIDEO_FILTER_MEMO_SELECT=%7B%22expireTime%22%3A1681472712797%2C%22type%22%3A1%7D; home_can_add_dy_2_desktop=%221%22; tt_scid=PNmY2BL-Q9E9lAfdecenRGOzq64XgFOc0CJGFMN.JIE-QJO51S3Zvw56-Z6O12QVf4d9; msToken=uFCSX87jL9sTq6ScVYJucYgv9Hd5gCbTPvKIGBRMgVLuo7Pp9zLRrutBYzq4BmnCr83WnJAwZb8H78lBr3s3eyJLnySxYO5FgClQRXW1i_mAu7fLfBj3gA==; msToken=Sh6bVLWZUEZ3ruIHq1L3iUXnr1GT5yklyo-XZRO7lNgsFvpYq0C7tcu5Z4Jv9DrMESZ9kGVhwKT4ftFDkBL11ZGPUxth2ToA4M4q-qs2MK9ctR7GhwFkGw==',
                    'referer': 'https://www.douyin.com/',
                    'user-agent': get_random_user_agent('pc')
                }
                try:
                    x_bogus = get_xb(f_url, headers['user-agent'])
                    if not x_bogus:
                        continue
                    url = f_url + '&X-Bogus={}'.format(x_bogus)
                    res = requests.get(url=url, headers=headers, proxies=Common.tunnel_proxies()).json()
                    aweme_list = res.get('aweme_list', [])
                except Exception as e:
                    Common.logger(log_type, crawler).error(f"获取抖音推荐失败:{e}")
                    Common.logging(log_type, crawler, env, f"获取抖音推荐失败:{e}")
                    continue
                if not aweme_list:
                    Common.logger(log_type, crawler).warning(f"抖音推荐没有获取到更多数据,页数:{page}")
                    Common.logging(log_type, crawler, env, f"抖音推荐没有获取到更多数据,页数:{page}")
                    continue
                for info in aweme_list:
                    try:
                        if info.get('is_ads'):
                            continue
                        publish_time = info.get('create_time')
                        if not publish_time:
                            continue
                        publish_time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(publish_time))
                        publish_day = int((int(time.time()) - publish_time) / (3600 * 24))
                        if not info['desc']:
                            video_title = random_title(log_type, crawler, env, text='title')
                        else:
                            video_title = cls.video_title(log_type, crawler, env, info['desc'])

                        video_dict = {'video_title': video_title,
                                      'video_id': info['aweme_id'],
                                      'play_cnt': info['statistics']['play_count'],
                                      'comment_cnt': info['statistics']['comment_count'],
                                      'like_cnt': info['statistics']['digg_count'],
                                      'share_cnt': info['statistics']['share_count'],
                                      'video_width': info['video']['width'],
                                      'video_height': info['video']['height'],
                                      'duration': round(info['video']['duration'] / 1000),
                                      'publish_time': publish_day,
                                      'publish_time_stamp': publish_time,
                                      'publish_time_str': publish_time_str,
                                      'user_name': info['author']['nickname'],
                                      'user_id': info['author_user_id'],
                                      'user_sec_id': info['author']['sec_uid'],
                                      'avatar_url': info['author']['avatar_thumb']['url_list'][0],
                                      'cover_url': info['video']['origin_cover']['url_list'][0].replace('\u0026', '&'),
                                      'video_url': info['video']['play_addr']['url_list'][0],
                                      'session': f"douyin{int(time.time())}"
                                      }

                        for k, v in video_dict.items():
                            Common.logger(log_type, crawler).info(f"{k}:{v}")
                        Common.logging(log_type, crawler, env, f"{video_dict}")
                        if download_rule(log_type=log_type, crawler=crawler, video_dict=video_dict,
                                         rule_dict=rule_dict) is False:
                            Common.logger(log_type, crawler).info("不满足抓取规则\n")
                            Common.logging(log_type, crawler, env, "不满足抓取规则\n")
                        elif any(str(word) if str(word) in video_dict["video_title"] else False
                                 for word in get_config_from_mysql(log_type=log_type,
                                                                   source=crawler,
                                                                   env=env,
                                                                   text="filter",
                                                                   action="")) is True:
                            Common.logger(log_type, crawler).info('已中过滤词\n')
                            Common.logging(log_type, crawler, env, '已中过滤词\n')
                        elif cls.repeat_video(log_type, crawler, video_dict["video_id"], env) != 0:
                            Common.logger(log_type, crawler).info('视频已下载\n')
                            Common.logging(log_type, crawler, env, '视频已下载\n')
                        else:
                            # cls.download_publish(log_type=log_type,
                            #                      crawler=crawler,
                            #                      our_uid=our_uid,
                            #                      video_dict=video_dict,
                            #                      rule_dict=rule_dict,
                            #                      env=env)
                            video_dict["out_user_id"] = video_dict["user_id"]
                            video_dict["platform"] = crawler
                            video_dict["strategy"] = log_type
                            video_dict["out_video_id"] = video_dict["video_id"]
                            video_dict["width"] = video_dict["video_width"]
                            video_dict["height"] = video_dict["video_height"]
                            video_dict["crawler_rule"] = json.dumps(rule_dict)
                            video_dict["user_id"] = our_uid
                            video_dict["publish_time"] = video_dict["publish_time_str"]
                            video_dict["strategy_type"] = log_type
                            mq.send_msg(video_dict)
                    except Exception as e:
                        Common.logger(log_type, crawler).error(f"抓取单条视频异常:{e}\n")
                        Common.logging(log_type, crawler, env, f"抓取单条视频异常:{e}\n")
            except Exception as e:
                Common.logger(log_type, crawler).error(f"抓取第{page}页时异常:{e}\n")
                Common.logging(log_type, crawler, env, f"抓取第{page}页时异常:{e}\n")

    @classmethod
    def repeat_video(cls, log_type, crawler, video_id, env):
        # sql = f""" select * from crawler_video where platform="{cls.platform}" and out_video_id="{video_id}"; """
        sql = f""" select * from crawler_video where platform in ("{crawler}","{cls.platform}") and out_video_id="{video_id}"; """
        repeat_video = MysqlHelper.get_values(log_type, crawler, sql, env)
        return len(repeat_video)

    # 下载 / 上传
    @classmethod
    def download_publish(cls, log_type, crawler, video_dict, rule_dict, our_uid, env):
        # 下载视频
        Common.download_method(log_type=log_type, crawler=crawler, text='video', title=video_dict['video_title'], url=video_dict['video_url'])
        md_title = md5(video_dict['video_title'].encode('utf8')).hexdigest()
        try:
            if os.path.getsize(f"./{crawler}/videos/{md_title}/video.mp4") == 0:
                # 删除视频文件夹
                shutil.rmtree(f"./{crawler}/videos/{md_title}")
                Common.logger(log_type, crawler).info("视频size=0，删除成功\n")
                Common.logging(log_type, crawler, env, "视频size=0，删除成功\n")
                return
        except FileNotFoundError:
            # 删除视频文件夹
            shutil.rmtree(f"./{crawler}/videos/{md_title}")
            Common.logger(log_type, crawler).info("视频文件不存在，删除文件夹成功\n")
            Common.logging(log_type, crawler, env, "视频文件不存在，删除文件夹成功\n")
            return
        # 下载封面
        Common.download_method(log_type=log_type, crawler=crawler, text='cover', title=video_dict['video_title'], url=video_dict['cover_url'])
        # 保存视频信息至txt
        Common.save_video_info(log_type=log_type, crawler=crawler, video_dict=video_dict)

        # 上传视频
        Common.logger(log_type, crawler).info("开始上传视频...")
        Common.logging(log_type, crawler, env, "开始上传视频...")
        if env == "dev":
            oss_endpoint = "out"
            our_video_id = Publish.upload_and_publish(log_type=log_type,
                                                      crawler=crawler,
                                                      strategy="推荐抓取策略",
                                                      our_uid=our_uid,
                                                      env=env,
                                                      oss_endpoint=oss_endpoint)
            our_video_link = f"https://testadmin.piaoquantv.com/cms/post-detail/{our_video_id}/info"
        else:
            oss_endpoint = "inner"
            our_video_id = Publish.upload_and_publish(log_type=log_type,
                                                      crawler=crawler,
                                                      strategy="推荐抓取策略",
                                                      our_uid=our_uid,
                                                      env=env,
                                                      oss_endpoint=oss_endpoint)

            our_video_link = f"https://admin.piaoquantv.com/cms/post-detail/{our_video_id}/info"

        if our_video_id is None:
            try:
                # 删除视频文件夹
                shutil.rmtree(f"./{crawler}/videos/{md_title}")
                return
            except FileNotFoundError:
                return

        # 视频信息保存数据库
        insert_sql = f""" insert into crawler_video(video_id,
                        user_id,
                        out_user_id,
                        platform,
                        strategy,
                        out_video_id,
                        video_title,
                        cover_url,
                        video_url,
                        duration,
                        publish_time,
                        play_cnt,
                        comment_cnt,
                        like_cnt,
                        share_cnt,
                        crawler_rule,
                        width,
                        height)
                        values({our_video_id},
                        {our_uid},
                        "{video_dict['user_id']}",
                        "{cls.platform}",
                        "推荐爬虫策略",
                        "{video_dict['video_id']}",
                        "{video_dict['video_title']}",
                        "{video_dict['cover_url']}",
                        "{video_dict['video_url']}",
                        {int(video_dict['duration'])},
                        "{video_dict['publish_time_str']}",
                        {int(video_dict['play_cnt'])},
                        {int(video_dict['comment_cnt'])},
                        {int(video_dict['like_cnt'])},
                        {int(video_dict['share_cnt'])},
                        '{json.dumps(rule_dict)}',
                        {int(video_dict['video_width'])},
                        {int(video_dict['video_height'])}) """
        Common.logger(log_type, crawler).info(f"insert_sql:{insert_sql}")
        Common.logging(log_type, crawler, env, f"insert_sql:{insert_sql}")
        MysqlHelper.update_values(log_type, crawler, insert_sql, env)
        Common.logger(log_type, crawler).info('视频信息写入数据库成功')
        Common.logging(log_type, crawler, env, '视频信息写入数据库成功')

        # 视频写入飞书
        Feishu.insert_columns(log_type, crawler, "82c8d9", "ROWS", 1, 2)
        upload_time = int(time.time())
        values = [[
            our_video_id,
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(upload_time)),
            "推荐",
            str(video_dict['video_id']),
            video_dict['video_title'],
            our_video_link,
            video_dict['play_cnt'],
            video_dict['comment_cnt'],
            video_dict['like_cnt'],
            video_dict['share_cnt'],
            video_dict['duration'],
            str(video_dict['video_width']) + '*' + str(video_dict['video_height']),
            video_dict['publish_time_str'],
            video_dict['user_name'],
            video_dict['user_id'],
            video_dict['avatar_url'],
            video_dict['cover_url'],
            video_dict['video_url']
        ]]
        time.sleep(0.5)
        Feishu.update_values(log_type, crawler, "82c8d9", "A2:Z2", values)
        Common.logger(log_type, crawler).info(f"视频已保存至云文档\n")
        Common.logging(log_type, crawler, env, f"视频已保存至云文档\n")


if __name__ == '__main__':
    pass

