generated from fahricansecer/boilerplate-be
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
const { Innertube } = require('youtubei.js');
|
|
|
|
async function test() {
|
|
const youtube = await Innertube.create({ lang: 'tr', location: 'TR' });
|
|
const videoId = 'ix8cLltPCCE';
|
|
console.log('Fetching comments for', videoId);
|
|
const commentThread = await youtube.getComments(videoId);
|
|
|
|
let comments = [];
|
|
let currentThread = commentThread;
|
|
let pages = 0;
|
|
|
|
while (currentThread) {
|
|
if (currentThread.contents) {
|
|
for (const thread of currentThread.contents) {
|
|
if (thread.comment?.content?.text) {
|
|
comments.push(thread.comment.content.text);
|
|
}
|
|
|
|
// Fetch replies if available
|
|
if (thread.has_replies) {
|
|
try {
|
|
const replies = await thread.getReplies();
|
|
if (replies) {
|
|
// Usually replies are in replies.contents or something similar
|
|
// We need to check how to extract replies
|
|
for (const reply of replies) {
|
|
if (reply.content?.text) {
|
|
comments.push(reply.content.text);
|
|
} else if (reply.comment?.content?.text) {
|
|
comments.push(reply.comment.content.text);
|
|
}
|
|
}
|
|
}
|
|
} catch(e) {
|
|
console.error('Error fetching replies', e.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (currentThread.has_continuation && pages < 50) {
|
|
pages++;
|
|
currentThread = await currentThread.getContinuation();
|
|
console.log('Page', pages, 'Total so far:', comments.length);
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
console.log('Total comments fetched:', comments.length);
|
|
}
|
|
|
|
test().catch(console.error);
|