2024-03-30 10:11:49 +00:00
|
|
|
const { Innertube } = require('youtubei.js');
|
2023-03-03 16:44:40 +00:00
|
|
|
const fetch = require('node-fetch')
|
2024-03-30 12:23:05 +00:00
|
|
|
|
2023-08-18 19:31:40 +00:00
|
|
|
const maxRetries = 5
|
2024-04-04 18:00:08 +00:00
|
|
|
const platforms = ['ANDROID', 'iOS', 'WEB']
|
2023-03-03 16:44:40 +00:00
|
|
|
|
2023-08-04 09:25:18 +00:00
|
|
|
async function getPipedInstance() {
|
2023-10-19 16:13:32 +00:00
|
|
|
const instances = await (await fetch('https://piped-instances.kavin.rocks/', {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
|
2024-03-30 19:05:11 +00:00
|
|
|
}
|
2023-10-19 16:13:32 +00:00
|
|
|
})).json()
|
2023-08-04 09:25:18 +00:00
|
|
|
return (instances[Math.floor(Math.random() * instances.length)]).api_url
|
2023-06-22 11:15:02 +00:00
|
|
|
}
|
2023-03-24 15:07:31 +00:00
|
|
|
|
2023-06-22 11:15:02 +00:00
|
|
|
async function getVideoMetadata(id) {
|
2024-03-30 12:23:05 +00:00
|
|
|
let error = ''
|
|
|
|
|
2023-08-18 19:31:40 +00:00
|
|
|
for (let retries = 0; retries < maxRetries; retries++) {
|
|
|
|
try {
|
2024-03-30 12:23:05 +00:00
|
|
|
const platform = platforms[retries % platforms.length];
|
2024-03-30 10:11:49 +00:00
|
|
|
const yt = await Innertube.create();
|
2024-03-30 12:23:05 +00:00
|
|
|
const info = await yt.getInfo(id, platform);
|
2024-03-30 10:11:49 +00:00
|
|
|
|
2024-03-30 12:23:05 +00:00
|
|
|
if (!info) {
|
|
|
|
error = 'ErrorCantConnectToServiceAPI'
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (info.playability_status.status !== 'OK') {
|
|
|
|
error = 'ErrorYTUnavailable'
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (info.basic_info.is_live) {
|
|
|
|
error = 'ErrorLiveVideo'
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-30 10:11:49 +00:00
|
|
|
return info
|
2023-08-18 19:31:40 +00:00
|
|
|
} catch (error) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 12:23:05 +00:00
|
|
|
return { error: error || 'ErrorUnknown' }
|
2023-03-23 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 11:15:02 +00:00
|
|
|
async function getChannel(id) {
|
2024-03-30 12:23:05 +00:00
|
|
|
let error = ''
|
|
|
|
|
2023-08-18 19:31:40 +00:00
|
|
|
for (let retries = 0; retries < maxRetries; retries++) {
|
|
|
|
try {
|
2024-03-30 12:23:05 +00:00
|
|
|
const platform = platforms[retries % platforms.length];
|
2024-03-30 10:11:49 +00:00
|
|
|
const yt = await Innertube.create();
|
2024-03-30 12:23:05 +00:00
|
|
|
const info = await yt.getChannel(id, platform);
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
error = 'ErrorCantConnectToServiceAPI'
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-30 10:11:49 +00:00
|
|
|
return info
|
2023-08-18 19:31:40 +00:00
|
|
|
} catch (error) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 12:23:05 +00:00
|
|
|
return { error: error || 'ErrorUnknown' }
|
2023-06-22 11:15:02 +00:00
|
|
|
}
|
2023-03-24 14:20:06 +00:00
|
|
|
|
2023-06-22 11:15:02 +00:00
|
|
|
async function getChannelVideos(id) {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
2023-03-24 14:20:06 +00:00
|
|
|
try {
|
2024-03-30 19:05:11 +00:00
|
|
|
const videos = [];
|
|
|
|
const yt = await Innertube.create();
|
|
|
|
const channel = await yt.getChannel(id);
|
|
|
|
let json = await channel.getVideos();
|
|
|
|
|
|
|
|
videos.push(...json.videos);
|
|
|
|
|
|
|
|
while (json.has_continuation && videos.length < 60) {
|
|
|
|
json = await getNextPage(json);
|
|
|
|
videos.push(...json.videos);
|
2023-06-22 11:15:02 +00:00
|
|
|
}
|
2024-03-30 19:05:11 +00:00
|
|
|
|
|
|
|
resolve(videos);
|
|
|
|
|
2023-03-24 14:20:06 +00:00
|
|
|
} catch (e) {
|
2024-03-30 19:05:11 +00:00
|
|
|
resolve(false);
|
2023-03-24 14:20:06 +00:00
|
|
|
}
|
2024-03-30 19:05:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
async function getNextPage(json) {
|
|
|
|
const page = await json.getContinuation();
|
|
|
|
return page;
|
|
|
|
}
|
2023-06-22 11:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getPlaylistVideos(id) {
|
2023-08-04 09:25:18 +00:00
|
|
|
const instance = await getPipedInstance()
|
2023-10-19 16:13:32 +00:00
|
|
|
const json = await (await fetch(`${instance}/playlists/${id}`, {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'Mozilla/5.0 (compatible; PreserveTube/0.0; +https://preservetube.com)'
|
|
|
|
}
|
|
|
|
})).json()
|
2024-03-30 15:51:13 +00:00
|
|
|
return json
|
2023-03-03 16:44:40 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 17:48:59 +00:00
|
|
|
async function getVideoDownload(url, quality) {
|
2024-03-30 13:32:37 +00:00
|
|
|
let json
|
|
|
|
|
|
|
|
for (let retries = 0; retries < maxRetries; retries++) {
|
|
|
|
try {
|
2024-03-30 19:23:32 +00:00
|
|
|
json = await (await fetch('http://cobalt-api:9000/api/json', {
|
2024-03-30 13:32:37 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
'url': url,
|
|
|
|
'vQuality': quality
|
|
|
|
})
|
|
|
|
})).json()
|
|
|
|
|
|
|
|
if (json.error) continue
|
|
|
|
return json
|
|
|
|
} catch (error) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 17:48:59 +00:00
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
2024-03-30 19:05:11 +00:00
|
|
|
module.exports = { getVideoMetadata, getChannel, getChannelVideos, getPlaylistVideos, getVideoDownload }
|