-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixer.js
84 lines (75 loc) · 2.18 KB
/
mixer.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
'use strict';
import { getCookie, makeRestRequest, Platform } from './util.js';
const fetchUserId = () => {
return new Promise((resolve, reject) => {
makeRestRequest({
method: 'GET',
url: 'https://mixer.com/api/v1/users/current',
headers: {},
json: true
})
.then(response => {
resolve(response.id);
})
.catch(reject);
});
};
const fetchFollowedChannels = user_id => {
return makeRestRequest({
method: 'GET',
url:
`https://mixer.com/api/v1/users/${user_id}/follows?limit=32&page=0&order=online:desc,viewersCurrent:desc,token:desc`,
headers: {},
json: true
});
};
const responseToStreamerObjs = response => {
let new_streamer_objs = [];
response.forEach(live_user => {
if (!live_user.online) {
return;
}
new_streamer_objs.push({
avatar: live_user.user.avatarUrl,
name: live_user.user.username,
stream_title: live_user.name,
game: live_user.type.name,
view_count: live_user.viewersCurrent,
link: 'https://www.mixer.com/' + live_user.user.username,
platform: Platform.MIXER
});
});
return new_streamer_objs;
};
class MixerFetcher {
constructor() {
// Whether the last fetch was successful.
this.status = false;
// Used to expire a successful status.
this.last_success = -1;
// The last retrieved streamer objects fetched. If there was a failure,
// return [].
this.streamer_objs = [];
}
// Gets the user's ID, then fetches their live follower list.
// Returns: Promise which resolves when all calls are complete.
fetchStreamerObjs() {
return new Promise((resolve, reject) => {
fetchUserId()
.then(fetchFollowedChannels)
.then(response => {
this.streamer_objs = responseToStreamerObjs(response);
this.status = true;
this.last_success = Date.now();
resolve(this.streamer_objs);
})
.catch(error => {
console.log('Unable to reach Mixer: ', error);
this.status = false;
this.streamer_objs = [];
resolve(this.streamer_objs);
});
});
}
}
export {MixerFetcher};