-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotify-loader.js
122 lines (99 loc) · 2.84 KB
/
spotify-loader.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
'use strict';
// NODE_ENV=development node spotify-loader.js
// Load up my private env
require('./config/env/heroku')();
let config = require('./config/config'),
mongoose = require('mongoose'),
path = require('path'),
chalk = require('chalk'),
moment = require('moment'),
SpotifyWebApi = require('spotify-web-api-node'),
accounts = require('./spotify-playlist.json');
/**
* Database, models, and helpers
*/
mongoose.connect(config.db, { useNewUrlParser: true, useUnifiedTopology: true }, function (err) {
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(chalk.red(err));
}
});
require(path.resolve('./app/models/playlist.server.model.js'));
let Playlist = mongoose.model('Playlist');
let getPublishedDate = function (title) {
let onlyDigits = title.replace(/([A-Z])\w+/g, '');
let m = moment(onlyDigits);
if (m.isValid()) {
return m.format('MM.DD.YYYY');
} else {
return null;
}
};
let create = function (title, tracks, done) {
let playlist = new Playlist(tracks);
playlist.title = title
playlist.tracks = tracks;
playlist.published_date = getPublishedDate(title);
let upsertData = playlist.toObject();
delete upsertData._id;
Playlist.updateOne({
title: playlist.title
}, upsertData, {
upsert: true
},
function (err) {
if (err) {
done(err);
} else {
done(playlist);
}
});
};
/**
* Spotify Api
*/
let spotifyApi = new SpotifyWebApi({
clientId: config.spotify.clientID,
clientSecret: config.spotify.clientSecret,
redirectUri: config.spotify.callbackURL
});
let savePlaylist = function (data) {
let tracks = [];
data.tracks.items.forEach(function (item) {
tracks.push({
id: item.track.id,
name: item.track.name,
artist: item.track.artists[0].name,
added_at: item.added_at,
open_url: item.track.external_urls.spotify,
uri: item.track.uri
});
});
let title = data.name;
create(title, tracks, function (data) {
console.log('[' + chalk.green(title) + ']' + ' saved!');
});
};
/**
**** Main entry point *****
* Call out to Spotify and save all the playlist tracks
*/
spotifyApi.clientCredentialsGrant()
.then(function (data) {
console.log('The access token is ' + chalk.yellow(data.body['access_token']));
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
// Loop over the accounts and save each playlist
accounts.forEach(function (acct) {
spotifyApi.getPlaylist(acct.playlist).then(function (data) {
// Upsert to the database
savePlaylist(data.body);
})
.catch(error => {
console.log(error)
});
});
})
.catch(function (error) {
console.log(chalk.red('Something went wrong when retrieving an access token'), error);
});