# -*- coding: utf-8 -*-
# @ModuleName: bottom_videos_update
# @Author: Liqian
# @Time: 2022/8/31 19:46
# @Software: PyCharm

import traceback
import datetime

from pool_predict import get_videos_from_flow_pool
from my_utils import filter_video_status
from db_helper import RedisHelper
from my_config import set_config
from log import Log

config_, _ = set_config()
log_ = Log()


def get_bottom_videos():
    """获取流量池视频作为兜底视频"""
    now_date = datetime.datetime.today()
    log_.info(f"now_date = {now_date}")
    app_type_list = [
        config_.APP_TYPE['VLOG'],
        config_.APP_TYPE['LOVE_LIVE'],
        config_.APP_TYPE['LONG_VIDEO'],
        config_.APP_TYPE['SHORT_VIDEO']
    ]

    video_ids = []
    for app_type in app_type_list:
        # 从流量池获取数据
        videos = get_videos_from_flow_pool(app_type=app_type)
        if len(videos) <= 0:
            continue
        for video in videos:
            video_id = int(video['videoId'])
            if video_id not in video_ids:
                video_ids.append(video_id)
    log_.info(f"video_ids count = {len(video_ids)}")
    filtered_videos = filter_video_status(video_ids)
    log_.info(f"filtered_videos count = {len(filtered_videos)}")
    if len(filtered_videos) > 0:
        # redis数据更新
        redis_helper = RedisHelper()
        redis_helper.del_keys(key_name=config_.BOTTOM2_KEY_NAME)
        redis_helper.add_data_with_set(key_name=config_.BOTTOM2_KEY_NAME, values=tuple(filtered_videos))
        # 移除bottom key的过期时间，将其转换为永久状态
        redis_helper.persist_key(key_name=config_.BOTTOM_KEY_NAME)
        log_.info('{} update bottom videos success!, count = {}'.format(now_date, len(filtered_videos)))


if __name__ == '__main__':
    get_bottom_videos()
