-
Notifications
You must be signed in to change notification settings - Fork 0
/
copycat.js
120 lines (110 loc) · 3.92 KB
/
copycat.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
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
// NOTE: this is an old version of copycat and it might not work anymore. please use the typescript version.
// require('dotenv').config();
//Scopes channel_editor channel:manage:broadcast channel:edit:commercial
import dotenv from "dotenv";
dotenv.config({
silent: process.env.NODE_ENV === 'production'
});
import fetch from 'node-fetch';
var esaGameId = "";
var bsgGameId = "";
var esaGame = "";
var esaTitle = "";
var bsgTitle = "";
var interval = process.env.INTERVAL; //in minutes
var readerChannelID = process.env.READERID; //giving channel, ESA
// var readerChannelID = '277226652' //McRaeathon
var targetChannelID = process.env.TARGETID; //receiving channels, BSG
logic();
setInterval(function() {
logic();
}, interval * 60 * 1000);
function logic() {
getGame();
// startAd(); //Debug only no touch
setTimeout(function() {
if (esaGameId !== bsgGameId) {
setGame();
startAd();
} else {
console.log("No update");
}
}, (2 * 1000));
}
function getGame() { //scope channel_editor
fetch('https://api.twitch.tv/helix/channels?broadcaster_id=' + readerChannelID, {
method: 'GET',
headers: {
// 'Authorization': 'Bearer ' + process.env.LOOKUP_AUTH,
'Authorization': 'Bearer ' + process.env.AUTH_TOKEN,
'Client-Id': process.env.CLIENTID
}
})
.then(res => res.json())
.then(res => {
console.log(res);
esaGame = res.data[0].game_name;
esaGameId = res.data[0].game_id;
esaTitle = res.data[0].title;
console.log("ESA Game saved: " + esaGameId + " " + res.data[0].game_name);
});
fetch('https://api.twitch.tv/helix/channels?broadcaster_id=' + targetChannelID, {
method: 'GET',
headers: {
// 'Authorization': 'Bearer ' + process.env.LOOKUP_AUTH,
'Authorization': 'Bearer ' + process.env.AUTH_TOKEN,
'Client-Id': process.env.CLIENTID
}
})
.then(res => res.json())
.then(res => {
console.log(res);
bsgGameId = res.data[0].game_id;
bsgTitle = res.data[0].title;
console.log("BSG Game saved: " + bsgGameId + ' ' + res.data[0].game_name);
});
}
function setGame() {
postRequest('https://api.twitch.tv/helix/channels?broadcaster_id=' + targetChannelID)
.then(data => console.log(""))
// .catch(error => console.error(error))
.catch(error => console.error())
function postRequest(url, data) {
return fetch(url, {
method: 'PATCH', // 'GET', 'PUT', 'DELETE', etc.
body: '{"game_id": "' + esaGameId + '", "title": "' + esaTitle + '"}', // Coordinate the body type with 'Content-Type'
headers: {
'Client-ID': process.env.CLIENTID,
'Authorization': 'Bearer ' + process.env.AUTH_TOKEN,
// 'Authorization': 'Bearer ' + process.env.POSTAUTH, //scope channel:manage:broadcast
// 'Accept': 'application/vnd.twitchtv.v5+json',
'Content-Type': 'application/json'
},
})
.then(response => response.json())
}
console.log("UPDATE: Changed BSG game to " + esaGameId + ' ' + esaGame);
}
function startAd() {
postRequest('https://api.twitch.tv/helix/channels/commercial')
.then(data => console.log(""))
.catch(error => console.error(error))
console.log('Sent commercial of 180 seconds');
function postRequest(url, data) {
return fetch(url, {
method: 'POST', // 'GET', 'PUT', 'DELETE', etc.
body: '{"broadcaster_id": ' + targetChannelID + ', "length": 180}', // Coordinate the body type with 'Content-Type'
headers: {
'Client-ID': process.env.CLIENTID,
// 'Authorization': 'Bearer ' + process.env.ADAUTH, //scope channel:edit:commercial
'Authorization': 'Bearer ' + process.env.AUTH_TOKEN,
// 'Accept': 'application/vnd.twitchtv.v5+json',
'Content-Type': 'application/json'
},
})
.then(response => response.json())
.then(res => {
console.log(res)
});
}
}