-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox4.js
69 lines (59 loc) · 2.48 KB
/
sandbox4.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
const fs = require('fs');
const fetch = require('node-fetch');
const newData = require('./gumtree6.json');
const mapVerifiedStreamers = (newData) => {
return Promise.all(Object.entries(newData).map(([gameName, streamers]) => {
return Promise.all([gameName, filterUsersWithVerifiedDrops(streamers)]);
}))
.then(filterEmptyGames)
.then(Object.fromEntries)
.then(array => fs.writeFileSync('results2.json', JSON.stringify(array, null, 2)));
};
function filterUsersWithVerifiedDrops(streamerNames) {
return Promise.all(streamerNames.map(streamerName => getVerifiedDrops(streamerName)))
// .then(item => item.map(newItem => newItem.length > 0))
.then(streamers => streamers
.filter(streamer => streamer.hasDrops));
}
function getVerifiedDrops(streamerName) {
return fetch('https://gql.twitch.tv/gql', {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Client-Id': 'kimne78kx3ncx6brgo4mv6wki5h1ko',
'User-Agent': 'superman'
},
body: JSON.stringify(body(streamerName))
})
.then(res => res.json())
.then(([{ data }]) => ({
loginName: data.user.login,
hasDrops: userHasDrops(data)
}));
}
function userHasDrops(userData) {
return userData.user.lastBroadcast.game?.activeDropCampaigns
.some(campaign => campaign.isAvailableToAllChannels || campaign.applicableChannels.find(channel => channel.id === userData.user.id));
}
function filterEmptyGames(gamesWithDrops) {
return gamesWithDrops.filter(([, usersWithDrops]) => {
return usersWithDrops.length > 0;
});
}
function body(streamerName) {
return [{ 'operationName':'Drops_ChannelDrops_User', 'variables':{ 'login':`${streamerName}`, 'isLoggedIn':false }, 'extensions':{ 'persistedQuery':{ 'version':1, 'sha256Hash':'f309b1d517d288074d50d96512059857cc67d8905d1379e414d70f7b981f2618' } } }];
}
mapVerifiedStreamers(newData);
// ({ loginName: data.user.login,
// channelId: data.user.id,
// newDropsForAll: data.user.lastBroadcast.game?.activeDropCampaigns.filter(item => item.isAvailableToAllChannels === true).map(item => item),
// dropSpecificChannelArray: data.user.lastBroadcast.game?.activeDropCampaigns
// .map(item => item.applicableChannels
// .map(channel => channel.id))[0] })
// .then(([streamers]) => ({
// loginName: streamers.loginName,
// applicableChannel: streamers.dropSpecificChannelArray
// .includes(streamers.channelId)
// || streamers.newDropsForAll
// .filter(drops => drops.isAvailableToAllChannels)
// }));