-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
207 lines (174 loc) · 7.65 KB
/
app.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const fs = require('fs')
const puppeteer = require('puppeteer');
const prompt = require('prompt-sync')({sigint: true});
const {blizzardEmail, blizzardPassword} = require('./config');
const getYoutubeLiveComments = async (liveVideoUrl, isDebugMode) => {
console.log('youtube live url detected');
const browser = await puppeteer.launch({headless: !isDebugMode, defaultViewport: null});
// get cookies from blizzard battle.net
const blizzardCookies = await getBlizzardCookies(browser);
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/64.0.3282.39 Safari/537.36');
await page.goto(liveVideoUrl);
// switch to the chat iframe
await page.waitFor('iframe#chatframe')
console.log('iframe is ready. Loading iframe content');
// keep refreshing the messages
setInterval(async () => {
const elementHandle = await page.$('iframe#chatframe');
const chatFrame = await elementHandle.contentFrame();
let messages = await chatFrame.evaluate(() => {
return Array.from(document.querySelectorAll('span#message')).map(
message => {
const text = message.innerText.trim();
if (text !== '')
return text;
}
)
});
if (isDebugMode)
console.log('-------------------------------------------');
for (const message of messages) {
if (message) {
let codeMatches = message.match(/(\w{2,10}-){3,10}(\w{2,10})/mg);
if (codeMatches !== null) {
try {
const code = codeMatches[0];
// save code to txt file
fs.appendFile('codes.txt', code + '\n', (err) => {
if (err) console.log(err);
console.log('Saved!');
});
console.log('======================== code found ========================');
console.log(code);
console.log('============================================================');
await redeemCode(blizzardCookies, code);
} catch (e) {
console.log(e);
}
} else if (isDebugMode)
console.log(message);
}
}
console.log(`${Date.now()}| ${messages.length} messages found on live feed`)
}, 200);
};
const getTwitchComments = async (liveVideoUrl, isDebugMode) => {
console.log('twitch url detected');
const browser = await puppeteer.launch({headless: false, defaultViewport: null});
// get cookies from blizzard battle.net
const blizzardCookies = await getBlizzardCookies(browser);
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/64.0.3282.39 Safari/537.36');
await page.goto(liveVideoUrl);
// switch to the chat fragment to load
await page.waitFor('.text-fragment')
console.log('chat has been loaded. getting messages...');
// keep refreshing the messages
setInterval(async () => {
let messages = await page.evaluate(() => {
return Array.from(document.querySelectorAll('.text-fragment')).map(
message => {
const text = message.innerText.trim();
if (text !== '')
return text;
}
)
});
if (isDebugMode)
console.log('-------------------------------------------');
for (const message of messages) {
if (message) {
let codeMatches = message.match(/(\w{2,10}-){3,10}(\w{2,10})/mg);
if (codeMatches !== null) {
const code = codeMatches[0];
console.log('======================== code found ========================');
console.log(code);
console.log('============================================================');
await redeemCode(blizzardCookies, code);
} else if (isDebugMode)
console.log(message);
}
}
console.log(`${messages.length} messages found on live feed`)
}, 100);
};
const getBlizzardCookies = async (browser) => {
console.log('logging into battle.net account')
const loginUrl = 'https://us.battle.net/login/en/?ref=https://us.battle.net/shop/en/checkout/key-claim&app=shop';
const email = blizzardEmail;
const password = blizzardPassword;
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) ' +
'AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/64.0.3282.39 Safari/537.36');
await page.goto(loginUrl);
// console.log('inserting email');
await page.type('#accountName', email, {delay: 50});
// console.log('inserting password');
await page.type('#password', password, {delay: 50});
// console.log('submitting');
await page.evaluate(() => {
document.querySelector('#submit').click();
});
try {
// wait for next page to load
await page.waitFor('#enter-code-input', {timeout: 8000})
console.log('logged in!')
} catch (e) {
console.log('security code required. a code should be sent to your registered email.')
const securityCode = prompt('Security code: ').trim();
// enter the security code and continue
await page.type('#email', securityCode, {delay: 50})
await page.evaluate(() => {
document.querySelector('#challenge-submit').click();
})
await page.waitFor('#enter-code-input', {timeout: 5000})
console.log('logged in!')
}
// return the cookies
const cookies = (await page.cookies()).map(ck => `${ck["name"]}=${ck["value"]}`)
await page.close();
return cookies.join('; ');
}
const redeemCode = async (cookie, code) => {
console.log('redeeming code:', code);
const url = `https://us.battle.net/shop/en/checkout/key-claim?keyCode=${code}` +
'&returnUrl=https%3A%2F%2Fus.shop.battle.net%2F';
await fetch(url,
{
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9," +
"image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language": "en-IN,en-GB;q=0.9,en-US;q=0.8,en;q=0.7",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"cookie": cookie
},
"referrer": "https://us.battle.net/shop/en/checkout/key-claim",
"referrerPolicy": "no-referrer-when-downgrade",
"body": null,
"method": "GET",
"mode": "cors"
});
console.log('code redeemed!');
}
const main = () => {
const isDebugMode = process.argv.includes('debug');
console.log('enter a twitch or youtube live stream url');
const liveVideoUrl = prompt('Live video url: ').trim();
if (liveVideoUrl.includes('youtube'))
getYoutubeLiveComments(liveVideoUrl, isDebugMode).then(r => null);
else if (liveVideoUrl.includes('twitch'))
getTwitchComments(liveVideoUrl, isDebugMode).then(r => null);
else
console.log('invalid url');
}
main();