File size: 6,322 Bytes
0d6128e
 
065a96f
 
0d11e5e
065a96f
 
 
 
 
bf7d327
065a96f
 
0d6128e
 
 
7eabc38
0d6128e
 
 
 
 
 
 
 
7eabc38
065a96f
 
 
0d6128e
 
 
 
bf7d327
0d6128e
 
0d11e5e
 
 
 
0d6128e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
065a96f
0d6128e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
065a96f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d11e5e
 
065a96f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d11e5e
 
 
 
065a96f
 
 
 
 
 
 
 
 
 
 
0d6128e
065a96f
0d6128e
 
49e3a43
 
 
0d6128e
 
 
 
0d11e5e
0d6128e
 
 
065a96f
 
 
 
0d6128e
 
 
 
 
 
 
 
 
 
 
 
065a96f
 
 
 
 
0d6128e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
065a96f
 
 
0d6128e
065a96f
0d6128e
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import requests
import gradio as gr
from utils import (
    download_file,
    extract_fst_url,
    HEADER,
    TIMEOUT,
    API_BILI,
    API_BILI_2,
    API_BILI_1,
    EN_US,
    TMP_DIR,
)

ZH2EN = {
    "状态栏": "Status",
    "请输入B站视频链接": "Please input BiliBili video link",
    "分P": "Part",
    "视频标题": "Video title",
    "视频封面": "Video cover",
    "视频下载": "Download video",
    "视频简介": "Video introduction",
    "视频时长(s)": "Video duration(s)",
    "UP主头像": "Uploader avatar",
    "UP主昵称": "Uploader nickname",
    "B站视频解析": "BiliBili video parser",
    "通道": "Channel",
    "接口1": "Interface 1",
    "接口2": "Interface 2",
}


def _L(zh_txt: str):
    return ZH2EN[zh_txt] if EN_US else zh_txt


def get_fst_url(text):
    fst_url = extract_fst_url(text)
    if fst_url:
        out_url = fst_url.split("?")[0]
        if out_url[-1] == "/":
            out_url = out_url[:-1]

        return out_url

    else:
        return f"https://www.bilibili.com/video/{text}"


def get_real_url(short_url):
    return requests.get(
        short_url,
        headers=HEADER,
        allow_redirects=True,
        timeout=TIMEOUT,
    ).url.split("/?")[0]


def get_video(video_url: str, p: int, cache: str):
    bvid = video_url.split("bilibili.com/video/")[-1]
    response = requests.get(
        API_BILI_2,
        params={
            "bv": bvid,
            "p": p,
            "otype": "json",
        },
        timeout=TIMEOUT,
    )
    response_json = response.json()
    retcode = response_json["code"]
    if retcode == 0:
        qualities: list = response_json["accept_quality"]
        quality = min(qualities[0], 80)
        url = requests.get(
            API_BILI_2,
            params={
                "bv": bvid,
                "p": p,
                "q": quality,
                "otype": "url",
            },
            timeout=TIMEOUT,
        ).text
        return download_file(url, bvid, cache)

    else:
        raise ConnectionError(f"Failed to get video: {retcode}")


def channel_2(video_url: str, p: int, cache: str):
    response = requests.get(
        API_BILI_1,
        params={
            "url": video_url,
            "type": "json",
        },
        timeout=TIMEOUT,
    )
    response_json = response.json()
    retcode = response_json["code"]
    if retcode == 200:
        title = response_json["title"]
        cover = response_json["imgurl"]
        desc = response_json["desc"]

        response_data = response_json["data"][int(p) - 1]
        dur = response_data["duration"]

        author_data = response_json["user"]
        author = author_data["name"]
        avatar = author_data["user_img"]

        video = get_video(video_url, p, cache)

        return title, cover, video, desc, dur, avatar, author

    else:
        raise ConnectionError(f"接口调用失败, 错误码: {retcode}")


def channel_1(video_url: str, p: int, cache: str):
    response = requests.get(API_BILI, params={"url": video_url}, timeout=TIMEOUT)
    response_json = response.json()
    retcode = response_json["code"]
    if retcode == 1:
        title = response_json["title"]
        cover = response_json["imgurl"]
        desc = response_json["desc"]

        author_data = response_json["user"]
        author = author_data["name"]
        avatar = author_data["user_img"]

        response_data = response_json["data"][int(p) - 1]
        dur = response_data["duration"]
        video_id = video_url.split("/")[-1]
        video = download_file(response_data["video_url"], video_id, cache)

        return title, cover, video, desc, dur, avatar, author

    else:
        raise ConnectionError(f"Failed to call API, error code: {retcode}")


# outer func
def infer(ch: str, video_url: str, p: int, cache=f"{TMP_DIR}/bili"):
    status = "Success"
    title = cover = desc = dur = video = author = avatar = None
    if not ch:
        ch == _L("接口1")

    try:
        if not video_url:
            raise ValueError("视频链接为空!")

        video_url = get_fst_url(video_url)
        if "b23.tv" in video_url:
            video_url = get_real_url(video_url)

        title, cover, video, desc, dur, avatar, author = (
            channel_1(video_url, p, cache)
            if ch == _L("接口1")
            else channel_2(video_url, p, cache)
        )

    except Exception as e:
        status = f"视频解析失败: {e}"

    return status, title, cover, video, desc, dur, avatar, author


def bili_parser():
    return gr.Interface(
        fn=infer,
        inputs=[
            gr.Dropdown(
                label=_L("通道"),
                choices=[_L("接口1"), _L("接口2")],
                value=_L("接口1"),
            ),
            gr.Textbox(
                label=_L("请输入B站视频链接"),
                placeholder="https://www.bilibili.com/video/*",
            ),
            gr.Slider(label=_L("分P"), minimum=1, maximum=1000, step=1, value=1),
        ],
        outputs=[
            gr.Textbox(label=_L("状态栏"), show_copy_button=True),
            gr.Textbox(label=_L("视频标题"), show_copy_button=True),
            gr.Image(label=_L("视频封面"), show_share_button=False),
            gr.Video(
                label=_L("视频下载"),
                show_download_button=True,
                show_share_button=False,
                format="mp4",
            ),
            gr.TextArea(label=_L("视频简介"), show_copy_button=True),
            gr.Textbox(label=_L("视频时长(s)"), show_copy_button=True),
            gr.Image(label=_L("UP主头像"), show_share_button=False),
            gr.Textbox(label=_L("UP主昵称"), show_copy_button=True),
        ],
        title=_L("B站视频解析"),
        flagging_mode="never",
        examples=[
            [_L("接口1"), "BV1Dt4y1o7bU", 1],
            [_L("接口1"), "https://b23.tv/LSoJzpW", 1],
            [_L("接口2"), "https://www.bilibili.com/video/BV1G8iRYBE4f", 1],
            [
                _L("接口2"),
                "【『新年おめでとう | 2024』你就如同烟花一般,在我心中绽放!-哔哩哔哩】 https://b23.tv/LuTAbzj",
                1,
            ],
        ],
        cache_examples=False,
    )