This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Discogs.js
124 lines (102 loc) · 3.99 KB
/
MMM-Discogs.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
var ids;
var errors = {
noCredentialsError: "Please specify your Discogs API token and username in config.js. Get your API token at https://www.discogs.com/settings/developers",
fetchingError: "Error while fetching"
};
var updateInterval;
var updateCounter;
function getRandomRelease() {
var ran = Math.floor(Math.random() * (ids.length - 1));
return ids[ran];
}
Module.register("MMM-Discogs", {
defaults: {
apiToken: "",
username: "",
updateDomInterval: 10 * 60 * 1000, //10 minutes
fetchCollection: 50, //update collection every 50 update dom events
animationSpeed: 750
},
start: function () {
var self = this;
updateCounter = self.defaults.fetchCollection;
if (this.config.apiToken == "" || this.config.username == "") {
this.error = errors.noCredentialsError;
} else {
this.error = false;
self.sendSocketNotification("INIT", this.config);
}
},
getDom: function () {
var wrapper = document.createElement("div");
if (!this.loaded) {
if (this.error) {
wrapper.innerHTML = this.error;
} else {
wrapper.innerHTML = this.translate("LOADING");
}
} else {
wrapper.className = "discogs-wrapper";
var cover = document.createElement("div");
cover.className = "discogs-cover";
cover.setAttribute("style", "background-image: url('modules/MMM-Discogs/cover/" + this.release.id + ".jpg')");
wrapper.appendChild(cover);
var descsription = document.createElement("div");
descsription.className = "discogs-description";
var artist = document.createElement("div");
artist.className = "discogs-artist";
artist.innerHTML = this.release.artist;
var title = document.createElement("div");
title.className = "discogs-title";
title.innerHTML = this.release.title + (this.release.year == 0 ? "" : " (" + this.release.year + ")");
var duration = document.createElement("div");
duration.className = "discogs-duration";
duration.innerHTML = this.release.duration;
descsription.appendChild(artist);
descsription.appendChild(title);
descsription.appendChild(duration);
wrapper.appendChild(descsription);
}
return wrapper;
},
socketNotificationReceived: function (notification, payload) {
if (notification === "NEW_DATA_RELEASE") {
this.loaded = true;
this.release = payload;
this.updateDom(this.config.animationSpeed);
} else if (notification === "NEW_DATA_IDS") {
ids = payload;
updateCounter = this.defaults.fetchCollection;
this.fetchRelease();
} else if (notification === "INIT_COMPLETE") {
this.fetchCollection();
} else if (notification === "ERROR") {
this.loaded = false;
this.error = errors[payload];
}
},
getStyles: function () {
return ["mmm-disocogs-style.css"];
},
getCommands: function(commander) {
commander.add({
command: 'discogsNext',
callback: 'fetchRelease',
description: "Get the next release from Discogs.",
})
},
fetchCollection: function () {
updateCounter = this.defaults.fetchCollection;
this.sendSocketNotification("FETCH_COLLECTION");
},
fetchRelease: function () {
var self = this;
window.clearInterval(updateInterval);
self.sendSocketNotification("FETCH_RELEASE", getRandomRelease());
updateInterval = setInterval(function () {
self.sendSocketNotification("FETCH_RELEASE", getRandomRelease());
updateCounter--;
if (updateCounter <= 0) self.fetchCollection();
}, self.config.updateDomInterval);
}
});