1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-12-23 06:43:17 +00:00

updated video search

This commit is contained in:
MatteZ02 2020-06-17 16:38:43 +03:00
parent 71250a8cf3
commit e0087d5b5f
6 changed files with 68 additions and 62 deletions

View File

@ -43,8 +43,8 @@
"soundcloud-api-client": "0.0.9", "soundcloud-api-client": "0.0.9",
"spotify-web-api-node": "^4.0.0", "spotify-web-api-node": "^4.0.0",
"utf-8-validate": "^5.0.2", "utf-8-validate": "^5.0.2",
"yt-search": "^1.1.2",
"ytdl-core": "^3.1.0", "ytdl-core": "^3.1.0",
"ytsr": "^0.1.15",
"zlib-sync": "^0.1.6" "zlib-sync": "^0.1.6"
} }
} }

View File

@ -1,7 +1,8 @@
const YouTube = require("simple-youtube-api"); const YouTube = require("simple-youtube-api");
const SpotifyApi = require("spotify-web-api-node"); const SpotifyApi = require("spotify-web-api-node");
const search = require("yt-search"); const ytdl = require("ytdl-core");
const ytdl = require("ytdl-core") const ytsr = require("ytsr");
const ms = require("ms");
module.exports = { module.exports = {
name: "play", name: "play",
@ -43,25 +44,18 @@ module.exports = {
if (!voiceChannel.speakable) if (!voiceChannel.speakable)
return msg.channel.send(client.messages.noPermsSpeak); return msg.channel.send(client.messages.noPermsSpeak);
if (ytdl.validateURL(url)) { if (ytdl.validateURL(url)) {
client.funcs.handleVideo({ client.funcs.handleVideo(url, msg, voiceChannel, client, false, "ytdl");
url: url
},
msg,
voiceChannel,
client,
false,
"ytdl"
);
} else if (url.match(/^https?:\/\/(open.spotify.com|spotify.com)(.*)$/)) { } else if (url.match(/^https?:\/\/(open.spotify.com|spotify.com)(.*)$/)) {
const playlistId = url.split("/playlist/")[1].split("?")[0]; const playlistId = url.split("/playlist/")[1].split("?")[0];
spotify.getPlaylist(playlistId).then( spotify.getPlaylist(playlistId).then(
async function (data) { async function (data) {
searchVideos(data, client, msg, voiceChannel) searchVideos(data, client, msg, voiceChannel);
}, },
function (err) { function (err) {
console.log(err); console.log(err);
msg.channel.send(client.messages.noResultsSpotify); msg.channel.send(client.messages.noResultsSpotify);
}); }
);
} else if ( } else if (
url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/) url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)
) { ) {
@ -71,7 +65,7 @@ module.exports = {
for (const video of Object.values(videos)) { for (const video of Object.values(videos)) {
const video2 = await youtube.getVideoByID(video.id); const video2 = await youtube.getVideoByID(video.id);
await client.funcs.handleVideo( await client.funcs.handleVideo(
video2, video2.url,
msg, msg,
voiceChannel, voiceChannel,
client, client,
@ -85,19 +79,24 @@ module.exports = {
); );
return lmsg.edit(message); return lmsg.edit(message);
} else { } else {
search(searchString, function (err, res) { ytsr(
if (err) return console.log(err); searchString, {
if (res.videos.length === 0) limit: 5,
return msg.channel.send(client.messages.noResults); },
function (err, res) {
if (err) console.log(err);
if (!res.items[0]) return msg.channel.send(client.messages.noResults);
const videoResults = res.items.filter(item => item.type === "video");
client.funcs.handleVideo( client.funcs.handleVideo(
res.videos[0], videoResults[0].link,
msg, msg,
voiceChannel, voiceChannel,
client, client,
false, false,
"ytdl" "ytdl"
); );
}); }
);
} }
}, },
}; };
@ -108,38 +107,45 @@ async function searchVideos(data, client, msg, voiceChannel) {
for (let i = 0; data.body.tracks.items.length > i; i++) { for (let i = 0; data.body.tracks.items.length > i; i++) {
const track = await data.body.tracks.items[i].track; const track = await data.body.tracks.items[i].track;
await client.funcs.sleep(250); await client.funcs.sleep(250);
search( ytsr(
`${track.artists[0].name} ${track.name} audio`, `${track.artists[0].name} ${track.name} audio`, {
limit: 5,
},
async function (err, res) { async function (err, res) {
if (err) return console.log(err); if (err) return console.log(err);
if (res.videos.length === 0) { if (!res.items[0]) {
search( ytsr(
`${track.artists[0].name} ${track.name} lyrics`, `${track.artists[0].name} ${track.name} lyrics`, {
limit: 5,
},
async function (err, res) { async function (err, res) {
if (err) return console.log(err); if (err) return console.log(err);
if (res.videos.length === 0) { if (!res.items[0]) {
search( ytsr(
`${track.artists[0].name} ${track.name}`, `${track.artists[0].name} ${track.name}`, {
limit: 5,
},
async function (err, res) { async function (err, res) {
if (err) return console.log(err); if (err) console.log(err);
if (res.videos.length === 0) { if (!res.items[0]) {
failed++; failed++;
return;
} }
await client.funcs.handleVideo( const videoResults = res.items.filter(item => item.type === "video");
res.videos[0], client.funcs.handleVideo(
videoResults[0].link,
msg, msg,
voiceChannel, voiceChannel,
client, client,
true, false,
"ytdl" "ytdl"
); );
} }
); );
return; return;
} }
const videoResults = res.items.filter(item => item.type === "video");
await client.funcs.handleVideo( await client.funcs.handleVideo(
res.videos[0], videoResults[0].link,
msg, msg,
voiceChannel, voiceChannel,
client, client,
@ -151,8 +157,9 @@ async function searchVideos(data, client, msg, voiceChannel) {
failed++; failed++;
return; return;
} }
const videoResults = res.items.filter(item => item.type === "video");
await client.funcs.handleVideo( await client.funcs.handleVideo(
res.videos[0], videoResults[0].link,
msg, msg,
voiceChannel, voiceChannel,
client, client,
@ -164,10 +171,7 @@ async function searchVideos(data, client, msg, voiceChannel) {
} }
let message; let message;
if (failed === 0) { if (failed === 0) {
message = client.messages.playlistAdded.replace( message = client.messages.playlistAdded.replace("%TITLE%", data.body.name);
"%TITLE%",
data.body.name
);
} else { } else {
message = `${client.messages.playlistAdded.replace( message = `${client.messages.playlistAdded.replace(
"%TITLE%", "%TITLE%",

View File

@ -1,4 +1,4 @@
const yts = require('yt-search'); const ytsr = require('ytsr');
const he = require('he'); const he = require('he');
module.exports = { module.exports = {
@ -22,10 +22,13 @@ module.exports = {
if (voiceChannel.full) return msg.channel.send(client.messages.channelFull); if (voiceChannel.full) return msg.channel.send(client.messages.channelFull);
if (!voiceChannel.joinable) return msg.channel.send(client.messages.noPermsConnect); if (!voiceChannel.joinable) return msg.channel.send(client.messages.noPermsConnect);
if (!voiceChannel.speakable) return msg.channel.send(client.messages.noPermsSpeak); if (!voiceChannel.speakable) return msg.channel.send(client.messages.noPermsSpeak);
yts(searchString, async function (err, res) { ytsr(searchString, {
limit: 20,
}, async function (err, res) {
if (err) return console.log(err); if (err) return console.log(err);
if (res.videos.length === 0) return msg.channel.send(client.messages.noResults); if (!res.items[0]) return msg.channel.send(client.messages.noResults);
const videos = res.videos.slice(0, 10); const videoResults = res.items.filter(item => item.type === "video");
const videos = videoResults.slice(0, 10);
let index = 0; let index = 0;
const embed = new Discord.MessageEmbed() const embed = new Discord.MessageEmbed()
.setTitle(client.messages.songSelection) .setTitle(client.messages.songSelection)
@ -44,7 +47,7 @@ module.exports = {
return msg.channel.send(client.messages.cancellingVideoSelection); return msg.channel.send(client.messages.cancellingVideoSelection);
} }
const videoIndex = parseInt(response.first().content) - 1; const videoIndex = parseInt(response.first().content) - 1;
return client.funcs.handleVideo(videos[videoIndex], msg, voiceChannel, client, false, "ytdl"); return client.funcs.handleVideo(videos[videoIndex].link, msg, voiceChannel, client, false, "ytdl");
}) })
} }
}; };

View File

@ -49,9 +49,8 @@ module.exports = {
if (songs.length !== 0) { if (songs.length !== 0) {
const lmsg = await msg.channel.send(client.messages.loadingSongs); const lmsg = await msg.channel.send(client.messages.loadingSongs);
for (let i = 0; i < songs.length; i++) { for (let i = 0; i < songs.length; i++) {
client.funcs.handleVideo({ client.funcs.handleVideo(
url: `https://www.youtube.com/watch?v=${songs[i].youtubeId}` `https://www.youtube.com/watch?v=${songs[i].youtubeId}`, msg, voiceChannel, client, true, "ytdl");
}, msg, voiceChannel, client, true, "ytdl");
} }
const message = client.messages.songsAdded.replace( const message = client.messages.songsAdded.replace(
"%AMOUNT%", "%AMOUNT%",

View File

@ -9,10 +9,10 @@ module.exports = async function (
playlist, playlist,
type type
) { ) {
const songInfo = await ytdl.getInfo(resource.url).catch(err => console.log(err)); const songInfo = await ytdl.getInfo(resource).catch(err => console.log(err));
const song = { const song = {
title: Discord.Util.escapeMarkdown(songInfo.videoDetails.title), title: Discord.Util.escapeMarkdown(songInfo.videoDetails.title),
url: resource.url, url: resource,
author: msg.author, author: msg.author,
type: type, type: type,
info: songInfo.videoDetails info: songInfo.videoDetails

View File

@ -5,7 +5,7 @@ module.exports = async function (client, msg, youtube, voiceChannel, url) {
const videos = await playlist.getVideos(); const videos = await playlist.getVideos();
for (const video of Object.values(videos)) { for (const video of Object.values(videos)) {
const video2 = await youtube.getVideoByID(video.id); const video2 = await youtube.getVideoByID(video.id);
await client.funcs.handleVideo(video2, msg, voiceChannel, client, true); await client.funcs.handleVideo(video2.url, msg, voiceChannel, client, true);
} }
let message; let message;
message = client.messages.playlistAdded.replace("%TITLE%", playlist.title); message = client.messages.playlistAdded.replace("%TITLE%", playlist.title);