JSFiddle - React, Tailwind, and code Playground
JavaScript
function uniq(a) {
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
function add(newValue, user) {
fs.readFile('./con.json', function(err, data) {
var obj = JSON.parse(data);
obj.twitch[user].push(...newValue);
var data = uniq(obj.twitch[user]);
obj.twitch[user] = data;
fs.writeFile('./con.json', JSON.stringify(obj, null, 2), function(err) { console.log('done'); });
})
}
const getUser = (name, accessToken, _callback) => {
const idOptions = {
url: 'https://api.twitch.tv/helix/users?login=' + name,
method: "GET",
headers: {
'Client-ID': process.env.CLIENT_ID,
'Authorization': 'Bearer ' + accessToken
}
};
request.get(idOptions, (err, res, body) => {
if (err) return console.log(err);
const objekt = JSON.parse(res.body);
objekt.data.forEach((name, i) => {
userid = name.id
})
});
_callback();
};
function gameRequest(accessToken, username) {
setTimeout(() => {
const gameOptions = {
url: 'https://api.twitch.tv/helix/users/follows?from_id=' + userid + '&first=100',
method: 'GET',
headers: {
'Client-ID': process.env.CLIENT_ID,
'Authorization': 'Bearer ' + accessToken
}
}
const gameRequest = request.get(gameOptions, (err, res, body) => {
let data = [];
const jsonObjekt = JSON.parse(res.body);
jsonObjekt.data.forEach((follower, i) => {
data.push(follower.to_name.toString().toLowerCase().split(' '));
add(data, username)
})
});
}, 2000)
}
const options = {
url: 'https://id.twitch.tv/oauth2/token',
json: true,
body: {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: 'client_credentials'
}
};
function secondFunction(tName) {
channels = request.post(options, (err, res, body) => {
if (err) {
...