-
Notifications
You must be signed in to change notification settings - Fork 1
/
lyricsFetcher.js
95 lines (77 loc) · 2 KB
/
lyricsFetcher.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
/* Magic Mirror
* Module: MMM-FF-Genius-Lyrics
*
* By Michael Trenkler
* ISC Licensed.
*/
const geniusLyrics = require("./node_modules/genius-lyrics-api/index.js");
const LyricFetcher = function (nodeHelper, config) {
var { moduleId } = config;
// public for filtering
this.moduleId = moduleId;
this.error = null;
var lyrics = null;
var artist = null;
var title = null;
var status = null;
var hidden;
this.suspend = () => {
hidden = true;
};
this.resume = () => {
hidden = false;
};
const prepareNotificationConfig = () => {
const copy = Object.assign({ lyrics: lyrics }, config);
copy.artist = artist;
copy.title = title;
copy.status = status;
return copy;
};
const updateLyrics = (lyricsData) => {
lyrics = lyricsData;
nodeHelper.sendSocketNotification("UPDATE_LYRICS", {
config: prepareNotificationConfig()
});
};
this.getLyrics = (songInfo) => {
config.artist = null;
config.title = null;
let _title = songInfo.name;
let _artist = songInfo.artists[0].name;
if (title === _title && artist === _artist) {
return updateLyrics(lyrics);
}
this.error = null;
title = _title;
artist = _artist;
const options = {
apiKey: config.apiKey,
title: songInfo.name,
artist: songInfo.artists[0].name,
optimizeQuery: true,
text_format: "html"
};
status = "LOADING";
geniusLyrics
.getLyrics(options)
.then((lyrics) => {
status = lyrics ? "OK" : "GENIUS_LYRICS_NOT_FOUND";
updateLyrics(lyrics);
})
.catch((err) => {
this.error = {
status: err.response.status,
statusText: err.response.statusText,
data: err.response.data,
config: err.config
};
status = err.response.status;
nodeHelper.sendSocketNotification("ERROR", {
config: prepareNotificationConfig(),
error: this.error
});
});
};
};
module.exports = LyricFetcher;