-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (138 loc) · 5.16 KB
/
index.js
File metadata and controls
180 lines (138 loc) · 5.16 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const SpotifyWebApi = require('spotify-web-api-node');
const { prompt } = require('enquirer');
require('dotenv').config();
const alphanumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
const possibleTitles = ["quirky", "cool", "ion", "atom", "marxism", "willtoplato", "yellow", "unit", "plate" ]
function randomInt(min, max) {
return Math.floor(
Math.random() * (max - min) + min
);
}
function generateState(len) {
var state = [];
for (let i = 0; i < len; ++i) {
let char = alphanumeric[randomInt(0, alphanumeric.length)];
state.push(char);
}
return state.join("");
}
function shuffle(set) {
var array = Array.from(set);
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
async function main() {
const spotifyApi = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
redirectUri: 'https://spotify.com',
});
const scopes = ['user-read-email', 'user-library-read','user-read-currently-playing', 'user-top-read','playlist-modify-private','playlist-modify-public','playlist-read-private','playlist-read-collaborative', 'user-library-modify'];
const state = generateState(100);
let authnUrl = spotifyApi.createAuthorizeURL(scopes, state);
console.log(`authorization url: ${authnUrl}`);
let code = await prompt([{
type: 'input',
name: 'url',
message: 'Enter your url from your browser'
}]).then(data => {
console.assert(data.url);
const url = new URL(data.url);
let code = url.searchParams.get('code');
return code;
});
await spotifyApi.authorizationCodeGrant(code).then(
data => {
spotifyApi.setAccessToken(data.body['access_token']);
spotifyApi.setRefreshToken(data.body['refresh_token']);
},
err => { console.error(`authorizationCodeGrant ${err}`); throw err; }
);
var titlelist = [];
titlelist.push(possibleTitles[randomInt(0, possibleTitles.length - 1)]);
titlelist.push(possibleTitles[randomInt(0, possibleTitles.length - 1)]);
const title = titlelist.join(" ")
// create playlist for authenticated user
const me = spotifyApi.getMe();
const playlist = spotifyApi.createPlaylist((await me).body.id, title);
const playlistId = (await playlist).body.id;
// saves the artist ids of your top 20 tracks to variable artistIds
const topTracksData = spotifyApi.getMyTopTracks();
const artistIds = (await topTracksData).body.items.map(t => t.artists[0].id);
// saves the artistsIds of related artists of your top tracks to list l, then saves uniques to uniqueSongslist
//assert we have nonempty list
console.assert(artistIds.length > 0);
let uniqueSongs = new Set();
for (currartist = 0; currartist < artistIds.length; ++currartist)
{
await spotifyApi.getArtistRelatedArtists(artistIds[currartist]).then(
data =>
{
let relatedArtists = data.body.artists;
for (related = 0; related < relatedArtists.length; ++related)
{
uniqueSongs.add(relatedArtists[related].id);
}
},
err => { console.error(err); }
);
}
var uniqueSongsList = shuffle(uniqueSongs);
// takes top track from each suggested artist
suggestedSongIds = [];
for (i = 0; i < 10; ++i)
{
await spotifyApi.getArtistTopTracks(uniqueSongsList[i],'US').then(
data =>
{
let topTracks = data.body.tracks;
suggestedSongIds.push('spotify:track:' + topTracks[0].id);
},
err => { console.error(err); }
);
}
// add tracks
spotifyApi.addTracksToPlaylist(playlistId, suggestedSongIds).then(
data =>
{
console.log('Nice!')
},
err => { console.error(err); }
);
}
if (require.main === module) {
main().catch(
err => { console.error(err); }
)
}
/*
a number of example calls to the api
spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE', {limit: 5, offset: 0}).then(
data => {
console.log('Album Information', data.body);
},
err => { console.error(err); }
);
spotifyApi.searchTracks('Beautiful').then(
data => {
console.log('Search tracks by "Beautiful"',data.body);
},
err => { console.error(err); }
);
spotifyApi.getMyCurrentPlayingTrack().then(
data => {
console.log('Now playing: ' + data.body.item.name);
},
err => { console.error(err); }
);
*/