Skip to content

Commit

Permalink
feat: mfsdk
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Dec 19, 2024
1 parent c02d1ac commit 52b65c8
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 311 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "MusicFreePlugins"]
path = MusicFreePlugins
url = https://github.com/maotoumao/MusicFreePlugins.git
1 change: 0 additions & 1 deletion MusicFreePlugins
Submodule MusicFreePlugins deleted from cef575
4 changes: 2 additions & 2 deletions src/components/playlist/BiliSearch/BiliSearchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useNoxSetting } from '@stores/useApp';
import usePlayback from '@hooks/usePlayback';
import useBiliSearch from '@hooks/useBiliSearch';
import SearchMenu from './SearchMenu';
import { getMusicFreePlugin } from '@utils/ChromeStorage';
import logger from '@utils/Logger';
import { getIcon } from './Icons';
import AutoComplete from '@components/commonui/AutoComplete';
Expand Down Expand Up @@ -44,6 +43,7 @@ export default ({
const playerSetting = useNoxSetting(state => state.playerSetting);
const searchOption = useNoxSetting(state => state.searchOption);
const searchProgress = useNoxSetting(state => state.searchBarProgress);
const mfsdks = useNoxSetting(state => state.MFsdks);
const navigationGlobal = useNavigation();
const externalSearchText = useNoxSetting(state => state.externalSearchText);
const setExternalSearchText = useNoxSetting(
Expand All @@ -66,7 +66,7 @@ export default ({
const pressed = useRef(false);

const handleMenuPress = (event: GestureResponderEvent) => {
getMusicFreePlugin().then(v => setShowMusicFree(v.length > 0));
setShowMusicFree(mfsdks.length > 0);
setDialogOpen(true);
setMenuCoords({
x: event.nativeEvent.pageX,
Expand Down
7 changes: 3 additions & 4 deletions src/components/playlist/BiliSearch/SearchMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useTranslation } from 'react-i18next';

import { SearchOptions } from '@enums/Storage';
import useAlert from '@components/dialogs/useAlert';
import { MUSICFREE } from '@utils/mediafetch/musicfree';
import Icons from './Icons';
import { useNoxSetting } from '@stores/useApp';
import { rgb2Hex } from '@utils/Utils';
Expand Down Expand Up @@ -33,7 +32,7 @@ export default ({
const { OneWayAlert } = useAlert();
const playerStyle = useNoxSetting(state => state.playerStyle);
const setSearchOption = useNoxSetting(state => state.setSearchOption);
const setDefaultSearch = (defaultSearch: SearchOptions | MUSICFREE) => {
const setDefaultSearch = (defaultSearch: SearchOptions) => {
toggleVisible();
setSearchOption(defaultSearch);
};
Expand Down Expand Up @@ -74,8 +73,8 @@ export default ({
{showMusicFree && (
<Menu.Item
leadingIcon={Icons.MUSICFREE}
onPress={() => setDefaultSearch(MUSICFREE.aggregated)}
title={`MusicFree.${MUSICFREE.aggregated}`}
onPress={() => setDefaultSearch(SearchOptions.MUSICFREE)}
title={`MusicFree.${SearchOptions.MUSICFREE}`}
/>
)}
{isAndroid && (
Expand Down
4 changes: 2 additions & 2 deletions src/components/setting/developer/plugins/musicfree/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ItemProps {
const RenderItem = ({ sdk, listRef }: ItemProps) => {
const playerStyle = useNoxSetting(state => state.playerStyle);
const rmMFsdks = useNoxSetting(state => state.rmMFsdks);
const addMFsdks = useNoxSetting(state => state.addMFsdks);
const replaceMFsdks = useNoxSetting(state => state.replaceMFsdks);

const deleteTheme = async () => {
rmMFsdks([sdk]);
Expand Down Expand Up @@ -51,7 +51,7 @@ const RenderItem = ({ sdk, listRef }: ItemProps) => {
<IconButton
icon="sync"
style={styles.deleteButton}
onPress={() => fetchMFsdk(sdk.srcUrl).then(addMFsdks)}
onPress={() => fetchMFsdk(sdk.srcUrl).then(replaceMFsdks)}
/>
<IconButton
icon="trash-can"
Expand Down
2 changes: 1 addition & 1 deletion src/enums/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export enum StorageKeys {
FADE_INTERVAL = 'fadeInterval',
COLORTHEME = 'ColorTheme',
REGEXTRACT_MAPPING = 'RegexExtract',
MUSICFREE_PLUGIN = 'MusicFreePlugin',
AA_PERMISSION = 'AndroidAutoPermission',
TANAKA_AMAZING_COMMODITIES = 'TanakaAmazingCommodities',
ALIST_CRED = 'AlistCred',
Expand All @@ -35,4 +34,5 @@ export enum SearchOptions {
YOUTUBE = 'youtube',
YOUTUBEM = 'yt music',
ALIST = 'alist',
MUSICFREE = 'musicfree',
}
3 changes: 1 addition & 2 deletions src/objects/Song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import he from 'he';
import { extractParenthesis } from '../utils/re';
import { reExtractSongName } from '@stores/regexStore';
import { Source } from '@enums/MediaFetch';
import { MUSICFREE } from '@utils/mediafetch/musicfree';
import { i0hdslbHTTPResolve } from '@utils/Utils';

export const DEFAULT_NULL_URL = 'NULL';
Expand All @@ -26,7 +25,7 @@ interface SongProps {
duration?: number;
album?: string;
addedDate?: number;
source?: Source | MUSICFREE;
source?: Source;
isLive?: boolean;
liveStatus?: boolean;
metadataOnLoad?: boolean;
Expand Down
5 changes: 2 additions & 3 deletions src/stores/useAPMUI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { StateCreator } from 'zustand';

import { IntentData } from '@enums/Intent';
import { MUSICFREE } from '@utils/mediafetch/musicfree';
import { SearchOptions } from '@enums/Storage';
import { saveDefaultSearch } from '@utils/ChromeStorage';

Expand All @@ -14,8 +13,8 @@ export interface APMUIStore {

intentData?: IntentData;
setIntentData: (val?: IntentData) => void;
searchOption: SearchOptions | MUSICFREE;
setSearchOption: (val: SearchOptions | MUSICFREE) => void;
searchOption: SearchOptions;
setSearchOption: (val: SearchOptions) => void;
gestureMode: boolean;
setGestureMode: (val: boolean) => void;

Expand Down
7 changes: 7 additions & 0 deletions src/stores/useMFsdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ export interface MFsdkStore {
setMFsdks: (mf: MFsdk[]) => void;
addMFsdks: (mf: MFsdk[]) => void;
rmMFsdks: (mf: MFsdk[]) => void;
replaceMFsdks: (mf: MFsdk[]) => void;
}

const store: StateCreator<MFsdkStore, [], [], MFsdkStore> = (set, get) => ({
MFsdks: [],
setMFsdks: mf => set({ MFsdks: mf }),
replaceMFsdks: mf => {
if (mf.length === 0) return;
set(s => ({
MFsdks: s.MFsdks.map(sdk => mf.find(v => v.srcUrl === sdk.srcUrl) ?? sdk),
}));
},
addMFsdks: mf => {
if (mf.length === 0) return;
addMFsdks(mf.map(v => v.path));
Expand Down
3 changes: 1 addition & 2 deletions src/types/media.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SortOptions, PlaylistTypes } from '@enums/Playlist';
import { Source } from '@enums/MediaFetch';
import { MUSICFREE } from '@utils/mediafetch/musicfree';
import { LrcSource } from '@enums/LyricFetch';

declare global {
namespace NoxMedia {
type SongSource = Source | MUSICFREE;
type SongSource = Source;

export interface Song {
id: string;
Expand Down
16 changes: 9 additions & 7 deletions src/utils/BiliSearch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import biliSearchFetch from '@utils/mediafetch/bilisearch';
import ytbVideoFetch from '@utils/mediafetch/ytbvideo';
import localFetch from '@utils/mediafetch/local';
import { MUSICFREE, searcher } from '@utils/mediafetch/musicfree';
import { getMusicFreePlugin } from '@utils/ChromeStorage';
import { SearchOptions } from '@enums/Storage';
import steriatkFetch from './mediafetch/steriatk';
import biliVideoSimilarFetch from './mediafetch/biliVideoSimilar';
Expand Down Expand Up @@ -35,6 +33,7 @@ import biliFavColleFetch from './mediafetch/biliFavColle';
import alistFetch from './mediafetch/alist';
import acfunFetch from './mediafetch/acfunvideo';
import { logger } from './Logger';
import { useNoxSetting } from '@stores/useApp';

/**
* assign the proper extractor based on the provided url. uses regex.
Expand All @@ -47,7 +46,7 @@ interface Props {
useBiliTag?: boolean;
fastSearch?: boolean;
cookiedSearch?: boolean;
defaultSearch?: SearchOptions | MUSICFREE;
defaultSearch?: SearchOptions;
genericSearch?: boolean;
}

Expand Down Expand Up @@ -129,12 +128,15 @@ export const searchBiliURLs = async ({
case SearchOptions.YOUTUBEM:
results = { songList: await fetchInnerTuneSearch(input) };
break;
case MUSICFREE.aggregated:
results.songList = await searcher[MUSICFREE.aggregated](
input,
await getMusicFreePlugin(),
case SearchOptions.MUSICFREE: {
const songLists = await Promise.all(
useNoxSetting
.getState()
.MFsdks.map(sdk => sdk.regexFetch({ url: input })),
);
results.songList = songLists.map(v => v.songList).flat();
break;
}
default:
results = await biliSearchFetch.regexFetch({
url: input,
Expand Down
9 changes: 1 addition & 8 deletions src/utils/ChromeStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,9 @@ import { NoxRepeatMode } from '@enums/RepeatMode';
import { PlaylistTypes } from '@enums/Playlist';
import { StorageKeys, SearchOptions } from '@enums/Storage';
import { DefaultSetting, OverrideSetting } from '@objects/Storage';
import { MUSICFREE } from '@utils/mediafetch/musicfree';
import { getAlistCred } from './alist/storage';
import { timeFunction } from './Utils';

export const setMusicFreePlugin = (val: MUSICFREE[]) =>
saveItem(StorageKeys.MUSICFREE_PLUGIN, val);

export const getMusicFreePlugin = (): Promise<MUSICFREE[]> =>
getItem(StorageKeys.MUSICFREE_PLUGIN, []);

export const getFadeInterval = async () =>
Number(await getItem(StorageKeys.FADE_INTERVAL)) || 0;

Expand Down Expand Up @@ -60,7 +53,7 @@ export const saveABMapping = async (val: NoxStorage.ABDict) =>
export const getDefaultSearch = (): Promise<SearchOptions> =>
getItem(StorageKeys.DEFAULT_SEARCH, SearchOptions.BILIBILI);

export const saveDefaultSearch = (val: SearchOptions | MUSICFREE) =>
export const saveDefaultSearch = (val: SearchOptions) =>
saveItem(StorageKeys.DEFAULT_SEARCH, val);

export const getCachedMediaMapping = () =>
Expand Down
32 changes: 19 additions & 13 deletions src/utils/mediafetch/evalsdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as cheerio from 'cheerio';
import CookieManager from '@react-native-cookies/cookies';
import he from 'he';
import { URL } from 'react-native-url-polyfill';
import logger from '../Logger';

const Qualities = ['super', 'high', 'standard', 'low'];

Expand All @@ -17,9 +18,7 @@ export interface MFsdk {
author: string;
srcUrl: string;
supportedSearchType: string[];
regexFetch: (
v: NoxNetwork.BiliSearchFetchProps,
) => Promise<NoxNetwork.NoxRegexFetch>;
regexFetch: (v: { url: string }) => Promise<NoxNetwork.NoxRegexFetch>;
resolveURL: (v: NoxMedia.Song) => Promise<NoxNetwork.ParsedNoxMediaURL>;
/*
search: [AsyncFunction: search],
Expand All @@ -46,25 +45,29 @@ const IMusicToNoxMedia = (val: IMusic.IMusicItem, source: string) => {
singer: val.artist,
singerId: val.id,
cover:
val.artwork ||
val.artwork ??
'https://i2.hdslb.com/bfs/face/b70f6e62e4582d4fa5d48d86047e64eb57d7504e.jpg',
lyric: val.lrc,
parsedName: val.title,
source,
duration: val.duration | 0,
duration: val.duration ?? 0,
} as NoxMedia.Song;
};

const searchWrapper =
(search: (s: string, p: number, t: string) => any, sdk: MFsdk) =>
async (
v: NoxNetwork.BiliSearchFetchProps,
): Promise<NoxNetwork.NoxRegexFetch> => {
const results = await search(v.url, 1, 'music');
const songList = results.data.map((iMusic: any) =>
IMusicToNoxMedia(iMusic, sdk.platform),
);
return { songList };
async (v: { url: string }): Promise<NoxNetwork.NoxRegexFetch> => {
try {
logger.debug(`[mfsdk][${sdk.platform}] searching ${v.url}`);
const results = await search(v.url, 1, 'music');
const songList = results.data.map((iMusic: any) =>
IMusicToNoxMedia(iMusic, sdk.platform),
);
return { songList };
} catch {
logger.debug(`[mfsdk][${sdk.platform}] failed to search.`);
return { songList: [] };
}
};

const resolveURLWrapper =
Expand All @@ -76,6 +79,9 @@ const resolveURLWrapper =
for (const quality of qualities) {
const res = await resolveURL(v, quality);
if (res) {
logger.debug(
`[mfsdk][${sdk.platform}] resolved ${v.name} with quality ${quality}`,
);
return res;
}
}
Expand Down
44 changes: 0 additions & 44 deletions src/utils/mediafetch/mfsdk.ts

This file was deleted.

Loading

0 comments on commit 52b65c8

Please sign in to comment.