-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusicSuggester.js
40 lines (32 loc) · 1.13 KB
/
musicSuggester.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
38
39
40
const SpotifyWebApi = require('spotify-web-api-node');
const spotifyApi = new SpotifyWebApi({
clientId: 'a76b22b0629d40a992d0fd86c4d50622',
clientSecret: '6cf3f015d82143aa99d7fa8ff26c3c54',
redirectUri: 'https://tommy-blog.online/',
});
const emotionMusicMap = {
neutral: 'ambient',
disgusted: 'punk',
fearful: 'dark-ambient',
happy: 'pop',
sad: 'blues',
angry: 'rock',
surprised: 'jazz',
};
async function addMusicToPlaylist(emotion) {
try {
const genre = emotionMusicMap[emotion] || 'pop';
// Lấy access token từ Spotify
const data = await spotifyApi.clientCredentialsGrant();
spotifyApi.setAccessToken(data.body['access_token']);
const searchData = await spotifyApi.searchTracks(`genre:${genre}`, { limit: 1 });
const track = searchData.body.tracks.items[0];
const playlistId = 'YOUR_PLAYLIST_ID';
await spotifyApi.addTracksToPlaylist(playlistId, [track.uri]);
return track.name;
} catch (error) {
console.error('Spotify API error:', error);
return null;
}
}
export default addMusicToPlaylist;