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'(?:&[^&]+)*?$'
    )
    return bool(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.douyin.com/--n8QjSuvh4/"
    url= "https://www.douyin.com/jingxuan?modal_id=7584179438045121842"
    print(get_dy_ocr_info(url))