From 369e06e6d8ca12ccc9671b3fa37e847c8f725212 Mon Sep 17 00:00:00 2001 From: lovegaoshi <106490582+lovegaoshi@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:24:49 -0700 Subject: [PATCH 1/2] feat: biliFavColle --- src/utils/BiliSearch.ts | 5 +++ src/utils/mediafetch/biliFavColle.ts | 64 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/utils/mediafetch/biliFavColle.ts diff --git a/src/utils/BiliSearch.ts b/src/utils/BiliSearch.ts index a56819f96..91d0d97bc 100644 --- a/src/utils/BiliSearch.ts +++ b/src/utils/BiliSearch.ts @@ -25,6 +25,7 @@ import bilisubliveFetch from './mediafetch/bilisublive'; import localFetch from '@utils/mediafetch/local'; import b23tvFetch from './mediafetch/b23tv'; import headRequestFetch from './mediafetch/headRequest'; +import biliFavColleFetch from './mediafetch/biliFavColle'; import { regexFetchProps } from './mediafetch/generic'; import { MUSICFREE, searcher } from './mediafetch/musicfree'; import { getMusicFreePlugin } from '@utils/ChromeStorage'; @@ -141,6 +142,10 @@ interface ReExtraction { } const reExtractions: ReExtraction[] = [ + { + match: biliFavColleFetch.regexSearchMatch, + fetch: biliFavColleFetch.regexFetch, + }, { match: biliVideoSimilarFetch.regexSearchMatch, fetch: biliVideoSimilarFetch.regexFetch, diff --git a/src/utils/mediafetch/biliFavColle.ts b/src/utils/mediafetch/biliFavColle.ts new file mode 100644 index 000000000..c8c97d5d1 --- /dev/null +++ b/src/utils/mediafetch/biliFavColle.ts @@ -0,0 +1,64 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/** + * refactor: + * bilisearch workflow: + * reExtractSearch matches regex patterns and use the corresponding fetch functions; + * fetch function takes extracted and calls a dataProcess.js fetch function; + * dataprocess fetch function fetches VIDEOINFO using data.js fetch function, then parses into SONGS + * data.js fetch function fetches VIDEOINFO. + * steps to refactor: + * each site needs a fetch to parse regex extracted, a videoinfo fetcher and a song fetcher. + */ +import { logger } from '../Logger'; +import { regexFetchProps } from './generic'; +import { biliShazamOnSonglist } from './bilishazam'; +import { fetchBiliPaginatedAPI } from './paginatedbili'; + +const URL_BILICOLLE_INFO = + 'https://api.bilibili.com/x/space/fav/season/list?season_id={sid}&pn={pn}&ps=100'; + +const fetchBiliColleList = async ( + mid: string, + sid: string, + progressEmitter: (val: number) => void = () => undefined, + favList: string[] = [] +) => { + logger.info('calling fetchBiliColleList'); + + return await fetchBiliPaginatedAPI({ + url: URL_BILICOLLE_INFO.replace('{sid}', sid), + getMediaCount: (data: any) => data.info.media_count, + getPageSize: () => 100, + getItems: (js: any) => js.data.medias, + progressEmitter, + favList, + }); +}; + +const regexFetch = async ({ + reExtracted, + progressEmitter = () => undefined, + favList, + useBiliTag, +}: regexFetchProps): Promise => ({ + songList: await biliShazamOnSonglist( + await fetchBiliColleList( + reExtracted[1]!, + reExtracted[2]!, + progressEmitter, + favList + ), + false, + progressEmitter, + useBiliTag || false + ), +}); + +const resolveURL = () => undefined; + +export default { + regexSearchMatch: /.*bilibili\.com\/\d+\/favlist\?fid=(\d+)&ftype=collect/, + regexFetch, + regexResolveURLMatch: /^null-/, + resolveURL, +}; From 48a69558bc58b240323d7317997d1bdb01024bfc Mon Sep 17 00:00:00 2001 From: lovegaoshi <106490582+lovegaoshi@users.noreply.github.com> Date: Mon, 22 Apr 2024 21:34:36 -0700 Subject: [PATCH 2/2] feat: biliFavColle --- __tests__/mediafetch/biliFavColle.test.ts | 10 ++++++++++ src/utils/mediafetch/biliFavColle.ts | 8 +------- 2 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 __tests__/mediafetch/biliFavColle.test.ts diff --git a/__tests__/mediafetch/biliFavColle.test.ts b/__tests__/mediafetch/biliFavColle.test.ts new file mode 100644 index 000000000..c793c97cc --- /dev/null +++ b/__tests__/mediafetch/biliFavColle.test.ts @@ -0,0 +1,10 @@ +import fetcher from '../../src/utils/mediafetch/biliFavColle'; +test('FavColle', async () => { + const content = await fetcher.regexFetch({ + reExtracted: fetcher.regexSearchMatch.exec( + 'https://space.bilibili.com/529249/favlist?fid=1061551&ftype=collect&ctype=21' + )!, + }); + // console.log(content); + expect(content?.songList[0]?.id).not.toBeNull(); +}); diff --git a/src/utils/mediafetch/biliFavColle.ts b/src/utils/mediafetch/biliFavColle.ts index c8c97d5d1..c11e88371 100644 --- a/src/utils/mediafetch/biliFavColle.ts +++ b/src/utils/mediafetch/biliFavColle.ts @@ -18,7 +18,6 @@ const URL_BILICOLLE_INFO = 'https://api.bilibili.com/x/space/fav/season/list?season_id={sid}&pn={pn}&ps=100'; const fetchBiliColleList = async ( - mid: string, sid: string, progressEmitter: (val: number) => void = () => undefined, favList: string[] = [] @@ -42,12 +41,7 @@ const regexFetch = async ({ useBiliTag, }: regexFetchProps): Promise => ({ songList: await biliShazamOnSonglist( - await fetchBiliColleList( - reExtracted[1]!, - reExtracted[2]!, - progressEmitter, - favList - ), + await fetchBiliColleList(reExtracted[1]!, progressEmitter, favList), false, progressEmitter, useBiliTag || false