import json
import time
import random
import requests


def get_tencent_video_url(video_id):
    url = "https://h5vv.video.qq.com/getinfo?vid={}&platform=101001&charge=0&otype=json&defn=shd".format(
        video_id
    )
    headers = {
        "Host": "h5vv.video.qq.com",
        "xweb_xhr": "1",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 MicroMessenger/6.8.0(0x16080000) NetType/WIFI MiniProgramEnv/Mac MacWechat/WMPF XWEB/30817",
        "Content-Type": "application/x-www-form-urlencoded",
        "Accept": "*/*",
        "Sec-Fetch-Site": "cross-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://servicewechat.com/wx5fcd817f3f80aece/3/page-frame.html",
        "Accept-Language": "en",
    }
    response = requests.get(url, headers=headers)
    result = json.loads(response.text.replace("QZOutputJson=", "")[:-1])
    vl = result["vl"]["vi"][0]
    key = vl["fvkey"]
    name = vl["fn"]
    folder = vl["ul"]["ui"][0]["url"]
    video_url = folder + name + "?vkey=" + key
    time.sleep(random.randint(1, 5))
    return video_url


def get_video_list():
    url = "https://txdby.leyoukge.com/api/getVideoListByPagination"
    params = {
        "i": 1,
        "v": "1.0.0",
        "class_id": 6,
        "page": 1
    }
    headers = {
        'Host': 'txdby.leyoukge.com',
        'xweb_xhr': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 MicroMessenger/6.8.0(0x16080000) NetType/WIFI MiniProgramEnv/Mac MacWechat/WMPF MacWechat/3.8.5(0x13080510)XWEB/1100',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': '*/*',
        'Referer': 'https://servicewechat.com/wx6665a673af83c3e8/3/page-frame.html',
        'Accept-Language': 'en-US,en;q=0.9'
    }
    response = requests.request("GET", url, headers=headers, params=params)
    # print(json.dumps(response.json(), indent=4, ensure_ascii=False))
    return response.json()


# result = get_video_list()
# print(json.dumps(result, ensure_ascii=False, indent=4))
for video_obj in get_video_list()['extra']:
    vid = video_obj['vid']
    print(video_obj['title'])
    url = get_tencent_video_url(vid)
    print(url)