Spaces:
Running
Running
admin
commited on
Commit
·
eaa17fe
1
Parent(s):
a944375
upl base codes
Browse files- .gitignore +3 -0
- README.md +2 -2
- app.py +113 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
*__pycache__*
|
2 |
+
test.*
|
3 |
+
rename.sh
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Bili Parser
|
3 |
-
emoji:
|
4 |
colorFrom: purple
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
@@ -8,7 +8,7 @@ sdk_version: 5.20.0
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
-
short_description:
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Bili Parser
|
3 |
+
emoji: 📺
|
4 |
colorFrom: purple
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
+
short_description: Parse Bilibili videos
|
12 |
---
|
13 |
|
14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import shutil
|
4 |
+
import requests
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
|
8 |
+
def download_file(url, video_id, cache_dir="./__pycache__"):
|
9 |
+
if os.path.exists(cache_dir):
|
10 |
+
shutil.rmtree(cache_dir)
|
11 |
+
|
12 |
+
os.makedirs(cache_dir)
|
13 |
+
local_file = f"{cache_dir}/{video_id}.mp4"
|
14 |
+
response = requests.get(url, stream=True)
|
15 |
+
if response.status_code == 200:
|
16 |
+
with open(local_file, "wb") as file:
|
17 |
+
for chunk in response.iter_content(chunk_size=8192):
|
18 |
+
file.write(chunk)
|
19 |
+
|
20 |
+
return local_file
|
21 |
+
|
22 |
+
|
23 |
+
def extract_fst_url(text):
|
24 |
+
url_pattern = r'(https?://[^\s"]+)'
|
25 |
+
match = re.search(url_pattern, text)
|
26 |
+
if match:
|
27 |
+
out_url = match.group(1)
|
28 |
+
if out_url[-1] == "/":
|
29 |
+
out_url = out_url[:-1]
|
30 |
+
|
31 |
+
return out_url
|
32 |
+
|
33 |
+
else:
|
34 |
+
return f"https://www.bilibili.com/video/{text}"
|
35 |
+
|
36 |
+
|
37 |
+
def get_real_url(short_url):
|
38 |
+
return requests.get(
|
39 |
+
short_url,
|
40 |
+
headers={
|
41 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36"
|
42 |
+
},
|
43 |
+
allow_redirects=True,
|
44 |
+
timeout=10,
|
45 |
+
).url.split("/?")[0]
|
46 |
+
|
47 |
+
|
48 |
+
def infer(video_url: str):
|
49 |
+
title = cover = desc = dur = video = author = avatar = None
|
50 |
+
if not video_url:
|
51 |
+
title = "Empty video link!"
|
52 |
+
return title, cover, video, desc, dur, avatar, author
|
53 |
+
|
54 |
+
video_url = extract_fst_url(video_url)
|
55 |
+
try:
|
56 |
+
if "b23.tv" in video_url:
|
57 |
+
video_url = get_real_url(video_url)
|
58 |
+
|
59 |
+
response = requests.get(os.getenv("api"), params={"url": video_url})
|
60 |
+
response_json = response.json()
|
61 |
+
retcode = response_json["code"]
|
62 |
+
if retcode == 1:
|
63 |
+
title = response_json["title"]
|
64 |
+
cover = response_json["imgurl"]
|
65 |
+
desc = response_json["desc"]
|
66 |
+
|
67 |
+
response_data = response_json["data"][0]
|
68 |
+
dur = response_data["duration"]
|
69 |
+
video_id = video_url.split("/")[-1]
|
70 |
+
video = download_file(response_data["video_url"], video_id)
|
71 |
+
|
72 |
+
author_data = response_json["user"]
|
73 |
+
author = author_data["name"]
|
74 |
+
avatar = author_data["user_img"]
|
75 |
+
|
76 |
+
else:
|
77 |
+
title = f"Failed to call API, error code: {retcode}"
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
title = f"Failed to parse video: {e}"
|
81 |
+
|
82 |
+
return title, cover, video, desc, dur, avatar, author
|
83 |
+
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
gr.Interface(
|
87 |
+
fn=infer,
|
88 |
+
inputs=[
|
89 |
+
gr.Textbox(
|
90 |
+
label="Please input Bilibili video link",
|
91 |
+
placeholder="https://www.bilibili.com/video/*",
|
92 |
+
show_copy_button=True,
|
93 |
+
),
|
94 |
+
],
|
95 |
+
outputs=[
|
96 |
+
gr.Textbox(label="Video title", show_copy_button=True),
|
97 |
+
gr.Image(label="Video cover"),
|
98 |
+
gr.Video(label="Download video", show_download_button=True),
|
99 |
+
gr.TextArea(label="Video introduction", show_copy_button=True),
|
100 |
+
gr.Textbox(label="Video duration(s)", show_copy_button=True),
|
101 |
+
gr.Image(label="Uploader avatar"),
|
102 |
+
gr.Textbox(label="Uploader nickname", show_copy_button=True),
|
103 |
+
],
|
104 |
+
title="Bilibili video parser",
|
105 |
+
description="This site does not provide any video storage services, only to provide the most basic resolution services",
|
106 |
+
flagging_mode="never",
|
107 |
+
examples=[
|
108 |
+
"BV1Dt4y1o7bU",
|
109 |
+
"https://b23.tv/LuTAbzj",
|
110 |
+
"https://www.bilibili.com/video/BV1Dt4y1o7bU",
|
111 |
+
],
|
112 |
+
cache_examples=False,
|
113 |
+
).launch()
|