-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
executable file
·178 lines (149 loc) · 4.27 KB
/
index.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
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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const chalk = require('chalk');
const ora = require('ora');
const inquirer = require('inquirer');
const Conf = require('conf');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
const spinner = ora('Loading ...');
// config file stored in /Users/{home}/Library/Preferences/{project-name}
const config = new Conf();
const spotifyApi = require('./api_calls/requests')
function auth() {
return new Promise((resolve, reject) => {
inquirer.prompt([
{
type: 'input',
message: 'Enter your Spotify username',
name: 'username'
},
{
type: 'password',
message: 'Enter your Spotify bearer token',
name: 'bearer'
}
]).then(function (answers) {
var answer = JSON.stringify(answers);
config.set(answers);
resolve(true);
}).catch(err => reject(err));
});
}
const singlespotify = async function singlespotify(inputs, flags) {
// "Young Thug"
const artistName = inputs;
// name of the playlist, optional parameter
var playlistName = flags['n'];
if (playlistName === undefined) {
playlistName = `${artistName}: singlespotify`;
}
if (artistName === undefined){
spinner.fail('Failed');
console.log(chalk.red(`
Oops! Remember to add an artist name!
Example
singlespotify "Kendrick Lamar"
`))
return
}
// empty string
if (artistName.trim() === ""){
spinner.fail('Failed');
config.clear();
console.log(chalk.red(`
Oops! Artist name can't be empty. Please provide an artist name!
`))
return
}
// ora loading spinner
spinner.start();
var allTracks = [];
var artists = [];
var relatedTracks = []
// get artist URI
let token = config.get('bearer')
// const artistSearch = await
spotifyApi.searchArtists(artistName, token).then(res => {
if (res.artists.items[0] === undefined) {
spinner.fail('Failed');
config.clear();
console.log(chalk.red(`
Oops! That search didn't work. Try again please!
`))
process.exit()
}
let artistID = res.artists.items[0].id;
spotifyApi.getArtistTopTracks(artistID, token).then(res => {
for (let artistTrack of res.tracks) {
allTracks.push(artistTrack.uri);
}
spotifyApi.getArtistRelatedArtists(artistID, token).then(res => {
for (var i=0;i<5;i++){
if (res.artists[i] !== undefined) {
artists.push(res.artists[i].id);
}
}
for (let i = 0; i < Math.min(artists.length, 5); i++) {
spotifyApi.getArtistTopTracks(artists[i], token).then(res => {
for (let artistTrack of res.tracks) {
relatedTracks.push(artistTrack.uri);
}
})
}
})
})
})
.catch(async err => {
spinner.fail('Failed');
config.clear();
console.log(chalk.red(`
ERROR: Incorrect username or bearer token
You might need to update your bearer token
Generate a new one at https://developer.spotify.com/console/post-playlists/
Try again!
$ singlespotify "artist_name"`));
process.exit()
});
// console.log(artistSearch.data)
var timeout = setInterval(function() {
if(relatedTracks.length !== 0 && allTracks.length !== 0) {
const tracks = allTracks.concat(relatedTracks)
clearInterval(timeout);
// call playlist gen function using allTracks
spotifyApi.createPlaylist(playlistName, token).then(res => spotifyApi.populatePlaylist(res.id, tracks, token).then(res => {
spinner.succeed('Success!');
console.log(chalk.green(`
Your playlist is ready!
It's called "${playlistName}"`));
}))
}
}, 400);
}
spinner.stop(); // like return
const cli = meow(chalk.cyan(`
Usage
$ singlespotify "artist_name"
? Enter your Spotify username <username>
? Enter your Spotify bearer token <bearer>
Options
--name [-n] "playlist name"
Example
$ singlespotify -a "Young Thug"
? Enter your Spotify username kabirvirji
? Enter your Spotify bearer token ************************************************************
For more information visit https://github.com/kabirvirji/singlespotify
`), {
alias: {
n: 'name'
}
}
);
updateNotifier({pkg}).notify();
(async () => {
if (config.get('username') === undefined || config.get('bearer') === undefined) {
let authorization = await auth();
}
singlespotify(cli.input[0], cli.flags);
})()