-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.js
63 lines (57 loc) · 1.68 KB
/
music.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @file This module is the graphql resolvers, handle music related query/mutation
*
*/
const ytdl = require('ytdl-core');
const ytsr = require('ytsr');
// Stream server variable
const STREAM_SERVER = process.env.STREAM_SERVER || 'http://localhost:7777';
async function getMusicInfo(parent, { url }) {
if (ytdl.validateURL(url)) {
const info = await ytdl.getBasicInfo(url);
const thumb = info.videoDetails.thumbnail.thumbnails
const result = {
src: `${STREAM_SERVER}/stream?id=${info.videoDetails.videoId}`,
title: info.videoDetails.title,
author: info.videoDetails.author.name,
thumbnail: thumb[thumb.length - 1].url
}
return result;
} else {
return {
src: null,
title: '',
author: '',
thumbnail: ''
};
}
}
async function searchMusic(parent, { keyword }) {
ytsr.do_warn_deprecate = false;
try {
const filter = await ytsr.getFilters(keyword);
// get video only filter
const videoOnly = filter.get('Type').find(o => o.name === 'Video');
// limit search result to 12
const option = {
limit: 12,
nextpageRef: videoOnly.ref,
};
try {
const result = await ytsr(null, option);
return result.items.map((item) => {
return {
src: item.link.replace('https://www.youtube.com/watch?v=', `${STREAM_SERVER}/stream?id=`),
title: item.title,
author: item.author.name,
thumbnail: item.thumbnail
}
});
} catch (error) {
throw new Error(`Search error for keyword: ${keyword}`);
}
} catch (error) {
throw new Error(`Search error for keyword: ${keyword}`);
}
}
module.exports = { getMusicInfo, searchMusic };