Files
HQNode/createAccount.js
2020-06-23 23:27:27 -05:00

251 lines
8.0 KiB
JavaScript

import fs from 'fs';
import readline from 'readline';
import https from 'https';
import prompt from 'prompt';
import Moniker from 'moniker';
import random_name from 'node-random-name';
import {
mainBearerToken
} from './config.json';
import Account from './classes/Account';
import HQSchedule from './classes/HQSchedule';
import Verification from './classes/Verification';
const HOST = 'api-quiz.hype.space';
const INIT_TOKEN = mainBearerToken;
const headers = {
'x-hq-client': 'Android/1.12.2',
'content-type': 'application/json; charset=UTF-8',
'user-agent': 'okhttp/3.8.0',
};
const removeFirstLine = function(srcPath, destPath) {
var rl = readline.createInterface({
input: fs.createReadStream(srcPath)
});
var output = fs.createWriteStream(destPath);
var firstRemoved = false;
rl.on('line', (line) => {
if (!firstRemoved) {
firstRemoved = true;
return;
}
output.write(line + '\n');
}).on('close', () => {})
}
const verify = (number, callback) => {
let verification;
let error;
const body = {
method: 'call',
phone: number,
};
const options = {
host: HOST,
path: '/verifications',
method: 'POST',
headers: {
...headers,
'content-length': JSON.stringify(body).length,
},
};
const req = https.request(options, (res, err) => {
if (err) {
console.log(`Error occured verifying ${number}:`, err);
return {
response: null,
error: err
};
}
res.on('data', (data) => {
verification = JSON.parse(data);
if (!verification.verificationId) {
if (verification.error) {
error = verification.error;
} else {
error = data;
}
} else {
verification = new Verification(verification);
}
callback({
response: verification,
error
});
});
});
req.write(JSON.stringify(body));
req.end();
};
const connect = (broadcastId, callback) => {
let verification;
let error;
const body = {
method: 'call',
phone: number,
};
const options = {
host: HOST,
path: '/verifications',
method: 'POST',
headers: {
...headers,
'content-length': JSON.stringify(body).length,
},
};
const req = https.request(options, (res, err) => {
if (err) {
console.log(`Error occured verifying ${number}:`, err);
return {
response: null,
error: err
};
}
res.on('data', (data) => {
verification = JSON.parse(data);
if (!verification.verificationId) {
if (verification.error) {
error = verification.error;
} else {
error = data;
}
} else {
verification = new Verification(verification);
}
callback({
response: verification,
error
});
});
});
req.write(JSON.stringify(body));
req.end();
};
const getSchedule = (callback) => {
let schedule;
let error;
const options = {
host: HOST,
path: '/shows/now?type=hq',
method: 'GET',
headers: {
'authorization': `Bearer ${INIT_TOKEN}`,
},
};
const req = https.request(options, (res, err) => {
if (err) {
console.log(`Error occured getting game schedule:`, err);
return {
response: null,
error: err
};
}
res.on('data', (data) => {
schedule = JSON.parse(data);
if (!schedule) {
if (schedule.error) {
error = schedule.error;
} else {
error = data;
}
} else {
schedule = new HQSchedule(schedule);
}
callback({
response: schedule,
error
});
});
});
req.write(JSON.stringify({}));
req.end();
}
// const names = Moniker.generator([Moniker.adjective, Moniker.noun], { glue: 'a', maxSize: 4 });
const run = function(){
prompt.start();
prompt.get(['newUser', 'number', 'referral'], (err, res) => {
const newUser = res.newUser;
const referral = res.referral;
console.log('Verifying with referral: ', res.number, referral);
verify(`+44${res.number}`, (result) => {
if (result.response) {
const verification = result.response;
console.log('Verified phone number with result:', verification);
prompt.start();
prompt.get(['code'], (err, res) => {
verification.confirm(res.code, (confirmResult) => {
if (confirmResult.response) {
const auth = confirmResult.response || {};
if (!auth.auth) {
verification.create(newUser, referral, (createResult) => {
if (createResult.response) {
const account = createResult.response;
console.log('Creation result:', account);
fs.readFile('accounts.json', 'utf8', (err, data) => {
if (err) {
console.log('Error reading accounts for writing: ', err);
} else {
const configObj = JSON.parse(data);
configObj.players.push({
"accessToken": account.accessToken,
"admin": account.admin,
"authToken": account.authToken,
"avatarUrl": account.avatarUrl,
"guest": account.guest,
"loginToken": account.loginToken,
"userId": account.userId,
"username": account.username,
"tester": account.tester,
});
const configJSON = JSON.stringify(configObj, null, 2);
fs.writeFile('accounts.json', configJSON, 'utf8', (err, data) => {
if (err) {
console.log('Error saving accounts: ', err);
} else {
console.log('Done!');
run();
}
});
}
});
} else if (createResult.error) console.log('Error creating account:', createResult.error);
});
} else {
console.log('This account already exists?', auth);
}
} else if (confirmResult.error) console.log('Error confirming code:', confirmResult.error);
});
});
} else if (result.error) console.log('Error verifying phone:', result.error);
});
});
}
run();