Skip to content

Commit

Permalink
feat: multiple artists matching
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Jan 19, 2024
1 parent 4e20773 commit 7af0829
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
19 changes: 7 additions & 12 deletions src/renderer/components/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { findLastIndex } from 'lodash-es'
import { difference, findLastIndex } from 'lodash-es'
import { LucideCloud, LucideCloudOff, LucideMaximize, LucideMinimize, LucideMonitor, LucideMonitorOff, LucideMonitorPause, LucideMonitorPlay, LucideMoon, LucideMove, LucidePause, LucidePin, LucidePinOff, LucidePlay, LucideSun, LucideX } from 'lucide-vue-next'
import type { CSSProperties } from 'vue'
import { nextTick, watchEffect } from 'vue'
Expand Down Expand Up @@ -258,7 +258,7 @@ async function load(query: string, properties?: MusicInfo) {
const song = songs.find(item => {
const transformed = service.transform(item)
if (properties?.album && transformed.album !== properties.album) return false
if (properties?.artist && transformed.artist !== properties.artist) return false
if (properties?.artists && difference(properties.artists, transformed.artists ?? []).length) return false
return true
}) ?? songs[0]
if (!song) return
Expand Down Expand Up @@ -317,16 +317,11 @@ watchEffect(onInvalidate => {
const timer = setInterval(async () => {
const result = await getConnectedData()
if (result) {
isPlaying = result[0] === 'playing'
currentTime = result[1]
if (!connectedInfo || connectedInfo.key !== result[2] || connectedInfo.name !== result[3]) {
keyword = result[3]
connectedInfo = {
key: result[2],
name: result[3],
artist: result[4],
album: result[5],
}
isPlaying = result.isPlaying
currentTime = result.currentTime
if (!connectedInfo || connectedInfo.key !== result.info.key || connectedInfo.name !== result.info.name) {
connectedInfo = result.info
keyword = result.info.name
load(keyword, connectedInfo)
}
} else {
Expand Down
25 changes: 22 additions & 3 deletions src/renderer/utils/connection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { MusicInfo } from '../vendors/types'

export function checkConnectable() {
return worldBridge.platform === 'darwin'
}
Expand All @@ -10,7 +12,13 @@ export function pauseConnected() {
worldBridge.applescript('if application "Music" is running then tell application "Music" to pause')
}

export type ConnectedData = [
export interface ConnectedData {
isPlaying: boolean,
currentTime: number,
info: MusicInfo,
}

export type AppleMusicData = [
playerState: 'playing' | 'paused',
playerPosition: number,
trackId: number,
Expand All @@ -19,6 +27,17 @@ export type ConnectedData = [
trackAlbum: string,
]

export function getConnectedData() {
return worldBridge.applescript<ConnectedData | undefined>('if application "Music" is running then tell application "Music" to get player state & (get player position) & (get {id, name, artist, album} of current track)')
export async function getConnectedData(): Promise<ConnectedData | undefined> {
const result = await worldBridge.applescript<AppleMusicData | undefined>('if application "Music" is running then tell application "Music" to get player state & (get player position) & (get {id, name, artist, album} of current track)')
if (!result) return undefined
return {
isPlaying: result[0] === 'playing',
currentTime: result[1],
info: {
key: result[2],
name: result[3],
artists: result[4].split(' & '),
album: result[5],
},
}
}
2 changes: 1 addition & 1 deletion src/renderer/vendors/kugou.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default defineMusicService<{
return {
key: song.hash,
name: song.songname,
artist: song.singername,
artists: song.singername.split('、'),
album: song.group?.[0]?.album_name,
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/vendors/netease.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineMusicService<{
return {
key: song.id,
name: song.name,
artist: song.artists?.[0]?.name,
artists: song.artists?.map(artist => artist.name),
album: song.album?.name,
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/vendors/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface MusicInfo {
key: any,
name: string,
artist?: string,
artists?: string[],
album?: string,
}

Expand Down

0 comments on commit 7af0829

Please sign in to comment.