From 5ef27461a085f9a46f84bc6f556579362e20efce Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 30 Aug 2019 11:55:30 +0800 Subject: [PATCH] feat: request updating psn profile for /ps/trophy --- lib/routes/ps/trophy.js | 49 +++++++++++++++++++++++++++++++++++++++++ package.json | 1 + yarn.lock | 9 ++++++++ 3 files changed, 59 insertions(+) diff --git a/lib/routes/ps/trophy.js b/lib/routes/ps/trophy.js index c7116c9a31..d4137fdecf 100644 --- a/lib/routes/ps/trophy.js +++ b/lib/routes/ps/trophy.js @@ -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`, diff --git a/package.json b/package.json index d6a10ac81b..8744e7751c 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "sanitize-html": "1.20.1", "sharp": "0.23.0", "socks-proxy-agent": "4.0.2", + "tough-cookie": "3.0.1", "tunnel": "0.0.6", "twit": "2.2.11", "winston": "3.2.1" diff --git a/yarn.lock b/yarn.lock index e761d71d86..9ee320f94f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10289,6 +10289,15 @@ touch@^3.1.0: dependencies: nopt "~1.0.10" +tough-cookie@3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-3.0.1.tgz#9df4f57e739c26930a018184887f4adb7dca73b2" + integrity sha512-yQyJ0u4pZsv9D4clxO69OEjLWYw+jbgspjTue4lTQZLfV0c5l1VmK2y1JK8E9ahdpltPOaAThPcp5nKPUgSnsg== + dependencies: + ip-regex "^2.1.0" + psl "^1.1.28" + punycode "^2.1.1" + tough-cookie@^2.3.3, tough-cookie@^2.3.4, tough-cookie@~2.5.0: version "2.5.0" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"