Add stop functionality, fix duration

This commit is contained in:
Azaaxin
2023-06-25 11:08:14 +02:00
committed by Myx
parent dddbe81fab
commit c802be1a61
17 changed files with 304 additions and 226 deletions

View File

@@ -1,5 +1,5 @@
const ytsr = require('ytsr');
const playdl = require('play-dl');
const ytsr = require("ytsr");
const playdl = require("play-dl");
async function getStream(query) {
try {
@@ -7,29 +7,48 @@ async function getStream(query) {
const match = query.match(regex);
let videoId;
let usingYtsr = false;
if(match == null) {
let result = await playdl.search(query, { limit: 1});
if (match == null) {
let result = await playdl.search(query, { limit: 1 });
videoId = result[0].id;
if (videoId == null) {
usingYtsr = true;
const searchResults = await ytsr(query, { page: 1, type: 'video' });
const searchResults = await ytsr(query, {
page: 1,
type: "video",
});
videoId = searchResults.items[0].id;
}
} else {
videoId = match[1];
}
const streamResult = await playdl.stream(`https://www.youtube.com/watch?v=${videoId}`, { quality: 2 });
const infoResult = usingYtsr ? await ytsr(`https://www.youtube.com/watch?v=${videoId}`, { limit: 1}) : await playdl.video_info(`https://www.youtube.com/watch?v=${videoId}`);
console.log(infoResult)
console.log("\x1b[36m",' Id: ', videoId, 'Alternative search:', usingYtsr)
const streamResult = await playdl.stream(
`https://www.youtube.com/watch?v=${videoId}`,
{ quality: 2 }
);
const infoResult = usingYtsr
? await ytsr(`https://www.youtube.com/watch?v=${videoId}`, {
limit: 1,
})
: await playdl.video_info(
`https://www.youtube.com/watch?v=${videoId}`
);
console.log("\x1b[36m"," Id: ", videoId, "Alternative search:", usingYtsr);
return {
title: (usingYtsr ? infoResult.items[0].title : infoResult.video_details.title) ?? 'Unknown, error fetching title.',
duration: (usingYtsr ? infoResult.items[0].duration : infoResult.video_details.durationInSec) ?? 'Unknown, error fetching duration.',
title:
(usingYtsr
? infoResult.items[0].title
: infoResult.video_details.title) ??
0,
duration:
(usingYtsr
? infoResult.items[0].duration
: (infoResult.video_details.durationInSec * 1000)) ??
0,
stream: streamResult.stream,
type: streamResult.type,
userInput: query
userInput: query,
};
} catch (error) {
console.log("\x1b[31m", error);
@@ -37,4 +56,4 @@ async function getStream(query) {
}
}
module.exports.getStream = getStream;
module.exports.getStream = getStream;