161 lines
3.9 KiB
JavaScript
161 lines
3.9 KiB
JavaScript
import fs from 'fs';
|
|
import request from 'request';
|
|
import prompts from 'prompts';
|
|
import aws4 from 'aws4';
|
|
import https from 'https';
|
|
import { parseString } from 'xml2js';
|
|
|
|
import { accounts } from './accounts.json';
|
|
import { HEADERS, SERVER_URL } from './config';
|
|
|
|
export default class Avatar {
|
|
constructor() {
|
|
this.logger = null;
|
|
this.playersReturned = 0;
|
|
}
|
|
|
|
async run() {
|
|
console.log(`You have ${accounts.length} players available!\n`);
|
|
|
|
const getChoice = async () => {
|
|
const response = await prompts([
|
|
{
|
|
type: 'text',
|
|
name: 'count',
|
|
message: 'Number of Players to Change Avatar:',
|
|
},
|
|
]);
|
|
|
|
return response;
|
|
};
|
|
|
|
const response = await getChoice();
|
|
const { count } = response;
|
|
|
|
const players = accounts.sort(() => Math.round(Math.random()) - 0.5);
|
|
|
|
for (let i = 0; i < parseInt(count); i++) {
|
|
this.changeAvatar(players[i]);
|
|
}
|
|
|
|
const getSummary = setInterval(() => {
|
|
if (this.playersReturned === parseInt(count)) {
|
|
clearInterval(getSummary);
|
|
fs.writeFile('accounts.json', JSON.stringify({accounts:players}, null, 2), 'utf8', (err, data) => {
|
|
if (err) {
|
|
console.log('Error saving accounts: ', err);
|
|
}
|
|
console.log('Done!');
|
|
});
|
|
}
|
|
}, 1000);
|
|
}
|
|
|
|
changeAvatar(player) {
|
|
const { username } = player;
|
|
console.log(`Attempting to change ${username}'s avatar...`);
|
|
|
|
request({
|
|
url: `https://${SERVER_URL}/credentials/s3`,
|
|
headers: {
|
|
...HEADERS,
|
|
authorization: `Bearer ${player.accessToken}`,
|
|
}
|
|
}, async (error, response, body) => {
|
|
try {
|
|
const result = JSON.parse(body);
|
|
const { error, accessKeyId, secretKey, sessionToken, expiration } = result;
|
|
|
|
if (error) {
|
|
console.log(`Error: ${error}`);
|
|
return;
|
|
}
|
|
|
|
this.uploadAvatar({ ...result, player });
|
|
} catch (error) {
|
|
console.log('Error\n', error.toString('utf8'));
|
|
}
|
|
});
|
|
}
|
|
|
|
uploadAvatar({ accessKeyId, secretKey, sessionToken, expiration, player }) {
|
|
const filename = `${player.username}.jpg`;
|
|
|
|
request({
|
|
uri: 'https://mailaton.com/avagen/',
|
|
encoding: null
|
|
}, (error, response, body) => {
|
|
let options = {
|
|
method: 'PUT',
|
|
service: 's3',
|
|
region: 'us-east-1',
|
|
host: 'hypespace-quiz.s3.amazonaws.com',
|
|
path: `/avatars/${filename}`,
|
|
signQuery: true,
|
|
headers: {
|
|
'Content-Type': 'image/jpeg',
|
|
},
|
|
};
|
|
|
|
aws4.sign(options, {
|
|
accessKeyId,
|
|
secretAccessKey: secretKey,
|
|
sessionToken,
|
|
});
|
|
|
|
options.headers = {
|
|
...options.headers,
|
|
'Content-Length': new Buffer(body).length,
|
|
};
|
|
|
|
const req = https.request(options, response => {
|
|
if (response.statusCode === 200) {
|
|
this.setAvatar(player);
|
|
} else {
|
|
console.log('response', response);
|
|
console.log(`Error uploading avatar. Got Status Code ${response.statusCode}!`);
|
|
}
|
|
});
|
|
|
|
req.on('error', err => {
|
|
console.log('Error uploading avatar:', err);
|
|
});
|
|
|
|
req.write(body);
|
|
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
setAvatar(player) {
|
|
const filename = `${player.username}.jpg`;
|
|
|
|
const options = {
|
|
headers: {
|
|
...HEADERS,
|
|
authorization: `Bearer ${player.accessToken}`,
|
|
},
|
|
body: JSON.stringify({avatarUrl: filename}),
|
|
};
|
|
|
|
request.put(`https://${SERVER_URL}/users/me/avatarUrl`, options)
|
|
.on('error', error => console.log('Error:', error))
|
|
.on('response', response => {
|
|
response.on('data', data => {
|
|
try {
|
|
const { error, avatarUrl } = JSON.parse(data);
|
|
if (error) {
|
|
console.log(`Error setting avatar for ${player.username}: ${error}`);
|
|
} else {
|
|
console.log(`${player.username}'s avatar successfully changed!`, avatarUrl);
|
|
player.avatarUrl = avatarUrl;
|
|
}
|
|
} catch (error) {
|
|
console.log(`Error setting avatar for ${player.username}: ${error}`);
|
|
}
|
|
this.playersReturned++;
|
|
});
|
|
});
|
|
}
|
|
}
|