feat: request updating psn profile for /ps/trophy

This commit is contained in:
DIYgod
2019-08-30 11:55:30 +08:00
parent d218976538
commit 5ef27461a0
3 changed files with 59 additions and 0 deletions
+49
View File
@@ -1,9 +1,58 @@
const got = require('@/utils/got');
const logger = require('@/utils/logger');
const wait = require('@/utils/wait');
const cheerio = require('cheerio');
const toughCookie = require('tough-cookie');
module.exports = async (ctx) => {
const id = ctx.params.id;
// get cookies and token
const cookieJar = new toughCookie.CookieJar();
const tokenResponse = await got({
method: 'get',
url: 'https://psnprofiles.com/',
cookieJar: cookieJar,
});
const token = cheerio
.load(tokenResponse.data)('meta[name="csrf-token"]')
.attr('content');
// update profile
const updateResponse = await got({
method: 'post',
url: 'https://psnprofiles.com/xhr/update',
headers: {
Referer: `https://psnprofiles.com/`,
'x-csrf-token': token,
'x-requested-with': 'XMLHttpRequest',
},
form: true,
data: {
psnId: id,
'g-recaptcha-response': '',
},
cookieJar: cookieJar,
});
if (updateResponse.data.match('In Queue')) {
for (let i = 0; i < 10; i++) {
// eslint-disable-next-line no-await-in-loop
const checkResponse = await got({
method: 'get',
url: `https://psnprofiles.com/xhr/update/status?user=${id}`,
headers: {
Referer: `https://psnprofiles.com/`,
},
});
if (checkResponse.data.status.match('updated')) {
logger.info(`${id}'s PSN profile has been updated`);
break;
}
// eslint-disable-next-line no-await-in-loop
await wait(200);
}
}
const response = await got({
method: 'get',
url: `https://psnprofiles.com/${id}?order=last-trophy`,