-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutubeAPI.js
More file actions
38 lines (31 loc) · 1017 Bytes
/
youtubeAPI.js
File metadata and controls
38 lines (31 loc) · 1017 Bytes
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
38
// youtubeApi.js
const { google } = require('googleapis');
const config = require('./config');
async function getVideoTitle(videoId) {
const youtube = google.youtube({
version: 'v3',
auth: config.apiKey
});
try {
const response = await youtube.videos.list({
part: 'snippet',
id: videoId
});
const video = response.data.items[0];
const title = video.snippet.title;
return title;
} catch (error) {
console.error('Error fetching video title:', error.message);
throw error;
}
}
// YouTube 動画の URL から ID を抽出する
function extractVideoIdFromUrl(url) {
// YouTube 動画の URL から ID を抽出する
const match = url.match(/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/);
return match ? match[1] : null;
}
module.exports = {
getVideoTitle: getVideoTitle,
extractVideoIdFromUrl: extractVideoIdFromUrl
};