Skip to content

Commit

Permalink
feat: biliVideoSimilar
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Apr 5, 2024
1 parent b890ba4 commit 708b8e2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
11 changes: 11 additions & 0 deletions __tests__/mediafetch/biliVideoSimilar.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fetcher from '../../src/utils/mediafetch/biliVideoSimilar';

test('biliVideoSimilar', async () => {
const content = await fetcher.regexFetch({
reExtracted: fetcher.regexSearchMatch.exec(
'bilibili.com/video/similarvideo/BV1cf421Z7oQ'
)!,
});
// console.log(content);
expect(content?.songList[0]?.id).not.toBeNull();
});
17 changes: 11 additions & 6 deletions src/hooks/useSongOperations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useNoxSetting } from '@stores/useApp';
import { CIDPREFIX } from '@utils/mediafetch/ytbvideo';
import { logger } from '@utils/Logger';
import { SOURCE } from '@enums/MediaFetch';

Expand All @@ -10,16 +9,22 @@ const useSongOperations = () => {
const setSongMenuVisible = useNoxSetting(state => state.setSongMenuVisible);

const startRadio = (song: NoxMedia.Song) => {
if (song.id?.startsWith(CIDPREFIX)) {
setExternalSearchText(`youtu.be/list=RD${song.bvid}`);
} else {
logger.warn(`[startRadio] ${song.bvid} is not a youtube video`);
switch (song.source) {
case SOURCE.ytbvideo:
setExternalSearchText(`youtu.be/${song.bvid}`);
break;
case SOURCE.bilivideo:
setExternalSearchText(`bilibili.com/video/similarvideo/${song.bvid}`);
default:
logger.warn(
`[startRadio] ${song.bvid} deos not have a start radio handle.`
);
}
setSongMenuVisible(false);
};

const radioAvailable = (song?: NoxMedia.Song) =>
song?.source === SOURCE.ytbvideo || song?.id?.startsWith(CIDPREFIX);
[SOURCE.ytbvideo, SOURCE.bilivideo].includes(song?.source as SOURCE);

return { startRadio, radioAvailable };
};
Expand Down
5 changes: 5 additions & 0 deletions src/utils/BiliSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { logger } from './Logger';

import { SEARCH_OPTIONS } from '@enums/Storage';
import steriatkFetch from './mediafetch/steriatk';
import biliVideoSimilarFetch from './mediafetch/biliVideoSimilar';
import biliBangumiFetch from './mediafetch/biliBangumi';
import biliAudioAMFetch from './mediafetch/biliAudioAM';
import biliAudioColleFetch from './mediafetch/biliAudioColle';
Expand Down Expand Up @@ -133,6 +134,10 @@ interface ReExtraction<T> {
}

const reExtractions: ReExtraction<NoxNetwork.NoxRegexFetch>[] = [
{
match: biliVideoSimilarFetch.regexSearchMatch,
fetch: biliVideoSimilarFetch.regexFetch,
},
{
match: localFetch.regexSearchMatch,
fetch: localFetch.regexFetch,
Expand Down
53 changes: 53 additions & 0 deletions src/utils/mediafetch/biliVideoSimilar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { logger } from '../Logger';
import { regexFetchProps } from './generic';
import bfetch from '@utils/BiliFetch';
import { SOURCE, BiliMusicTid } from '@enums/MediaFetch';
import SongTS from '@objects/Song';
import { fetchBVID } from './bilivideo';

/**
* https://api.bilibili.com/x/web-interface/archive/related?bvid=BV1xx411c7m9
*/
const API =
'https://api.bilibili.com/x/web-interface/archive/related?bvid={sid}';

const fetchBiliVideoSimilarList = async (bvid: string) => {
logger.info('calling fetchBiliVideoSimilarList');
const res = await bfetch(API.replace('{sid}', bvid));
const json = await res.json();
return (await fetchBVID(bvid)).concat(
json.data
// limit similar videos to music only.
.filter((v: any) => BiliMusicTid.includes(v.tid))
.map((data: any) =>
SongTS({
cid: data.cid,
bvid: data.bvid,
name: data.title,
nameRaw: data.title,
singer: data.owner.name,
singerId: data.owner.mid,
cover: data.pic,
lyric: '',
page: 1,
duration: data.duration,
album: data.title,
source: SOURCE.bilivideo,
})
)
);
};

const regexFetch = async ({
reExtracted,
}: regexFetchProps): Promise<NoxNetwork.NoxRegexFetch> => ({
songList: await fetchBiliVideoSimilarList(reExtracted[1]!),
});

export default {
// https://www.bilibili.com/audio/similarsongs/3680653
regexSearchMatch: /bilibili\.com\/video\/similarvideo\/(BV[^/?]+)/,
regexFetch,
};

0 comments on commit 708b8e2

Please sign in to comment.