-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetcher.js
More file actions
159 lines (139 loc) · 4.19 KB
/
Copy pathfetcher.js
File metadata and controls
159 lines (139 loc) · 4.19 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
"use strict"
const Log = require("logger");
const BASE_URL = "https://ws.audioscrobbler.com/2.0/";
/**
* Last.fm fetcher object.
*
* This object is responsible for data retrieval
* from Last.fm, call intervals and broadcasts downstream.
*/
module.exports = class Fetcher {
#passiveCounter = 0;
#listener_ids = [];
#active = true;
#scheduleId;
/**
*
* @param {string} notification message type
* @param {object} payload message data
*/
#callback = () => {};
#apiKey;
#username;
#activeInterval;
#passiveInterval;
#passiveCount;
/**
* Create a new Fetcher object.
*
* @param {object} config module config parameters
* @param {fn} callback method to broadcast messages
*/
constructor(config, callback) {
this.#apiKey = config.apiKey;
this.#username = config.username;
this.#activeInterval = config.activeInterval;
this.#passiveInterval = config.passiveInterval;
this.#passiveCount = config.passiveCount;
this.#callback = callback;
this.#schedule();
}
/**
* Check if the fetcher is compatible with given module config.
*
* @param {object} config module config parameters
* @returns boolean
*/
isListenerCompatible(config) {
return (
this.#apiKey === config.apiKey &&
this.#username === config.username &&
this.#activeInterval === config.activeInterval &&
this.#passiveInterval === config.passiveInterval &&
this.#passiveCount === config.passiveCount);
}
/**
* Register a module with the fetcher.
*
* @param {string} listener_id module identifier
*/
addListener(listener_id) {
if (this.#listener_ids.includes(listener_id)) return;
Log.info(`[FETCHER][${this.#listener_ids}] Adding listener ${listener_id}.`);
this.#listener_ids.push(listener_id);
}
/**
* Retrieve data from Last.fm
*/
#get() {
Log.info(`[FETCHER][${this.#listener_ids}] Retrieving Last.fm data.`);
const url = new URL(BASE_URL);
url.searchParams.set("method", "user.getrecenttracks");
url.searchParams.set("user", this.#username);
url.searchParams.set("api_key", this.#apiKey);
url.searchParams.set("limit", "1");
url.searchParams.set("format", "json");
fetch(url.toString())
.then(response => response.json())
.then((data) => this.#status(data))
.then((data) => this.#broadcast(data));
}
/**
* Parse data and update active state.
*
* @param {object} data last.fm data
* @returns object
*/
#status(data) {
if (data.recenttracks.track.length === 0 ||
!("@attr" in data.recenttracks.track[0]) ||
!("nowplaying" in data.recenttracks.track[0]["@attr"]) ||
!data.recenttracks.track[0]["@attr"]["nowplaying"]) {
this.#passiveCounter >= this.#passiveCount ? this.#setActive(false) : this.#passiveCounter++;
return {};
}
this.#passiveCounter = 0;
this.#setActive(true);
return data;
}
/**
* Broadcast data to each module.
*
* @param {object} data Last.fm data
*/
#broadcast(data) {
this.#listener_ids.forEach(id => this.#callback("UPDATE", {
identifier: id,
data,
}));
}
/**
* Set the active state and reschedule the scheduler.
*
* @param {boolean} active the active state
*/
#setActive(active) {
if (this.#active === active) return;
Log.info(`[FETCHER][${this.#listener_ids}] Switching active to ${active}.`);
this.#unschedule();
this.#active = active;
this.#schedule();
}
/**
* Create a new scheduler.
*/
#schedule() {
this.#scheduleId = setInterval(
() => this.#get(),
this.#active ? this.#activeInterval * 1000 : this.#passiveInterval * 1000
);
this.#get();
}
/**
* Remove the scheduler.
*/
#unschedule() {
if (this.#scheduleId) clearInterval(this.#scheduleId);
this.#scheduleId = undefined;
}
}