Skip to content

Commit

Permalink
add config ENABLE_LIVE_ROOM_AS_VIDEO and allow search video
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Jun 2, 2024
1 parent 790556d commit 883e8ca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 8 additions & 0 deletions fuo_bilibili/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
ui_mgr: Optional[BUiManager] = None


def init_config(config):
config.deffield('ENABLE_LIVE_ROOM_AS_VIDEO',
type_=bool,
default=True,
desc='treat live room as video')


# noinspection PyProtectedMember
def enable(app: Union[App, GuiApp]):
global ui_mgr
provider.enable_live_room_as_video = app.config.bilibili.ENABLE_LIVE_ROOM_AS_VIDEO
app.library.register(provider)
if app.mode & App.GuiMode:
ui_mgr = BUiManager(app, provider)
Expand Down
23 changes: 23 additions & 0 deletions fuo_bilibili/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
PROVIDER_ID = __identifier__


def get_text_from_html(html):
return BeautifulSoup(html, features="html.parser").get_text()


class BBriefAlbumModel(BriefAlbumModel):
cover: str = ''

Expand Down Expand Up @@ -241,6 +245,7 @@ def create_model(cls, request: SearchRequest, response: Union[SearchResponse, Tu
for result in results:
if isinstance(result, SearchResultVideo):
songs.append(BSongModel.create_model(result))
videos.append(BVideoModel.create_video_model(result))
elif isinstance(result, SearchResultUser):
artists.append(cls.search_user_model(result))
elif isinstance(result, SearchResultLiveRoom):
Expand Down Expand Up @@ -477,3 +482,21 @@ def create_live_model(cls, live: LiveFeedListResponse.LiveFeedListResponseData.L
duration=0,
cover=live.cover,
)

@classmethod
def create_video_model(cls, result: SearchResultVideo) -> 'VideoModel':
return VideoModel(
source=__identifier__,
identifier=result.bvid,
title=get_text_from_html(result.title),
artists=[BriefArtistModel(
source=__identifier__,
name=result.author,
identifier=result.mid
)],
duration=result.duration.total_seconds(),
cover=result.pic,
play_count=result.play,
released=result.pubdate.strftime('%Y-%m-%d'),
)

12 changes: 9 additions & 3 deletions fuo_bilibili/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
from fuo_bilibili.util import json_to_lrc_text

SEARCH_TYPE_MAP = {
FuoSearchType.vi: BilibiliSearchType.LIVE_ROOM, # 对应直播间
# 对应直播间
FuoSearchType.vi: BilibiliSearchType.LIVE_ROOM,
FuoSearchType.ar: BilibiliSearchType.BILI_USER, # 对应B站用户
FuoSearchType.so: BilibiliSearchType.VIDEO, # 对应投稿视频
FuoSearchType.al: (BilibiliSearchType.MEDIA, BilibiliSearchType.BANGUMI), # 对应番剧电影
Expand Down Expand Up @@ -100,16 +101,21 @@ class meta:
ModelType.album: (Pf.model_v2 | Pf.get),
}

def __init__(self):
def __init__(self, enable_live_room_as_video=True):
super(BilibiliProvider, self).__init__()
self._api = BilibiliApi()
self._user = None
self._video_quality_codes = dict()
self._video_cids = dict()
self._video_avids = dict()

self.enable_live_room_as_video = enable_live_room_as_video

def _format_search_request(self, keyword, type_) -> Union[SearchRequest, Tuple[SearchRequest]]:
btype = SEARCH_TYPE_MAP.get(type_)
if self.enable_live_room_as_video is False and type_ == FuoSearchType.vi:
btype = BilibiliSearchType.VIDEO
else:
btype = SEARCH_TYPE_MAP.get(type_)
if btype is None:
raise NotImplementedError
if isinstance(btype, Tuple):
Expand Down

0 comments on commit 883e8ca

Please sign in to comment.