"""
@author: luojunhui
"""
import time
import json
import random
import hashlib
from datetime import datetime, timedelta

import uuid
import requests
import urllib.parse


def request_for_info(video_id):
    """
    请求数据
    :param video_id:
    :return:
    """
    url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
    data = {
        "videoIdList": [video_id]
    }
    header = {
        "Content-Type": "application/json",
    }
    response = requests.post(url, headers=header, data=json.dumps(data))
    return response.json()


def get_info_lists(vid_list):
    """
    获取视频list
    :param vid_list:
    :return:
    """
    url = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo"
    data = {
        "videoIdList": vid_list
    }
    header = {
        "Content-Type": "application/json",
    }
    response = requests.post(url, headers=header, data=json.dumps(data))
    return response.json()


def generate_daily_strings(start_date, end_date):
    """
    Generate daily date_str
    :param start_date:
    :param end_date:
    :return:
    """
    start = datetime.strptime(start_date, "%Y%m%d")
    end = datetime.strptime(end_date, "%Y%m%d")
    current = start
    date_strings = []
    while current <= end:
        date_strings.append(current.strftime("%Y%m%d"))
        current += timedelta(days=1)
    return date_strings


def whisper(video_id):
    """
    input video_id, output video_text
    :param video_id:
    :return:
    """
    url = "http://61.48.133.26:5999/video_to_text"
    body = {
        "video_id": video_id
    }
    header = {
        "Content-Type": "application/json",
    }
    response = requests.post(
        url=url,
        json=body,
        headers=header
    )
    return response.json()


def hash_title(title):
    """
    hash map
    :param title:
    :return:
    """
    # 创建md5哈希对象
    hash_object = hashlib.md5()

    # 对标题进行编码
    title_bytes = title.encode('utf-8')

    # 更新哈希对象
    hash_object.update(title_bytes)

    # 获取十六进制形式的哈希值
    hash_hex = hash_object.hexdigest()

    return hash_hex


def create_gzh_path(video_id, shared_uid):
    """
    :param video_id: 视频 id
    :param shared_uid: 分享 id
    """

    def generate_source_id():
        """
        generate_source_id
        :return:
        """
        timestamp = str(int(time.time() * 1000))
        random_str = str(random.randint(1000, 9999))
        hash_input = f"{timestamp}-{random_str}"
        return hashlib.md5(hash_input.encode()).hexdigest()

    root_share_id = str(uuid.uuid4())
    source_id = "video_to_articles" + generate_source_id()
    url = f"pages/user-videos?id={video_id}&su={shared_uid}&fromGzh=1&rootShareId={root_share_id}&shareId={root_share_id}&rootSourceId={source_id}"
    # 自动把 root_share_id 加入到白名单
    # auto_white(root_share_id)
    return root_share_id, source_id, f"pages/category?jumpPage={urllib.parse.quote(url, safe='')}"


def chunks(chunk_list, chunk_size):
    """
    分页
    :param chunk_list:
    :param chunk_size:
    """
    for i in range(0, len(chunk_list), chunk_size):
        yield chunk_list[i: i + chunk_size]
