import requests
import urllib.parse
import re
from urllib.parse import quote_plus
"""
快手
"""


def is_valid_kuaishou_profile(url: str) -> bool:

    pattern = re.compile(r'''
        ^https://                  # 必须以https协议开头
        www\.kuaishou\.com         # 固定域名
        /f/                        # 固定路径前缀
        [0-9a-zA-Z_-]{10,20}       # 用户ID：10-20位数字、字母、下划线或连字符
        $                          # 字符串结束
    ''', re.VERBOSE)

    return bool(pattern.match(url))

def get_ks_info(video_url):
    url = ""
    if is_valid_kuaishou_profile(video_url):
        url = f"http://172.16.18.10:8875/api/v1/ks?share_url={quote_plus(video_url)}"

    payload={}
    headers = {}
    if url == "":
        return None

    response = requests.request("GET", url, headers=headers, data=payload)

    return response.json()

"""
小红书
"""

def is_valid_xiaohongshu_link(url: str) -> bool:
    pattern = re.compile(
        r'^https://www\.xiaohongshu\.com/(?:discovery|explore)/'
        r'(?:item/)?[0-9a-fA-F]{24}\?'
        r'(?:[^&]+&)*?'
        r'xsec_token=[^&]+'
        r'(?:&[^&]+)*?$'
    )

    xhslink_pattern = re.compile(r'^http://xhslink\.com/m/[A-Za-z0-9]{11}$')

    return bool(pattern.match(url) or xhslink_pattern.match(url))


def get_xhs_info(video_url):
    url = ""
    if is_valid_xiaohongshu_link(video_url):
        url = f"http://172.16.18.10:8875/api/v1/xhs?share_url={quote_plus(video_url)}"
    payload = {}
    headers = {}
    if url == "":
        return None
    response = requests.request("GET", url, headers=headers, data=payload)

    return response.json()

"""
抖音图文
"""
def get_dy_ocr_info(video_url):
    url = ""
    pattern1 = r'(?:douyin\.com/(?:video|share/video|note|jingxuan)/|video_id|modal_id=)(\d+)'
    match1 = re.search(pattern1, video_url)
    if match1:
        url = f"http://172.16.18.10:8875/api/v1/ocr?video_id={match1.group(1)}"

    pattern = r'https?://v\.douyin\.com/[a-zA-Z0-9-]+/?'
    pattern = r'https?://v\.douyin\.com/[a-zA-Z0-9_\-]+/?'

    if re.search(pattern, video_url):
        url = f"http://172.16.18.10:8875/api/v1/ocr?video_share_url={video_url}"
    payload = {}
    headers = {}
    if url == "":
        return None
    response = requests.request("GET", url, headers=headers, data=payload)

    return response.json()


def is_valid_bilibili_video(url: str) -> bool:

    pattern = re.compile(r'''
        ^https://                  # 必须以https协议开头
        www\.bilibili\.com         # 固定域名
        /video/                    # 固定路径
        BV[0-9a-zA-Z]{10,30}       # BV号（放宽长度限制为10-15位）
        /?\?                       # 允许视频ID后有一个可选的斜杠，再必须跟问号
        .+                         # 问号后至少有一个参数
        $                          # 字符串结束
    ''', re.VERBOSE)

    return bool(pattern.match(url))


def get_bilibili_info(video_url):
    url = ""
    if is_valid_bilibili_video(video_url):
        url = f"http://172.16.18.10:8875/api/v1/bilibili?share_url={quote_plus(video_url)}"
    payload = {}
    headers = {}
    if url == "":
        return None
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()

if __name__ == '__main__':

    # #
    # # url= "https://v.kuaishou.com/KFy0JkVY"
    # # "29 【要当可爱的水手啦～～～ - 夹克本人 | 小红书 - 你的生活兴趣社区】 😆 tx3lJkm14vxxx1A 😆 https://www.xiaohongshu.com/discovery/item/6894c614000000000403dcad?source=webshare&xhsshare=pc_web&xsec_token=CBmYaPr2HJVyffdsKGD_zkN5gOTLXgasc3dPOzNRGjKhs=&xsec_source=pc_share"
    # # #
    url= "https://www.xiaohongshu.com/discovery/item/69d6aae5000000002102da3e?source=webshare&xhsshare=pc_web&xsec_token=CBxnnqlpIrloQXUm5cqwsVQpcNWp7RDwb9XCFKqq_d510=&xsec_source=pc_share "
    result = get_xhs_info(url)
    print(result)
    # # print(len(result))