This repository was archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiRunner.js
More file actions
193 lines (178 loc) · 5.72 KB
/
apiRunner.js
File metadata and controls
193 lines (178 loc) · 5.72 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//Version
const appVersion = '1.0.0';
const apiRunnerVersion = '1.0';
const infoButton = document.querySelector('#app-info');
const helpLink = '<a href="https://discordapp.com/invite/bf5hGgD" target="_blank">Help</a>'
//Display Version when info icon is clicked
infoButton.addEventListener("click", function () {
const msg = `App ${appVersion} / Api Runner ${apiRunnerVersion} ${helpLink}`
showAlert(msg, 'primary')
})
//API url
var apiUrl = 'https://mixer.com/api/v1/';
//Return URL from apiUrl and user input
function buildUrl(name) {
console.log('Building URL')
var url = `${apiUrl}channels/${name}`;
return url
}
//Return JSON object from url
function Get(url) {
console.log('Creating API dump for ' + url);
//Get JSON from URL and check status code
var Httpreq = new XMLHttpRequest();
Httpreq.open("GET", url, false);
Httpreq.send(null);
var statusCode = Httpreq.status;
if (statusCode !== 200) {
console.log(response)
return {"statusCode": statusCode};
}
else {
var response = JSON.parse(Httpreq.responseText);
response.statusCode = statusCode;
console.log(response)
return response
}
}
//Return Lightstream status from id(aka ChannelId)
function parseLightstream(channelId) {
console.log('Parsing Lighstream')
videoUrl = `${apiUrl}channels/${channelId}/videoSettings`;
var rawData = Get(videoUrl);
if (rawData['statusCode'] !== 200){
var lightstream = `false ${rawData['statusCode']}`;
return lightstream;
}
else {
var lightstream = rawData['isLSEnabled'];
console.log(lightstream);
return lightstream;
}
}
//Return xuid from id(aka ChannelId)
function parseXuid(channelId) {
console.log('Parsing Lighstream')
videoUrl = `${apiUrl}users/${channelId}/xuid`;
var rawData = Get(videoUrl);
console.log(rawData)
if (rawData['statusCode'] !== 200){xuid = `failed ${rawData['statusCode']}`;}
else {
var xuid = rawData['xuid'];
}
console.log(xuid);
return xuid;
}
//Parse JSON Object for keys defined in keys variable
function parseApi(keys, rawData) {
console.log('Parsing API dump')
var allKeys = Get(keys + '.json');
var apiName;
var parseResult = new Object();
for (apiName in allKeys) {
//Parse String as JSON
var status = rawData[apiName];
//Custom rules below
var humanApiName = allKeys[apiName];
//Save statusCode
if (apiName == 'statusCode') {
humanApiName = apiName;
}
//Getting xuid
if (apiName == 'xuid') {
status = parseXuid(rawData['userId']);
}
//Formatting date
if (apiName == 'createdAt') {
status = status.substr(0,10);
}
//Gettings groups
if (apiName == 'groups') {
var allGroups = new Array()
groupData = rawData.user['groups'];
for (group in groupData) {
allGroups.push(groupData[parseInt(group)].name)
}
console.log(allGroups.toString())
status = allGroups.toString()
}
//Gettings Sparks
if (apiName == 'sparks') {
status = rawData.user.sparks;
}
//Parsing Lighstream
if (apiName == 'isLSEnabled') {
status = parseLightstream(rawData['id']);
}
parseResult[humanApiName] = status
}
return parseResult
}
//Build final table and append to html doc
function buildTable(parseResult) {
for (var name in parseResult) {
status = parseResult[name]
console.log(name, status)
if (status == 'undefined') {console.log(`Skipping ${name}`)}
else if (name === 'statusCode') {console.log(`Skipping ${name}`)}
else {
var template = $("#table-template").html();
var tableRow = Mustache.render(template, {class: name, name: name, status: status});
$("table").append(tableRow);
}
}
}
//Delede existing elements with id="generated"
function resetHtml(htmlTag) {
document.getElementById(htmlTag).innerHTML = '';
console.log('Clearing old HTML');
}
//Display error message with msg, type
function showAlert(msg, type, timeout=3000) {
resetHtml('generated');
const alert = document.querySelector('#alert');
type = `alert-${type}`
alert.innerHTML = msg;
alert.classList.add('alert');
alert.classList.add(type);
const clearAlert = () => {
alert.classList.remove('alert');
alert.classList.remove(type);
alert.innerHTML = '';
}
setTimeout(() => clearAlert(), timeout);
}
//Check if input is valid and call functions
function apiRunner() {
var input = document.getElementById("api-search-box").value;
if (input.length <= 2) {
showAlert('Invalid username or CID', 'warning');
}
else {
resetHtml('generated');
var url = buildUrl(input);
var rawData = Get(url);
var statusCode = rawData["statusCode"]
console.log(`Raw data status ${statusCode}`)
if (statusCode !== 200) {
showAlert(`User not found ${statusCode}`, 'danger')
}
else {
var keys = 'apiList'
var parseResult = parseApi(keys, rawData);
buildTable(parseResult)
}
}
}
// Get the input field
var input = document.getElementById("api-search-box");
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
apiRunner();
}
});