# -*- coding: utf-8 -*-
# @ModuleName: old_video_recall
# @Author: Liqian
# @Time: 2022/4/25 下午3:33
# @Software: PyCharm
import pandas as pd
from datetime import datetime
from my_utils import get_data_from_odps, filter_video_status
from db_helper import RedisHelper
from my_config import set_config
from log import Log

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


def get_old_videos():
    """获取老旧视频"""
    now_date = datetime.strftime(datetime.today(), '%Y%m%d')
    log_.info(f"now_date = {now_date}")
    project = config_.OLD_VIDEOS_PROJECT
    table = config_.OLD_VIDEOS_TABLE
    records = get_data_from_odps(project=project, table=table, date=now_date)
    video_ids = [int(record['videoid']) for record in records]
    log_.info(f'videos count = {len(video_ids)}')
    if len(video_ids) > 0:
        # 对视频状态进行过滤
        filtered_videos = filter_video_status(list(video_ids))
        log_.info('filter videos status finished, filtered_videos count={}'.format(len(filtered_videos)))
        if not filtered_videos:
            log_.info('视频状态不符合分发')
            return None
        # 上传数据到redis
        key_name = f'{config_.RECALL_KEY_NAME_PREFIX_OLD_VIDEOS}{now_date}'
        redis_helper = RedisHelper()
        # 如果key已存在，删除key
        if redis_helper.key_exists(key_name):
            redis_helper.del_keys(key_name)
        # 写入redis
        redis_helper.add_data_with_set(key_name=key_name, values=filtered_videos, expire_time=24 * 3600)
        log_.info('data to redis finished!')
    else:
        log_.info(f'no data!')


if __name__ == '__main__':
    get_old_videos()
