-
Notifications
You must be signed in to change notification settings - Fork 0
/
randGenerator.js
121 lines (67 loc) · 2.81 KB
/
randGenerator.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
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
109
110
111
112
113
114
115
116
117
118
119
120
const fs = require('fs')
const SpotifyWebApi = require('spotify-web-api-node');
const token = '' //Generated by authServer.js
const spotifyApi = new SpotifyWebApi();
spotifyApi.setAccessToken(token);
searchTracks(); //searches tracks with random character
function searchTracks() {
(async () => {
console.log("searchTracks called")
const randomOffset = Math.floor(Math.random() * 10000); //generate randomOffset
const alphabet = "abcdefghijklmnopqrstuvwxyz"
const randomCharacter = alphabet[Math.floor(Math.random() * alphabet.length)] //get a radom character to search tracks with
const data = await spotifyApi.searchTracks(randomCharacter)
const firstPage = data.body.tracks.items;
const max = 19
const random = Math.floor(Math.random() * max)
const artistID = await getRandArtist(randomCharacter) //get ID for random artist using randomCharacter a-z generated from above
var genreSeed = seedGenres(); //get randomly seeded Genre
recommend(firstPage[random].id,artistID,genreSeed) //pass trackSeed, TrackArtist, and genreSeed for recommend function
})().catch(e => {
console.error(e)
});
}
function seedGenres() {
(async () => {
const data = await spotifyApi.getAvailableGenreSeeds()
randSeed = Math.floor(Math.random() * (125 - 0 + 1) + 0) //126 genres so range is 0 - 125
return data.body.genres[randSeed];
})().catch(e => {
console.error(e);
});
}
function getRandArtist(randChar) {
(async () => {
console.log("getRandArtist called")
const offsetArtist = Math.floor(Math.random() * (1000 - 0 + 1) + 0)
const data = await spotifyApi.searchArtists(randChar, {limit: 1, offset: offsetArtist})
console.log("data.body.artists.tiems[0].id is" + data.body.artists.items[0].id)
return data.body.artists.items[0].id;
})().catch(e => {
console.error(e);
});
}
function recommend(seedTrack,seedArtist,seedGenre) {
(async () => {
const data = await spotifyApi.getRecommendations({
seed_artists: [seedArtist],
seed_genres: [seedGenre],
seed_tracks: [seedTrack],
min_popularity: 40,
limit: 30
})
const playList = await spotifyApi.createPlaylist("Completely Random", { "description": 'Your very own random playlist', 'public': true})
console.log(JSON.stringify(data.body.tracks[0].uri))
const playlistID = playList.body.id;
const playlistAdd = []
for( let i = 0; i < 30; i++) {
const currTrackID = data.body.tracks[i].uri
playlistAdd.push(currTrackID);
// await spotifyApi.addTracksToPlaylist(playlistID, "spotify:track:"+currTrackID)
}
await spotifyApi.addTracksToPlaylist(playlistID,playlistAdd)
console.log("Playlist Created!")
})().catch(e => {
console.error(e);
});
}