Skip to content

Commit

Permalink
feat: videos with multiple episodes
Browse files Browse the repository at this point in the history
Signed-off-by: jingfelix <jingfelix@outlook.com>
  • Loading branch information
jingfelix committed Oct 10, 2023
1 parent 93259fd commit 2cb10dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 48 deletions.
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from src.bilifm import app

if __name__ == "__main__":
app()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="bilifm",
version="0.1.6",
version="0.1.7",
author="Felix Jing",
author_email="jingfelix@outlook.com",
description="Download Bilibili videos as audios.",
Expand Down
6 changes: 3 additions & 3 deletions src/bilifm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import typer

from bilifm.audio import Audio
from bilifm.user import User
from bilifm.fav import Fav
from .audio import Audio
from .user import User
from .fav import Fav

app = typer.Typer()

Expand Down
94 changes: 50 additions & 44 deletions src/bilifm/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

class Audio:
bvid = ""
cid = ""
title = ""
playUrl = "http://api.bilibili.com/x/player/playurl"
baseUrl = ""
part_list = []

def __init__(self, bvid: str) -> None:
if bvid is None:
Expand All @@ -25,17 +24,8 @@ def __init__(self, bvid: str) -> None:
self.__get_cid_title(bvid)
else:
# AV号
self.__get_cid_title(bvid[:12], int(bvid[13:]))
self.__get_cid_title(bvid[:12])

params = {
"fnval": 16,
"bvid": self.bvid,
"cid": self.cid,
}

self.baseUrl = requests.get(self.playUrl, params=params).json()["data"]["dash"][
"audio"
][0]["baseUrl"]

def download(self):
headers = {
Expand All @@ -51,35 +41,46 @@ def download(self):

start_time = time.time()
try:
response = requests.get(url=self.baseUrl, headers=headers, stream=True)

total_size = int(response.headers.get("content-length", 0))
temp_size = 0

# 如果文件已存在,则跳过下载
if os.path.exists(self.title + ".mp3"):
typer.echo(f"{self.title} already exists, skip for now")
return

with open(self.title + ".mp3", "wb") as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()

temp_size += len(chunk)
done = int(50 * temp_size / total_size)
sys.stdout.write(
"\r[%s%s] %s/%s %s"
% (
"#" * done,
"-" * (50 - done),
temp_size,
total_size,
self.title,
for cid, part in zip(self.cid_list, self.part_list):
baseUrl = requests.get(
self.playUrl,
params={
"fnval": 16,
"bvid": self.bvid,
"cid": cid,
},
).json()["data"]["dash"]["audio"][0]["baseUrl"]

response = requests.get(url=baseUrl, headers=headers, stream=True)

total_size = int(response.headers.get("content-length", 0))
temp_size = 0

# 如果文件已存在,则跳过下载
file_path = f"{self.title}-{part}.mp3"
if os.path.exists(file_path):
typer.echo(f"{self.title} already exists, skip for now")
return

with open(file_path, "wb") as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()

temp_size += len(chunk)
done = int(50 * temp_size / total_size)
sys.stdout.write(
"\r[%s%s] %s/%s %s"
% (
"#" * done,
"-" * (50 - done),
temp_size,
total_size,
self.title,
)
)
)
sys.stdout.flush()
sys.stdout.flush()

except Exception as e:
typer.echo("Download failed")
Expand All @@ -92,24 +93,29 @@ def download(self):
" " + str(round(end_time - start_time, 2)) + " seconds download finish\n"
)

def __get_cid_title(self, bvid: str, p: int = 1):
def __get_cid_title(self, bvid: str):
url = "https://api.bilibili.com/x/web-interface/view?bvid={bvid}".format(
bvid=bvid
)
try:
response = requests.get(url)
data = response.json().get("data")
self.title = data.get("title")
self.title = self.__title_process(data.get("title"))

# 这里是否也应该也使用get方法?
self.cid = str(data["pages"][p - 1]["cid"])
self.cid_list = [str(page["cid"]) for page in data["pages"]]
self.part_list = [self.__title_process(str(page["part"])) for page in data["pages"]]

except ValueError as e:
raise e

except Exception as e:
raise e


def __title_process(self, title: str):
replaceList = ["?", "\\", "*", "|", "<", ">", ":", "/", " "]
for ch in replaceList:
self.title = self.title.replace(ch, "-")
title = title.replace(ch, "-")

return title

0 comments on commit 2cb10dc

Please sign in to comment.