-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytService.js
37 lines (31 loc) · 997 Bytes
/
ytService.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
import { google } from 'googleapis';
import dotenv from 'dotenv';
dotenv.config();
const youtube = google.youtube({
version: 'v3',
auth: process.env.YOUTUBE_API_KEY,
});
const fetchCommentsFromYT = async (videoId) => {
try {
const response = await youtube.commentThreads.list({
part: ['snippet'],
videoId: videoId,
maxResults: 20,
});
if (!response.data.items) {
throw new Error('No comments found');
}
const comments = response.data.items.map(item => ({
author: item.snippet.topLevelComment.snippet.authorDisplayName,
text: item.snippet.topLevelComment.snippet.textDisplay,
publishedAt: item.snippet.topLevelComment.snippet.publishedAt,
videoId: videoId,
commentId: item.snippet.topLevelComment.id,
}));
return comments;
} catch (err) {
console.error('Error fetching comments:', err);
throw new Error(`Failed to fetch comments: ${err.message}`);
}
};
export { fetchCommentsFromYT };