-
Notifications
You must be signed in to change notification settings - Fork 769
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "MusicFree", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"license": "GPL", | ||
"private": true, | ||
"author": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import PluginManager from '@/core/pluginManager'; | ||
import minDistance from './minDistance'; | ||
|
||
/** | ||
* | ||
* @param musicItem 音乐类型 | ||
* @param type 媒体类型 | ||
* @param abortFunction 如果函数为true,则中断 | ||
* @returns | ||
*/ | ||
export default async function <T extends ICommon.SupportMediaType>( | ||
musicItem: IMusic.IMusicItem, | ||
type: T = 'music' as T, | ||
abortFunction?: () => boolean, | ||
): Promise<ICommon.SupportMediaItemBase[T] | null> { | ||
const keyword = musicItem.alias || musicItem.title; | ||
const plugins = PluginManager.getSearchablePlugins(type); | ||
|
||
let distance = Infinity; | ||
let minDistanceMusicItem; | ||
let targetPlugin; | ||
|
||
const startTime = Date.now(); | ||
|
||
for (let plugin of plugins) { | ||
// 超时时间:10s | ||
if (abortFunction?.() || Date.now() - startTime > 8000) { | ||
break; | ||
} | ||
if (plugin.name === musicItem.platform) { | ||
continue; | ||
} | ||
const results = await plugin.methods | ||
.search(keyword, 1, type) | ||
.catch(() => null); | ||
|
||
// 取前两个 | ||
const firstTwo = results?.data?.slice(0, 2) || []; | ||
|
||
for (let item of firstTwo) { | ||
if (item.title === keyword && item.artist === musicItem.artist) { | ||
distance = 0; | ||
minDistanceMusicItem = item; | ||
targetPlugin = plugin; | ||
break; | ||
} else { | ||
const dist = | ||
minDistance(keyword, musicItem.title) + | ||
minDistance(item.artist, musicItem.artist); | ||
if (dist < distance) { | ||
distance = dist; | ||
minDistanceMusicItem = item; | ||
targetPlugin = plugin; | ||
} | ||
} | ||
} | ||
|
||
if (distance === 0) { | ||
break; | ||
} | ||
} | ||
if (minDistanceMusicItem && targetPlugin) { | ||
return minDistanceMusicItem as ICommon.SupportMediaItemBase[T]; | ||
} | ||
|
||
return null; | ||
} |