File size: 3,341 Bytes
0f30007
 
97fe330
0f30007
 
 
 
97fe330
 
 
 
 
 
7741f33
 
 
 
 
97fe330
 
 
 
0f30007
 
 
 
 
 
 
 
 
 
21e99f3
0f30007
 
 
 
 
 
 
 
 
 
0dcf4cd
0f30007
 
 
 
97fe330
 
0f30007
21e99f3
0f30007
 
 
 
 
 
 
 
 
 
327d8d0
0f30007
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21e99f3
0f30007
 
21e99f3
0f30007
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import re
import shutil
import requests
import gradio as gr


def download_file(url, video_id, cache_dir="./__pycache__"):
    if os.path.exists(cache_dir):
        shutil.rmtree(cache_dir)

    os.makedirs(cache_dir)
    local_file = f"{cache_dir}/{video_id}.mp4"
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        with open(local_file, "wb") as file:
            for chunk in response.iter_content(chunk_size=8192):
                file.write(chunk)

    return local_file


def extract_fst_url(text):
    url_pattern = r'(https?://[^\s"]+)'
    match = re.search(url_pattern, text)
    if match:
        return match.group(1)
    else:
        return None


def infer(video_url):
    video = parse_time = desc = avatar = author = sign = None
    if not video_url:
        desc = "The video sharing link is empty!"
        return video, parse_time, desc, avatar, author, sign

    video_url = extract_fst_url(video_url)
    if not video_url:
        desc = "Please enter a valid video sharing link!"
        return video, parse_time, desc, avatar, author, sign

    try:
        response = requests.get(os.getenv("api"), params={"url": video_url})
        response_json = response.json()
        retcode = response_json["code"]
        if retcode == 200:
            response_data = response_json["data"]
            video_id = response_data["play_url"].split("video_id=")[1].split("&")[0]
            video = download_file(response_data["video_url"], video_id)
            parse_time = response_data["parse_time"]

            additional_data = response_data["additional_data"][0]
            desc = additional_data["desc"]
            avatar = additional_data["url"].split("?from=")[0]
            author = additional_data["nickname"]
            sign = additional_data["signature"]

        else:
            desc = f"Interface call failed, error code: HTTP {retcode}"

    except Exception as e:
        desc = f"Video parsing failed: {e}"

    return video, parse_time, desc, avatar, author, sign


if __name__ == "__main__":
    gr.Interface(
        fn=infer,
        inputs=[
            gr.Textbox(
                label="Please enter TikTok video sharing short link",
                placeholder="https://v.douyin.com/*",
                show_copy_button=True,
            ),
        ],
        outputs=[
            gr.Video(label="Video download", show_download_button=True),
            gr.Textbox(label="Parsing time", show_copy_button=True),
            gr.Textbox(label="Video description", show_copy_button=True),
            gr.Image(label="Author avatar"),
            gr.Textbox(label="Author nickname", show_copy_button=True),
            gr.TextArea(label="Author signature", show_copy_button=True),
        ],
        title="Parse TikTok video without watermark",
        description="This site does not provide any video storage services, only to provide the most basic resolution services",
        flagging_mode="never",
        examples=[
            "https://v.douyin.com/iUsp2kWc",
            "5.17 06/17 [email protected] OXM:/ MC——创世! 密码的 这咋玩?# 我的世界 # 我的世界极限生存 # 冰℃  https://v.douyin.com/i5MXfJac/ 复制此链接,打开Dou音搜索,直接观看视频!",
        ],
        cache_examples=False,
    ).launch()