-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutubeService.js
More file actions
108 lines (97 loc) · 3.34 KB
/
youtubeService.js
File metadata and controls
108 lines (97 loc) · 3.34 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// youtubeService.js
const axios = require('axios');
const ytdl = require('ytdl-core'); // Import ytdl-core
require('dotenv').config(); // Load environment variables
const YOUTUBE_API_KEY = process.env.YOUTUBE_API_KEY;
const YOUTUBE_API_URL = 'https://www.googleapis.com/youtube/v3/search';
async function fetchLatestMusic() {
try {
const response = await axios.get(YOUTUBE_API_URL, {
params: {
part: 'snippet',
maxResults: 1,
q: 'latest music',
type: 'video',
key: YOUTUBE_API_KEY
}
});
console.log('YouTube API response:', response.data);
return response.data.items.map(item => ({
videoId: item.id.videoId,
title: item.snippet.title,
description: item.snippet.description,
thumbnail: item.snippet.thumbnails.high.url,
publishedAt: item.snippet.publishedAt,
channelId: item.snippet.channelId,
channelTitle: item.snippet.channelTitle
}));
} catch (error) {
console.error('Error fetching latest music from YouTube:', error.response ? error.response.data : error.message);
return [];
}
}
async function fetchTrendingMusic() {
try {
const response = await axios.get(YOUTUBE_API_URL, {
params: {
part: 'snippet',
maxResults: 10,
q: 'trending music',
type: 'video',
key: YOUTUBE_API_KEY
}
});
console.log('YouTube API response for trending music:', response.data);
return response.data.items.map(item => ({
videoId: item.id.videoId,
title: item.snippet.title,
description: item.snippet.description,
thumbnail: item.snippet.thumbnails.high.url,
publishedAt: item.snippet.publishedAt,
channelId: item.snippet.channelId,
channelTitle: item.snippet.channelTitle
}));
} catch (error) {
console.error('Error fetching trending music from YouTube:', error.response ? error.response.data : error.message);
return [];
}
}
async function getDownloadUrl(videoUrl) {
try {
const info = await ytdl.getInfo(videoUrl);
const audioFormat = ytdl.chooseFormat(info.formats, { quality: 'highestaudio' });
return audioFormat.url;
} catch (error) {
console.error('Error getting download URL:', error);
}
}
async function searchYouTube(query) {
try {
const response = await axios.get(YOUTUBE_API_URL, {
params: {
part: 'snippet',
maxResults: 10,
q: query + ' music',
type: 'video',
key: YOUTUBE_API_KEY
}
});
return response.data.items.map(item => ({
title: item.snippet.title,
artist: item.snippet.channelTitle,
url: `https://www.youtube.com/watch?v=${item.id.videoId}`,
thumbnail: item.snippet.thumbnails.high.url,
genre: 'YouTube',
source: 'youtube'
}));
} catch (error) {
console.error('YouTube search error:', error);
return [];
}
}
module.exports = {
fetchLatestMusic,
fetchTrendingMusic,
getDownloadUrl,
searchYouTube
};