import datetime
import traceback
import datetime
from my_config import set_config
from log import Log
from my_utils import execute_sql_from_odps
from db_helper import RedisHelper

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


def get_benshanzhufu_videos():
    """获取本山祝福视频并存入redis"""
    try:
        # 获取本山祝福视频
        sql = f"SELECT videoid FROM {config_.PROJECT_BENSHAN_ZHUFU}.{config_.TABLE_BENSHAN_ZHUFU};"
        records = execute_sql_from_odps(project=config_.PROJECT_BENSHAN_ZHUFU, sql=sql)
        video_id_list = []
        with records.open_reader() as reader:
            for record in reader:
                video_id = int(record['videoid'])
                video_id_list.append(video_id)
        log_.info(f"benshanzhufu videos count = {len(video_id_list)}")
        # 存入redis
        if len(video_id_list) > 0:
            redis_helper.del_keys(key_name=config_.BENSHAN_ZHUFU_KEY_NAME)
            redis_helper.add_data_with_set(key_name=config_.BENSHAN_ZHUFU_KEY_NAME, values=video_id_list,
                                           expire_time=25 * 3600)

    except Exception as e:
        log_.error(traceback.format_exc())


def get_xng_videos():
    """获取小年糕视频并存入redis"""
    try:
        # 获取小年糕视频
        sql = f"SELECT videoid FROM {config_.PROJECT_XNG}.{config_.TABLE_XNG};"
        records = execute_sql_from_odps(project=config_.PROJECT_XNG, sql=sql)
        video_id_list = []
        with records.open_reader() as reader:
            for record in reader:
                video_id = int(record['videoid'])
                video_id_list.append(video_id)

        log_.info(f"xng videos count = {len(video_id_list)}")
        # 存入redis
        if len(video_id_list) > 0:
            redis_helper.del_keys(key_name=config_.XNG_KEY_NAME)
            redis_helper.add_data_with_set(key_name=config_.XNG_KEY_NAME, values=video_id_list,
                                           expire_time=25 * 3600)

    except Exception as e:
        log_.error(traceback.format_exc())


def get_special_area_limit_videos():
    """获取特殊地域屏蔽视频并存入redis"""
    try:
        # 获取特殊地域屏蔽视频
        sql = f"SELECT videoid FROM {config_.PROJECT_SPECIAL_AREA_LIMIT}.{config_.TABLE_SPECIAL_AREA_LIMIT};"
        records = execute_sql_from_odps(project=config_.PROJECT_SPECIAL_AREA_LIMIT, sql=sql)

        video_id_list = []
        with records.open_reader() as reader:
            for record in reader:
                video_id = int(record['videoid'])
                video_id_list.append(video_id)
        log_.info(f"special area limit videos count = {len(video_id_list)}")
        # 存入redis
        if len(video_id_list) > 0:
            redis_helper.del_keys(key_name=config_.SPECIAL_AREA_LIMIT_KEY_NAME)
            redis_helper.add_data_with_set(key_name=config_.SPECIAL_AREA_LIMIT_KEY_NAME, values=video_id_list,
                                           expire_time=25 * 3600)

    except Exception as e:
        log_.error(traceback.format_exc())


def get_political_videos():
    """获取涉政视频并存入redis"""
    try:
        # 获取涉政视频
        sql = f"SELECT videoid FROM {config_.PROJECT_POLITICAL_VIDEOS}.{config_.TABLE_POLITICAL_VIDEOS};"
        records = execute_sql_from_odps(project=config_.PROJECT_POLITICAL_VIDEOS, sql=sql)
        video_id_list = []
        with records.open_reader() as reader:
            for record in reader:
                video_id = int(record['videoid'])
                video_id_list.append(video_id)
        log_.info(f"political videos count = {len(video_id_list)}")
        # 存入redis
        if len(video_id_list) > 0:
            redis_helper.del_keys(key_name=config_.POLITICAL_VIDEOS_KEY_NAME)
            redis_helper.add_data_with_set(key_name=config_.POLITICAL_VIDEOS_KEY_NAME, values=video_id_list,
                                           expire_time=25 * 3600)
    except Exception as e:
        log_.error(traceback.format_exc())


def main():
    now_h = datetime.datetime.now().hour
    if now_h == 4:
        get_benshanzhufu_videos()
    get_xng_videos()
    get_special_area_limit_videos()
    get_political_videos()


if __name__ == '__main__':
    main()
