Spaces:
Running
Running
admin
commited on
Commit
·
065a96f
1
Parent(s):
0d6128e
merge 2 bili apis
Browse files- modules/bili.py +82 -30
- utils.py +1 -0
modules/bili.py
CHANGED
@@ -1,7 +1,16 @@
|
|
1 |
import re
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
-
from utils import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
ZH2EN = {
|
7 |
"状态栏": "Status",
|
@@ -15,6 +24,9 @@ ZH2EN = {
|
|
15 |
"UP主头像": "Uploader avatar",
|
16 |
"UP主昵称": "Uploader nickname",
|
17 |
"B站视频解析": "Bilibili video parser",
|
|
|
|
|
|
|
18 |
}
|
19 |
|
20 |
|
@@ -48,7 +60,7 @@ def get_real_url(short_url):
|
|
48 |
).url.split("/?")[0]
|
49 |
|
50 |
|
51 |
-
def get_video(video_url: str, p: int, cache
|
52 |
bvid = video_url.split("bilibili.com/video/")[-1]
|
53 |
response = requests.get(
|
54 |
API_BILI_2,
|
@@ -80,8 +92,62 @@ def get_video(video_url: str, p: int, cache=f"{TMP_DIR}/bili"):
|
|
80 |
raise ConnectionError(f"Failed to get video: {retcode}")
|
81 |
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# outer func
|
84 |
-
def infer(video_url: str, p: int):
|
85 |
status = "Success"
|
86 |
title = cover = desc = dur = video = author = avatar = None
|
87 |
try:
|
@@ -92,31 +158,11 @@ def infer(video_url: str, p: int):
|
|
92 |
if "b23.tv" in video_url:
|
93 |
video_url = get_real_url(video_url)
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
"type": "json",
|
100 |
-
},
|
101 |
-
timeout=TIMEOUT,
|
102 |
)
|
103 |
-
response_json = response.json()
|
104 |
-
retcode = response_json["code"]
|
105 |
-
if retcode == 200:
|
106 |
-
title = response_json["title"]
|
107 |
-
cover = response_json["imgurl"]
|
108 |
-
desc = response_json["desc"]
|
109 |
-
|
110 |
-
response_data = response_json["data"][int(p) - 1]
|
111 |
-
dur = response_data["duration"]
|
112 |
-
video = get_video(video_url, p)
|
113 |
-
|
114 |
-
author_data = response_json["user"]
|
115 |
-
author = author_data["name"]
|
116 |
-
avatar = author_data["user_img"]
|
117 |
-
|
118 |
-
else:
|
119 |
-
raise ConnectionError(f"接口调用失败, 错误码: {retcode}")
|
120 |
|
121 |
except Exception as e:
|
122 |
status = f"视频解析失败: {e}"
|
@@ -128,6 +174,11 @@ def bili_parser():
|
|
128 |
return gr.Interface(
|
129 |
fn=infer,
|
130 |
inputs=[
|
|
|
|
|
|
|
|
|
|
|
131 |
gr.Textbox(
|
132 |
label=_L("请输入B站视频链接"),
|
133 |
placeholder="https://www.bilibili.com/video/*",
|
@@ -152,10 +203,11 @@ def bili_parser():
|
|
152 |
title=_L("B站视频解析"),
|
153 |
flagging_mode="never",
|
154 |
examples=[
|
155 |
-
["BV1Dt4y1o7bU", 1],
|
156 |
-
["https://b23.tv/
|
157 |
-
["https://www.bilibili.com/video/
|
158 |
[
|
|
|
159 |
"【『新年おめでとう | 2024』你就如同烟花一般,在我心中绽放!-哔哩哔哩】 https://b23.tv/LuTAbzj",
|
160 |
1,
|
161 |
],
|
|
|
1 |
import re
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
+
from utils import (
|
5 |
+
download_file,
|
6 |
+
HEADER,
|
7 |
+
TIMEOUT,
|
8 |
+
API_BILI,
|
9 |
+
API_BILI_2,
|
10 |
+
API_BILI_1,
|
11 |
+
LANG,
|
12 |
+
TMP_DIR,
|
13 |
+
)
|
14 |
|
15 |
ZH2EN = {
|
16 |
"状态栏": "Status",
|
|
|
24 |
"UP主头像": "Uploader avatar",
|
25 |
"UP主昵称": "Uploader nickname",
|
26 |
"B站视频解析": "Bilibili video parser",
|
27 |
+
"通道": "Channel",
|
28 |
+
"接口1": "Interface 1",
|
29 |
+
"接口2": "Interface 2",
|
30 |
}
|
31 |
|
32 |
|
|
|
60 |
).url.split("/?")[0]
|
61 |
|
62 |
|
63 |
+
def get_video(video_url: str, p: int, cache: str):
|
64 |
bvid = video_url.split("bilibili.com/video/")[-1]
|
65 |
response = requests.get(
|
66 |
API_BILI_2,
|
|
|
92 |
raise ConnectionError(f"Failed to get video: {retcode}")
|
93 |
|
94 |
|
95 |
+
def channel_2(video_url: str, p: int, cache: str):
|
96 |
+
response = requests.get(
|
97 |
+
API_BILI_1,
|
98 |
+
params={
|
99 |
+
"url": video_url,
|
100 |
+
"type": "json",
|
101 |
+
},
|
102 |
+
timeout=TIMEOUT,
|
103 |
+
)
|
104 |
+
response_json = response.json()
|
105 |
+
retcode = response_json["code"]
|
106 |
+
if retcode == 200:
|
107 |
+
title = response_json["title"]
|
108 |
+
cover = response_json["imgurl"]
|
109 |
+
desc = response_json["desc"]
|
110 |
+
|
111 |
+
response_data = response_json["data"][int(p) - 1]
|
112 |
+
dur = response_data["duration"]
|
113 |
+
video = get_video(video_url, p, cache)
|
114 |
+
|
115 |
+
author_data = response_json["user"]
|
116 |
+
author = author_data["name"]
|
117 |
+
avatar = author_data["user_img"]
|
118 |
+
|
119 |
+
return title, cover, video, desc, dur, avatar, author
|
120 |
+
|
121 |
+
else:
|
122 |
+
raise ConnectionError(f"接口调用失败, 错误码: {retcode}")
|
123 |
+
|
124 |
+
|
125 |
+
def channel_1(video_url: str, p: int, cache: str):
|
126 |
+
response = requests.get(API_BILI, params={"url": video_url}, timeout=TIMEOUT)
|
127 |
+
response_json = response.json()
|
128 |
+
retcode = response_json["code"]
|
129 |
+
if retcode == 1:
|
130 |
+
title = response_json["title"]
|
131 |
+
cover = response_json["imgurl"]
|
132 |
+
desc = response_json["desc"]
|
133 |
+
|
134 |
+
response_data = response_json["data"][int(p) - 1]
|
135 |
+
dur = response_data["duration"]
|
136 |
+
video_id = video_url.split("/")[-1]
|
137 |
+
video = download_file(response_data["video_url"], video_id, cache)
|
138 |
+
|
139 |
+
author_data = response_json["user"]
|
140 |
+
author = author_data["name"]
|
141 |
+
avatar = author_data["user_img"]
|
142 |
+
|
143 |
+
return title, cover, video, desc, dur, avatar, author
|
144 |
+
|
145 |
+
else:
|
146 |
+
raise ConnectionError(f"Failed to call API, error code: {retcode}")
|
147 |
+
|
148 |
+
|
149 |
# outer func
|
150 |
+
def infer(ch: str, video_url: str, p: int, cache=f"{TMP_DIR}/bili"):
|
151 |
status = "Success"
|
152 |
title = cover = desc = dur = video = author = avatar = None
|
153 |
try:
|
|
|
158 |
if "b23.tv" in video_url:
|
159 |
video_url = get_real_url(video_url)
|
160 |
|
161 |
+
title, cover, video, desc, dur, avatar, author = (
|
162 |
+
channel_1(video_url, p, cache)
|
163 |
+
if ch == _L("接口1")
|
164 |
+
else channel_2(video_url, p, cache)
|
|
|
|
|
|
|
165 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
except Exception as e:
|
168 |
status = f"视频解析失败: {e}"
|
|
|
174 |
return gr.Interface(
|
175 |
fn=infer,
|
176 |
inputs=[
|
177 |
+
gr.Dropdown(
|
178 |
+
label=_L("通道"),
|
179 |
+
choices=[_L("接口1"), _L("接口2")],
|
180 |
+
value=_L("接口1"),
|
181 |
+
),
|
182 |
gr.Textbox(
|
183 |
label=_L("请输入B站视频链接"),
|
184 |
placeholder="https://www.bilibili.com/video/*",
|
|
|
203 |
title=_L("B站视频解析"),
|
204 |
flagging_mode="never",
|
205 |
examples=[
|
206 |
+
[_L("接口1"), "BV1Dt4y1o7bU", 1],
|
207 |
+
[_L("接口1"), "https://b23.tv/LSoJzpW", 1],
|
208 |
+
[_L("接口2"), "https://www.bilibili.com/video/BV1G8iRYBE4f", 1],
|
209 |
[
|
210 |
+
_L("接口2"),
|
211 |
"【『新年おめでとう | 2024』你就如同烟花一般,在我心中绽放!-哔哩哔哩】 https://b23.tv/LuTAbzj",
|
212 |
1,
|
213 |
],
|
utils.py
CHANGED
@@ -7,6 +7,7 @@ from datetime import datetime
|
|
7 |
|
8 |
LANG = os.getenv("language")
|
9 |
API_TIKTOK = os.getenv("api_tiktok")
|
|
|
10 |
API_BILI_1 = os.getenv("api_bili_1")
|
11 |
API_BILI_2 = os.getenv("api_bili_2")
|
12 |
if not (API_TIKTOK and API_BILI_1 and API_BILI_2):
|
|
|
7 |
|
8 |
LANG = os.getenv("language")
|
9 |
API_TIKTOK = os.getenv("api_tiktok")
|
10 |
+
API_BILI = os.getenv("api_bili")
|
11 |
API_BILI_1 = os.getenv("api_bili_1")
|
12 |
API_BILI_2 = os.getenv("api_bili_2")
|
13 |
if not (API_TIKTOK and API_BILI_1 and API_BILI_2):
|