Skip to content

Commit

Permalink
fix v tail - bug
Browse files Browse the repository at this point in the history
  • Loading branch information
HFrost0 committed Dec 18, 2022
1 parent 4cd44c1 commit c3a69d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
12 changes: 6 additions & 6 deletions bilix/api/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ async def get_up_info(client: httpx.AsyncClient, url_or_mid: str, pn=1, ps=30, o
@dataclass
class VideoInfo:
title: str
h1_title: str
h1_title: str # for bv same to title, but for tv or bangumi title will be more specific
aid: Union[str, int]
cid: Union[str, int]
p: int
pages: Sequence[Sequence[str]]
pages: Sequence[Sequence[str]] # [[p_name, p_url], ...]
img_url: str
status: dict
bvid: str = None
Expand Down Expand Up @@ -197,8 +197,8 @@ async def get_video_info(client: httpx.AsyncClient, url) -> VideoInfo:
base_url = url.split('?')[0]
for idx, i in enumerate(init_info['videoData']['pages']):
p_url = f"{base_url}?p={idx + 1}"
add_name = f"P{idx + 1}-{i['part']}" if len(init_info['videoData']['pages']) > 1 else ''
pages.append([add_name, p_url])
p_name = f"P{idx + 1}-{i['part']}" if len(init_info['videoData']['pages']) > 1 else ''
pages.append([p_name, p_url])
elif 'initEpList' in init_info: # 动漫,电视剧,电影
stat = init_info['mediaInfo']['stat']
status = {
Expand All @@ -218,8 +218,8 @@ async def get_video_info(client: httpx.AsyncClient, url) -> VideoInfo:
title = legal_title(re.search('property="og:title" content="([^"]*)"', res.text).groups()[0])
for idx, i in enumerate(init_info['initEpList']):
p_url = i['link']
add_name = i['title']
pages.append([add_name, p_url])
p_name = i['title']
pages.append([p_name, p_url])
else:
raise AttributeError("未知类型")

Expand Down
39 changes: 16 additions & 23 deletions bilix/download/downloader_bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,20 @@ async def get_series(self, url: str, quality: Union[str, int] = 0, image=False,
hierarchy = self._make_hierarchy_dir(hierarchy, title)
else:
hierarchy = hierarchy if type(hierarchy) is str else '' # incase hierarchy is False
cors = [self.get_video(p_url, quality, add_name,
image=image,
subtitle=subtitle, dm=dm, only_audio=only_audio, codec=codec, hierarchy=hierarchy,
video_info=video_info if idx == p else None)
for idx, (add_name, p_url) in enumerate(pages)]
cors = [self.get_video(p_url, quality=quality, image=image, subtitle=subtitle, dm=dm, only_audio=only_audio,
codec=codec, hierarchy=hierarchy, video_info=video_info if idx == p else None)
for idx, (_, p_url) in enumerate(pages)]
if p_range:
cors = cors_slice(cors, p_range)
await asyncio.gather(*cors)

async def get_video(self, url: str, quality: Union[str, int] = 0, add_name='', image=False, subtitle=False,
async def get_video(self, url: str, quality: Union[str, int] = 0, image=False, subtitle=False,
dm=False, only_audio=False, codec: str = '', hierarchy: str = '', video_info=None):
"""
下载单个视频
:param url: 视频的url
:param quality: 画面质量,0为可以观看的最高画质,越大质量越低,超过范围时自动选择最低画质,或者直接使用字符串指定'1080p'等名称
:param add_name: 给文件的额外添加名,用户请直接保持默认
:param image: 是否下载封面
:param subtitle: 是否下载字幕
:param dm: 是否下载弹幕
Expand All @@ -345,19 +342,20 @@ async def get_video(self, url: str, quality: Union[str, int] = 0, add_name='', i
except AttributeError as e:
logger.warning(f'{url} {e}')
return
title = video_info.h1_title
title = legal_title(title, add_name)
video_info.title = title # update video_info title
p = video_info.p
p_name = video_info.pages[p][0]
# join p_name and title
title = legal_title(video_info.h1_title, p_name)
img_url = video_info.img_url
if not video_info.dash:
logger.warning(f'{video_info.title} 需要大会员或该地区不支持')
logger.warning(f'{title} 需要大会员或该地区不支持')
return
# choose video quality
try:
video, audio = choose_quality(video_info, quality, codec)
except ValueError:
logger.warning(
f"{video_info.title} 清晰度<{quality}> 编码<{codec}>不可用,请检查输入是否正确或是否需要大会员")
f"{title} 清晰度<{quality}> 编码<{codec}>不可用,请检查输入是否正确或是否需要大会员")
return

file_dir = f'{self.videos_dir}/{hierarchy}' if hierarchy else self.videos_dir
Expand Down Expand Up @@ -414,12 +412,10 @@ async def get_dm(self, url, update=False, convert_func=None, hierarchy: str = ''
"""
if not video_info:
video_info = await api.get_video_info(self.client, url)
title = video_info.title
aid, cid = video_info.aid, video_info.cid

file_dir = f'{self.videos_dir}/{hierarchy}' if hierarchy else self.videos_dir
file_type = '.' + ('bin' if not convert_func else convert_func.__name__.split('2')[-1])
file_name = f"{title}-弹幕{file_type}"
file_name = legal_title(video_info.h1_title, video_info.pages[video_info.p][0], "弹幕") + file_type
file_path = f'{file_dir}/{file_name}'
if not update and os.path.exists(file_path):
logger.info(f"[green]已存在[/green] {file_name}")
Expand All @@ -436,32 +432,29 @@ async def get_dm(self, url, update=False, convert_func=None, hierarchy: str = ''
logger.info(f"[cyan]已完成[/cyan] {file_name}")
return file_path

async def get_subtitle(self, url, convert=True, hierarchy: str = '', video_info=None):
async def get_subtitle(self, url, convert_func=json2srt, hierarchy: str = '', video_info=None):
"""
获取某个视频的字幕文件
:param url: 视频url
:param convert: 是否转换成srt
:param convert_func: function used to convert original subtitle text
:param hierarchy:
:param video_info: 额外数据,提供则不再访问前端
:return:
"""
if not video_info:
video_info = await api.get_video_info(self.client, url)
p, cid = video_info.p, video_info.cid
title = video_info.title
add_name = video_info.pages[p][0]
title = legal_title(title, add_name)
p_name = video_info.pages[p][0]
try:
subtitles = await api.get_subtitle_info(self.client, video_info.bvid, cid)
except AttributeError as e:
logger.warning(f'{url} {e}')
return
cors = []
for sub_url, sub_name in subtitles:
file_name = f"{title}-{sub_name}"
cors.append(self._get_static(sub_url, file_name, convert_func=json2srt if convert else None,
hierarchy=hierarchy))
file_name = legal_title(video_info.h1_title, p_name, sub_name)
cors.append(self._get_static(sub_url, file_name, convert_func=convert_func, hierarchy=hierarchy))
paths = await asyncio.gather(*cors)
return paths

Expand Down
7 changes: 4 additions & 3 deletions bilix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ async def merge_files(file_list: Sequence[str], new_name: str):
os.rename(first_file, new_name)


def legal_title(*parts: str, join_str: str = '-', max_length=100):
def legal_title(*parts: str, join_str: str = '-', max_length=150):
"""
join several string parts to os illegal file/dir name (no illegal character and not too long)
join several string parts to os illegal file/dir name (no illegal character and not too long).
auto skip empty.
:param parts:
:param join_str: the string to join each part
:param max_length: joined file name max length, tail will be truncated if overly long
:return:
"""
return f'{join_str.join(map(replace_illegal, parts)):.{max_length}}'
return f'{join_str.join(filter(lambda x: len(x) > 0, map(replace_illegal, parts))):.{max_length}}'


def replace_illegal(s: str):
Expand Down

0 comments on commit c3a69d3

Please sign in to comment.