Skip to content

Commit

Permalink
feat: support custom playback source
Browse files Browse the repository at this point in the history
  • Loading branch information
Linkontoask committed Dec 31, 2020
1 parent 260e730 commit a6b5949
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
16 changes: 4 additions & 12 deletions packages/api/module/song_url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@ const match = require('@radishes/unblock')
const crypto = require('crypto')
const { cookieToJson } = require('../util/index')

const find = (id) => {
return match(id, [
'qq',
'xiami',
'baidu',
'kugou',
'kuwo',
'migu',
'joox',
'youtube',
])
const find = (id, source) => {
const playSource = source.split(',')
return match(id, playSource)
.then((url) => {
return url.url
})
Expand Down Expand Up @@ -52,7 +44,7 @@ module.exports = (query, request) => {
let i = 0
while (i < body.data.length) {
if (!body.data[i].url || body.data[i].freeTrialInfo) {
const url = await find(body.data[i].id)
const url = await find(body.data[i].id, query.source)
v.body.data[i].url = url
}
i++
Expand Down
16 changes: 13 additions & 3 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { get } from '@/utils/http'
import { SongsDetail, PlayList, Albums, Song } from '@/interface/index'
import {
SongsDetail,
PlayList,
Albums,
Song,
PlaySource
} from '@/interface/index'
import { SubscribeActionType } from '@/shared/subscribe'

export const getSongUrl = async <T>(id: number | number[]): Promise<T> => {
export const getSongUrl = async <T>(
id: number | number[],
source?: PlaySource[]
): Promise<T> => {
const data = await get<{ data: T }>('/api/song/url', {
id: Array.isArray(id) ? id.join(',') : id,
br: 9.99e5
br: 9.99e5,
source: source?.join(',')
})
return data.data
}
Expand Down
5 changes: 2 additions & 3 deletions src/components-business/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {
import {
SongsDetail,
ListFormat,
SongsBase,
FooterMutations,
DownloadActions,
SongListColumnsType
} from '@/interface/index'
import { useDownloadModule, useFooterModule } from '@/modules/index'
import { useSubscribe } from '@/shared/subscribe'
import { getMusicUrl } from '@/shared/music-shared'
import { instance } from '@/components-business/fly/index'
import { getSongUrl } from '@/api/index'
import remove from 'lodash/remove'
import './index.less'

Expand Down Expand Up @@ -73,7 +72,7 @@ const columns = [
end: end,
duartion: 0.8
})
const data = await getSongUrl<SongsBase[]>(text.id)
const data = await getMusicUrl(text.id)
if (data.length) {
const music = {
...text,
Expand Down
10 changes: 10 additions & 0 deletions src/interface/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ export interface HttpGet {
export interface HttpPost {
[key: string]: string
}

export type PlaySource =
| 'qq'
| 'xiami'
| 'baidu'
| 'kugou'
| 'kuwo'
| 'migu'
| 'joox'
| 'youtube'
5 changes: 2 additions & 3 deletions src/pages/download/sage.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { ActionTree, MutationTree } from 'vuex'
import {
SongsDetail,
SongsBase,
DownloadState,
Downloaded,
DownloadActions,
DownloadMutations
} from '@/interface'
import { RootState } from '@/store/index'
import { getSongUrl } from '@/api/index'
import { getMusicUrl } from '@/shared/music-shared'
import { download } from '@/utils/index'
import { importIpc } from '@/electron/event/ipc-browser'
import { DownloadIpcType } from '@/electron/event/action-types'

export const actions: ActionTree<DownloadState, RootState> = {
async [DownloadActions.DOWNLOAD_MUSIC]({ commit }, song: SongsDetail) {
const url = await getSongUrl<SongsBase[]>(song.id)
const url = await getMusicUrl(song.id)
song.size = url[0].size
commit(DownloadMutations.SET_DOWNLOAD_MUSIC, song)
// TODO ws protocol to be supported, download progress to be discussed
Expand Down
7 changes: 4 additions & 3 deletions src/pages/footer/sage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { toRaw } from 'vue'
import { ActionTree, MutationTree, GetterTree } from 'vuex'
import { isNumber, timeTos, toFixed, toArrayBuffer } from '@/utils/index'
import { getSongUrl, getSongDetail, getLyric } from './api/index'
import { getSongDetail, getLyric } from './api/index'
import { FooterState, FooterActions, FooterMutations } from './interface'
import { getMusicUrl } from '@/shared/music-shared'
import { RootState } from '@/store/index'
import { SongsDetail, SongsBase } from '@/interface'
import { SongsDetail } from '@/interface'
import cloneDeep from 'lodash/cloneDeep'
import remove from 'lodash/remove'

Expand Down Expand Up @@ -121,7 +122,7 @@ export const actions: ActionTree<FooterState, RootState> = {
) {
let id, url
if (typeof payload === 'number') {
const data = await getSongUrl<SongsBase[]>(payload)
const data = await getMusicUrl(payload)
id = payload
url = data[0].url
} else {
Expand Down
9 changes: 8 additions & 1 deletion src/shared/music-shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { FooterMutations, FooterActions } from '@/interface'
import { FooterMutations, FooterActions, SongsBase } from '@/interface'
import { useFooterModule } from '@/modules'
import { getSongUrl } from '@/api/index'
import store from '@/store/index'

export const playMusic = () => {
const store = useFooterModule()
Expand All @@ -21,3 +23,8 @@ export const playMusic = () => {
store.useMutations(FooterMutations.PLAY_MUSIC)
}
}

export const getMusicUrl = (id: number | number[]) => {
const source = store.state.playSource
return getSongUrl<SongsBase[]>(id, source)
}
8 changes: 6 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createStore, MutationTree, createLogger } from 'vuex'
import { FooterMutations } from '@/interface'
import { AllMutations } from '@/interface/index'
import { AllMutations, PlaySource } from '@/interface/index'
import { getNodeEnv } from '@/utils/index'
import createPersistedState from 'vuex-persistedstate'
import modules from '@/modules/index'
Expand All @@ -25,6 +25,7 @@ export interface RootState {
[x: string]: any
historyRoute: HistoryRoute
percentage: number
playSource: PlaySource[]
}

const state: RootState = {
Expand All @@ -34,7 +35,8 @@ const state: RootState = {
before: [],
after: []
},
percentage: 0
percentage: 0,
playSource: ['qq', 'kuwo', 'migu']
}

const mutations: MutationTree<RootState> = {
Expand Down Expand Up @@ -78,6 +80,8 @@ plugins.push(
createPersistedState({
key: 'RADISHES-VUEX',
paths: [
'historyRoute',
'playSource',
'Auth.user',
'Header.themeColor',
'Layout',
Expand Down

0 comments on commit a6b5949

Please sign in to comment.