-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
117 lines (105 loc) · 3.82 KB
/
worker.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const yts = require('yt-search');
const events = require('events');
class YTUtils {
constructor() {
this.events = new events.EventEmitter();
return this;
}
on(event, callback) {
this.events.on(event, callback);
}
once(event, callback) {
this.events.once(event, callback);
}
error(data) {
this.events.emit("error", data);
}
async getVideoData(query) {
let playlist = this.playlistParser(query);
if (playlist) {
this.events.emit("message", { content: "Loading Playlist items..." });
var videos;
try {
videos = await this.fetchPlaylist(playlist);
} catch(e) {
this.error(e);
this.events.emit("message", { content: "Failed to load playlist. Maybe it's unviewable?" });
return false;
}
if (videos) {
this.events.emit("message", { content: `Successfully added playlist with ${videos.length} videos to the queue.` });
} else {
this.events.emit("message", { content: `**There was an error fetching the playlist '${playlist}'!**` });
return false;
}
return { type: "list", data: videos };
} else {
let parsed = this.youtubeParser(query);
if (!parsed) {
this.events.emit("message", { content: "Searching..." });
let video = await this.search(query);
if (video) {
this.events.emit("message", { content: `Successfully added [${video.title}](https://remix.fairuse.org/) to the queue.` });
} else {
this.events.emit("message", { content: `**There was an error loading a YouTube video using the query '${query}'!**` });
}
if (!video) return false;
return { type: "video", data: video };
} else {
this.events.emit("message", { content: "Loading video data..." });
let video = await this.search(parsed, true);
if (video) {
this.events.emit("message", { content: `Successfully added [${video.title}](https://remix.fairuse.org/) to the queue.` });
} else {
this.events.emit("message", { content: `**There was an error loading the YouTube video with the id '${parsed}'!**` });
}
if (!video) return false;
return { type: "video", data: video };
}
}
}
async search(string, id) {
return (id) ? await yts({ videoId: id }) : (await yts(string)).videos[0];
}
async fetchPlaylist(id) {
return (await yts({ listId: id })).videos;
}
youtubeParser(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
playlistParser(url) {
var match = url.match(/[&?]list=([^&]+)/i);
return (match || [0, false])[1];
}
}
const { workerData, parentPort } = require('worker_threads');
const utils = new YTUtils();
utils.events.on("message", (data) => {
parentPort.postMessage(JSON.stringify(data));
});
utils.events.on("error", (data) => {
console.error("worker error: " + data);
});
if (workerData) {
if (workerData.type == "command") {
utils.getVideoData(workerData.query).then((videoData) => {
parentPort.postMessage(JSON.stringify(videoData));
}).catch((e) => {
console.log("command error: ", e);
parentPort.postMessage(JSON.stringify({ content: "There was an error processing the command." }));
});
} else if (workerData.type == "voiceCommand") {
utils.search(workerData.query, false).then((videoData) => {
if (videoData) {
parentPort.postMessage({ type: "video", data: JSON.stringify(videoData) });
} else {
parentPort.postMessage("false");
}
}).catch(error => {
console.log("voice command error: ", error);
parentPort.postMessage(JSON.stringify({ content: "There was an error processing the voice command." }));
});
}
}