feat(/ps/trophy): remove auto update; sort items by pubTime; fix title text

This commit is contained in:
DIYgod 2019-09-08 12:43:48 +08:00
parent fc4841bed6
commit 67380a6067
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
3 changed files with 3 additions and 60 deletions

View File

@ -1,58 +1,9 @@
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`,
@ -103,7 +54,8 @@ module.exports = async (ctx) => {
$('.page h3')
.eq(0)
.text()
.trim(),
.trim()
.replace(' Trophies', ''),
description: `<img src="${
item
.find('.trophy source')
@ -152,6 +104,7 @@ module.exports = async (ctx) => {
items.forEach((item) => {
result = result.concat(item);
});
items.sort((a, b) => new Date(a.pubDate) - new Date(b.pubDate));
ctx.state.data = {
title: `${id} 的 PSN 奖杯`,

View File

@ -90,7 +90,6 @@
"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"

View File

@ -10359,15 +10359,6 @@ 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"