-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-SpotifyReleases.js
169 lines (144 loc) · 6.24 KB
/
MMM-SpotifyReleases.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
Module.register("MMM-SpotifyReleases", {
// Module config defaults.
defaults: {
useHeader: true, // false if you don't want a header
header: "New releases from your favorite artists!", // Any text you want
maxWidth: "500px",
animationSpeed: 3000, // fade in and out speed
updateInterval: 60 * 60 * 1000,
visibleReleasesAmount: 5,
},
getStyles: function() {
return ["MMM-SpotifyReleases.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0",
// Get data from config file.
this.accessToken = this.config.accessToken;
this.refreshToken = this.config.refreshToken;
this.client_id = this.config.clientID;
this.client_secret = this.config.clientSecret;
this.artists = this.config.artists;
this.visibleReleasesAmount = this.config.visibleReleasesAmount;
// List of all albums.
// This list is populated after a succesfull spotify api request.
this.allAlbums = {};
// put the data in the right format for the socket communation
this.credentials = {
clientID: this.client_id,
clientSecret: this.client_secret,
accessToken: this.accessToken,
refreshToken: this.refreshToken,
};
this.socketSendData = {
credentials: this.credentials,
artists: this.artists,
}
// Setup the spotify connector.
this.sendSocketNotification('CONNECT_TO_SPOTIFY', this.socketSendData);
this.scheduleUpdate();
},
getDom: function() {
// Setup the wrapper.
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
// Show loading text when the spotify api call hasn't been completed yet.
if (!this.loaded) {
wrapper.innerHTML = "Loading new releases from your favorite artists...";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
// Show the header.
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright", "light", "header");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
var top = document.createElement("div");
top.classList.add("list-row");
// Sort all albums by releasedate.
var sortedAlbumList = Object.values(this.allAlbums);
sortedAlbumList = sortedAlbumList.sort((a, b) => (parseInt(b.release_date.split("-").join("")) - parseInt(a.release_date.split("-").join(""))))
// Show the most recent albums.
for(const album_i in sortedAlbumList.slice(0, this.visibleReleasesAmount))
{
var album = sortedAlbumList[album_i];
var div = document.createElement("div");
div.classList.add("xsmall", "bright", "duration", "AlbumCard");
// Show the release date, title and album/single as a horizontal list.
var top_list = document.createElement("ul");
top_list.classList.add("horizontalList");
// Release date.
var releaseDate_elem = document.createElement("h10");
// Calculate the day difference.
var releaseDate = new Date(album.release_date);
var today = new Date();
const diffTime = Math.abs(today - releaseDate);
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
var releaseDateText = "";
if(diffDays === 0)
{
releaseDateText = "Today";
}
else if(diffDays === 1)
{
releaseDateText = "Yesterday";
}
else
{
releaseDateText = diffDays.toString() + " days ago.";
}
releaseDate_elem.innerText = releaseDateText;
top_list.appendChild(releaseDate_elem);
// Album type.
var albumType_elem = document.createElement("h10");
albumType_elem.classList.add("AlbumType");
albumType_elem.innerText = album.album_type;
top_list.appendChild(albumType_elem);
// Title.
var title_elem = document.createElement("h3");
title_elem.classList.add("AlbumName");
title_elem.innerText = album.name;
top_list.appendChild(title_elem);
div.appendChild(top_list);
// Show the artists to a horizontal list.
var artists_elem = document.createElement("ul");
artists_elem.classList.add("horizontalList");
for(const artist_i in album.artists)
{
var artist = album.artists[artist_i];
var artist_name = artist.name;
var artist_elem = document.createElement("li");
artist_elem.classList.add("Artist");
artist_elem.innerText = artist_name;
artists_elem.appendChild(artist_elem);
}
div.appendChild(artists_elem);
wrapper.appendChild(div);
}
return wrapper;
},
// Schedules the refreshing of the recent releases.
scheduleUpdate: function() {
setInterval(() => {
this.sendSocketNotification('UPDATE_RELEASES', this.socketSendData);
}, this.config.updateInterval);
this.sendSocketNotification('UPDATE_RELEASES', this.socketSendData);
var self = this;
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case 'RETRIEVED_SONG_DATA':
if(payload !== "FAILED!")
{
// Replace the currently showed albums with the new ones.
this.allAlbums = payload;
this.updateDom(this.config.animationSpeed);
this.loaded = true;
}
}
},
});