diff --git a/lib/routes/pixiv/bookmarks.js b/lib/routes/pixiv/bookmarks.js
index eb7ab8cc97..191f88192e 100644
--- a/lib/routes/pixiv/bookmarks.js
+++ b/lib/routes/pixiv/bookmarks.js
@@ -10,11 +10,12 @@ module.exports = async (ctx) => {
const id = ctx.params.id;
- if (!getToken()) {
+ const token = await getToken();
+ if (!token) {
throw 'pixiv not login';
}
- const [bookmarksResponse, userDetailResponse] = await Promise.all([getBookmarks(id, getToken()), getUserDetail(id, getToken())]);
+ const [bookmarksResponse, userDetailResponse] = await Promise.all([getBookmarks(id, token), getUserDetail(id, token)]);
const illusts = bookmarksResponse.data.illusts;
const username = userDetailResponse.data.user.name;
@@ -26,10 +27,10 @@ module.exports = async (ctx) => {
item: illusts.map((illust) => {
const images = [];
if (illust.page_count === 1) {
- images.push(`

`);
+ images.push(`
`);
} else {
for (let i = 0; i < illust.page_count; i++) {
- images.push(`
`);
+ images.push(`
`);
}
}
return {
diff --git a/lib/routes/pixiv/illustfollow.js b/lib/routes/pixiv/illustfollow.js
index 3c817de8f6..301e35c813 100644
--- a/lib/routes/pixiv/illustfollow.js
+++ b/lib/routes/pixiv/illustfollow.js
@@ -5,10 +5,13 @@ module.exports = async (ctx) => {
if (!config.pixiv || !config.pixiv.refreshToken) {
throw 'pixiv RSS is disabled due to the lack of relevant config';
}
- if (!getToken()) {
+
+ const token = await getToken();
+ if (!(await getToken())) {
throw 'pixiv not login';
}
- const response = await getIllustFollows(getToken());
+
+ const response = await getIllustFollows(token);
const illusts = response.data.illusts;
ctx.state.data = {
title: `Pixiv关注的新作品`,
@@ -17,10 +20,10 @@ module.exports = async (ctx) => {
item: illusts.map((illust) => {
const images = [];
if (illust.page_count === 1) {
- images.push(`
`);
+ images.push(`
`);
} else {
for (let i = 0; i < illust.page_count; i++) {
- images.push(`
`);
+ images.push(`
`);
}
}
return {
diff --git a/lib/routes/pixiv/ranking.js b/lib/routes/pixiv/ranking.js
index 7d11991aae..34241b4835 100644
--- a/lib/routes/pixiv/ranking.js
+++ b/lib/routes/pixiv/ranking.js
@@ -55,11 +55,12 @@ module.exports = async (ctx) => {
const mode = alias[ctx.params.mode] ? alias[ctx.params.mode] : ctx.params.mode;
const date = ctx.params.date ? new Date(ctx.params.date) : new Date();
- if (!getToken()) {
+ const token = await getToken();
+ if (!token) {
throw 'pixiv not login';
}
- const response = await getRanking(mode, ctx.params.date && date, getToken());
+ const response = await getRanking(mode, ctx.params.date && date, token);
const illusts = response.data.illusts;
diff --git a/lib/routes/pixiv/search.js b/lib/routes/pixiv/search.js
index 567f10a81b..8cf793ecf7 100644
--- a/lib/routes/pixiv/search.js
+++ b/lib/routes/pixiv/search.js
@@ -12,15 +12,16 @@ module.exports = async (ctx) => {
const order = ctx.params.order || 'date';
const mode = ctx.params.mode;
- if (!getToken()) {
+ const token = await getToken();
+ if (!token) {
throw 'pixiv not login';
}
let response;
if (order === 'popular') {
- response = await searchPopularIllust(keyword, getToken());
+ response = await searchPopularIllust(keyword, token);
} else {
- response = await searchIllust(keyword, getToken());
+ response = await searchIllust(keyword, token);
}
let illusts = response.data.illusts;
diff --git a/lib/routes/pixiv/token.js b/lib/routes/pixiv/token.js
index 178c727284..8a3ccf9958 100644
--- a/lib/routes/pixiv/token.js
+++ b/lib/routes/pixiv/token.js
@@ -1,6 +1,5 @@
const config = require('@/config').value;
const logger = require('@/utils/logger');
-const wait = require('@/utils/wait');
const got = require('./pixiv-got');
const { maskHeader } = require('./constants');
@@ -12,8 +11,8 @@ const authorizationInfo = {
hash_secret: '28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c',
};
-async function refreshToken() {
- const response = await got({
+const refreshToken = () =>
+ got({
method: 'post',
url: 'https://oauth.secure.pixiv.net/auth/token',
form: {
@@ -31,38 +30,29 @@ async function refreshToken() {
logger.error('Pixiv refresh token failed.');
logger.debug(e);
});
- return response;
-}
-async function tokenLoop() {
- const res = await refreshToken();
- if (res && res.access_token) {
- logger.info('Pixiv login success.');
- token = res.access_token;
- let expires_in = res.expires_in * 0.9;
- /* eslint-disable no-constant-condition, no-await-in-loop */
- while (true) {
- await wait(expires_in * 1000);
- const refresh_res = await refreshToken();
- if (refresh_res && refresh_res.access_token) {
- logger.debug('Pixiv refresh token success.');
- token = refresh_res.access_token;
- expires_in = refresh_res.expires_in * 0.9;
- } else {
- expires_in = 30;
- logger.error(`Pixiv refresh token failed, retry in ${expires_in} seconds.`);
- }
- }
- /* eslint-enable no-await-in-loop */
+async function tickToken() {
+ const result = await refreshToken();
+ let expireTime;
+ if (result && result.access_token) {
+ logger.debug('Pixiv refresh token success.');
+ token = result.access_token;
+ expireTime = result.expires_in;
+ } else {
+ expireTime = 30;
+ logger.error(`Pixiv refresh token failed, retry in ${expireTime} seconds.`);
+ }
+ // If is package, the mock server should be closed after request, needn't to refresh
+ if (!config.isPackage) {
+ setTimeout(tickToken, expireTime);
}
}
-if (config.pixiv && config.pixiv.refreshToken) {
- tokenLoop();
-}
-
-module.exports = function getToken() {
+module.exports = async () => {
+ if (!token) {
+ await tickToken();
+ }
return token;
};
-module.exports.refreshToken = refreshToken;
+module.exports.tickToken = tickToken;
diff --git a/lib/routes/pixiv/user.js b/lib/routes/pixiv/user.js
index cbb5cda820..e48a536be1 100644
--- a/lib/routes/pixiv/user.js
+++ b/lib/routes/pixiv/user.js
@@ -8,12 +8,12 @@ module.exports = async (ctx) => {
}
const id = ctx.params.id;
-
- if (!getToken()) {
+ const token = await getToken();
+ if (!token) {
throw 'pixiv not login';
}
- const response = await getIllusts(id, getToken());
+ const response = await getIllusts(id, token);
const illusts = response.data.illusts;
const username = illusts[0].user.name;
@@ -25,10 +25,10 @@ module.exports = async (ctx) => {
item: illusts.map((illust) => {
const images = [];
if (illust.page_count === 1) {
- images.push(`
`);
+ images.push(`
`);
} else {
for (let i = 0; i < illust.page_count; i++) {
- images.push(`
`);
+ images.push(`
`);
}
}
return {