koel/resources/assets/js/services/youtube.js

36 lines
822 B
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import { http } from '.'
import { event } from '../utils'
import router from '../router'
2016-07-30 15:32:17 +00:00
export const youtube = {
/**
* Search for YouTube videos related to a song.
*
* @param {Object} song
*/
2016-12-20 15:44:47 +00:00
searchVideosRelatedToSong (song) {
2016-07-30 15:32:17 +00:00
if (!song.youtube) {
2016-11-26 03:25:35 +00:00
song.youtube = {}
2016-07-30 15:32:17 +00:00
}
2016-11-26 03:25:35 +00:00
const pageToken = song.youtube.nextPageToken || ''
2016-12-20 15:44:47 +00:00
return new Promise((resolve, reject) => {
2017-01-14 13:09:38 +00:00
http.get(`youtube/search/song/${song.id}?pageToken=${pageToken}`, ({ data }) => {
song.youtube.nextPageToken = data.nextPageToken
song.youtube.items.push(...data.items)
2016-12-20 15:44:47 +00:00
resolve()
}, error => reject(error))
2016-11-26 03:25:35 +00:00
})
2016-07-30 15:32:17 +00:00
},
/**
* Play a YouTube video.
*
* @param {string} id The video ID
*/
2016-11-26 03:25:35 +00:00
play (id) {
event.emit('youtube:play', id)
router.go('youtube')
}
}