-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
190 lines (160 loc) · 4.96 KB
/
Copy pathindex.js
File metadata and controls
190 lines (160 loc) · 4.96 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
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
'use strict';
const got = require('got');
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();
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 spotifork = async function spotifork(inputs, flags) {
spinner.start();
// playlist URI
let PlaylistURI = inputs;
// name of the playlist, optional parameter
let playlistName = flags['n'];
if (PlaylistURI === undefined){
spinner.fail('Failed');
console.log(chalk.red(`
Oops! Remember to add the playlist URI!
Example
spotifork spotify:user:kabirvirji:playlist:23v4GpUwnvSENslciz2CkC
`))
return
}
let URI = inputs.split(":");
const playlistID = URI[2];
var getPlaylistOptions = {
json: true,
headers: {
'Content-type': 'application/json',
'Authorization' : `Bearer ${config.get('bearer')}`
}
};
// get playlist
got(`https://api.spotify.com/v1/playlists/${playlistID}/tracks`, getPlaylistOptions)
.then(response => {
let responseTracks = response.body.items
if (playlistName === undefined){
playlistName = "🍴";
}
// holds playlist tracks
let tracks = responseTracks.map(responseTrack => responseTrack.track.uri)
var createPlaylistOptions = {
json: true,
headers: {
'Content-type': 'application/json',
'Authorization' : `Bearer ${config.get('bearer')}`,
'Accept' : 'application/json'
},
body: JSON.stringify({ description: `spotiforked using https://github.com/kabirvirji/spotifork`, name: `${playlistName}`, public : true})
};
// create playlist
got.post(`https://api.spotify.com/v1/users/${config.get('username')}/playlists`, createPlaylistOptions)
.then(response => {
// get playlist ID
const newPlaylistID = response.body.id;
// function to add tracks to playlist
function populatePlaylist (id, uris, name) {
var url = `https://api.spotify.com/v1/users/${config.get('username')}/playlists/${id}/tracks?uris=${uris}`
var options = {
json: true,
headers: {
'Content-type': 'application/json',
'Authorization' : `Bearer ${config.get('bearer')}`,
}
};
got.post(url, options)
.then(response => {
spinner.succeed('Success!');
console.log(chalk.green(`
Your playlist is ready!
It's called "${name}"`));
})
.catch(err => {
spinner.fail('Failed');
// don't need to reset config since credentials are correct at this point
console.log(chalk.red(`
There was an error adding songs to the playlist.
However, a playlist was created.
Please try a different search.`));
});
}
populatePlaylist(newPlaylistID, tracks, playlistName);
})
.catch(async err => {
spinner.fail('Failed');
config.clear();
console.log(err)
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/web-api/console/post-playlists/
Try again!
$ spotifork <playlist uri>`));
});
})
.catch(err => {
spinner.fail('Failed');
config.clear()
console.log(chalk.red(`
ERROR: Incorrect username, bearer token, or URI
You might need to update your bearer token
Generate a new one at https://developer.spotify.com/web-api/console/post-playlists/
Remember to select the suggested scopes!
Also, make sure you entered a valid Spotify URI!
Try again!
$ spotifork <playlist uri>`));
})
}
spinner.stop();
const cli = meow(chalk.cyan(`
Usage
$ spotifork <playlist URI>
? Enter your Spotify username <username>
? Enter your Spotify bearer token <bearer>
Options
--name [-n] "playlist name"
Example
$ spotifork spotify:user:kabirvirji:playlist:23v4GpUwnvSENslciz2CkC
? Enter your Spotify username kabirvirji
? Enter your Spotify bearer token ************************************************************
For more information visit https://github.com/kabirvirji/spotifork
`), {
alias: {
n: 'name'
}
}
);
// to notify users about updates
updateNotifier({pkg}).notify();
(async () => {
if (config.get('username') === undefined || config.get('bearer') === undefined) {
let authorization = await auth();
}
spotifork(cli.input[0], cli.flags);
})()