diff --git a/router.js b/router.js index f93d48525..6613dc411 100644 --- a/router.js +++ b/router.js @@ -61,7 +61,8 @@ router.get('/mzitu/post/:id', require('./routes/mzitu/post')); router.get('/mzitu/tag/:tag', require('./routes/mzitu/tag')); // // Pixiv -router.get('/pixiv/user/:id', require('./routes/pixiv/user')); +router.get('/pixiv/user/bookmarks/:id', require('./routes/pixiv/bookmarks')); +router.get('/pixiv/user/:id/', require('./routes/pixiv/user')); router.get('/pixiv/ranking/:mode/:date?', require('./routes/pixiv/ranking')); // 豆瓣 diff --git a/routes/pixiv/api/getBookmarks.js b/routes/pixiv/api/getBookmarks.js new file mode 100644 index 000000000..b218b26e6 --- /dev/null +++ b/routes/pixiv/api/getBookmarks.js @@ -0,0 +1,25 @@ + +const axios = require('axios'); +const maskHeader = require('../constants').maskHeader; + +/** + * 获取用户的收藏 + * + * @param {string} user_id 目标用户id + * @param {string} token pixiv oauth token + * @returns {Promise>} + */ +module.exports = async function getBookmarks(user_id, token) { + return await axios({ + method: 'get', + url: 'https://app-api.pixiv.net/v1/user/bookmarks/illust', + headers: { + ...maskHeader, + 'Authorization': 'Bearer ' + token + }, + params: { + user_id: user_id, + restrict: 'public' + }, + }); +} \ No newline at end of file diff --git a/routes/pixiv/api/getIllusts.js b/routes/pixiv/api/getIllusts.js new file mode 100644 index 000000000..de6d11b59 --- /dev/null +++ b/routes/pixiv/api/getIllusts.js @@ -0,0 +1,24 @@ +const axios = require('axios'); +const maskHeader = require('../constants').maskHeader; + +/** + * 获取用户插画作品 + * @param {string} user_id 目标用户id + * @param {string} token pixiv oauth token + * @returns {Promise>} + */ +module.exports = async function getIllusts(user_id, token) { + return await axios({ + method: 'get', + url: 'https://app-api.pixiv.net/v1/user/illusts', + headers: { + ...maskHeader, + 'Authorization': 'Bearer ' + token + }, + params: { + user_id: user_id, + filter: 'for_ios', + type: 'illust' + }, + }) +} diff --git a/routes/pixiv/api/getRanking.js b/routes/pixiv/api/getRanking.js new file mode 100644 index 000000000..1f30dbef0 --- /dev/null +++ b/routes/pixiv/api/getRanking.js @@ -0,0 +1,33 @@ +const axios = require('axios'); +const maskHeader = require('../constants').maskHeader; +const assert = require('assert'); + +const allowMode = [ + 'day', 'week', 'month', 'day_male', 'day_female', 'week_original', 'week_rookie', 'day_r18', 'day_male_r18', 'day_female_r18', 'week_r18', 'week_r18g' +]; + +/** + * 获取某天的排行榜 + * @param {string} mode 模式 + * @param {Date} date 日期 + * @param {string} token pixiv oauth token + * @returns {Promise>} + */ +module.exports = async function getRanking(mode, date, token) { + assert(allowMode.includes(mode), 'Mode not allow.'); + return await axios({ + method: 'get', + url: 'https://app-api.pixiv.net/v1/illust/ranking', + headers: { + ...maskHeader, + 'Authorization': 'Bearer ' + token + }, + params: { + mode: mode, + filter: 'for_ios', + ...(date && { + date: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}` + }) + }, + }); +} \ No newline at end of file diff --git a/routes/pixiv/api/getUserDetail.js b/routes/pixiv/api/getUserDetail.js new file mode 100644 index 000000000..45c718090 --- /dev/null +++ b/routes/pixiv/api/getUserDetail.js @@ -0,0 +1,29 @@ + +const axios = require('axios'); +const maskHeader = require('../constants').maskHeader; + +/** + * pixiv 用户 + * @typedef {{user: {id: number, name: string, account: string, profile_image_urls: any, comment: string}, profile: any} userDetail + */ + +/** + * 获取用户信息 + * + * @param {string} user_id 目标用户id + * @param {string} token pixiv oauth token + * @returns {Promise>} + */ +module.exports = async function getUserDetail(user_id, token) { + return await axios({ + method: 'get', + url: 'https://app-api.pixiv.net/v1/user/detail', + headers: { + ...maskHeader, + 'Authorization': 'Bearer ' + token + }, + params: { + user_id: user_id + }, + }); +} \ No newline at end of file diff --git a/routes/pixiv/bookmarks.js b/routes/pixiv/bookmarks.js new file mode 100644 index 000000000..8b2ff56e7 --- /dev/null +++ b/routes/pixiv/bookmarks.js @@ -0,0 +1,51 @@ +const config = require('../../config'); + +const pixivConfig = config.pixiv; + +if (!pixivConfig) { + module.exports = () => {}; + return; +} + +const template = require('../../utils/template'); +const getToken = require('./token'); +const getBookmarks = require('./api/getBookmarks'); +const getUserDetail = require('./api/getUserDetail'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + if (typeof getToken() === 'null') { + ctx.throw(500); + return; + } + + const [bookmarksResponse, userDetailResponse] = await Promise.all([ + getBookmarks(id, getToken()), + getUserDetail(id, getToken()) + ]); + + const illusts = bookmarksResponse.data.illusts; + const username = userDetailResponse.data.user.name + + ctx.body = template({ + title: `${username} 的收藏`, + link: `https://www.pixiv.net/bookmark.php?id=${id}`, + description: `${username} 的 pixiv 最新收藏`, + item: illusts.map((illust, index) => { + const images = []; + if (illust.page_count === 1) { + images.push(`

`); + } else { + for (let i = 0; i < illust.page_count; i++) { + images.push(`

`); + } + } + return { + title: `${illust.title}`, + description: `

画师:${illust.user.name} - 上传于:${new Date(illust.create_date).toLocaleString('zh-cn')} - 阅览数:${illust.total_view} - 收藏数:${illust.total_bookmarks}

${images.join('')}`, + link: `https://www.pixiv.net/member_illust.php?mode=medium&illust_id=${illust.id}` + }; + }) + }); +}; \ No newline at end of file diff --git a/routes/pixiv/ranking.js b/routes/pixiv/ranking.js index 6bee576c4..37a1ef8bf 100644 --- a/routes/pixiv/ranking.js +++ b/routes/pixiv/ranking.js @@ -8,17 +8,9 @@ if (!pixivConfig) { return; } -const axios = require('axios'); const template = require('../../utils/template'); -const FormData = require('form-data'); const getToken = require('./token'); -const maskHeader = require('./constants').maskHeader; -const assert = require('assert'); -const util = require('util'); - -const allowMode = [ - 'day', 'week', 'month', 'day_male', 'day_female', 'week_original', 'week_rookie', 'day_r18', 'day_male_r18', 'day_female_r18', 'week_r18', 'week_r18g' -]; +const getRanking = require('./api/getRanking'); const titles = { 'day': 'pixiv 日排行', @@ -54,28 +46,12 @@ module.exports = async (ctx) => { const mode = ctx.params.mode; const date = ctx.params.date ? new Date(ctx.params.date) : new Date(); - assert(allowMode.includes(mode), 'Mode not allow.'); - if (typeof getToken() === 'null') { ctx.throw(500); return; } - const response = await axios({ - method: 'get', - url: 'https://app-api.pixiv.net/v1/illust/ranking', - headers: { - ...maskHeader, - 'Authorization': 'Bearer ' + getToken() - }, - params: { - mode: mode, - filter: 'for_ios', - ...(ctx.params.date && { - date: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}` - }) - }, - }); + const response = await getRanking(mode, ctx.params.date && date, getToken()); const illusts = response.data.illusts; diff --git a/routes/pixiv/user.js b/routes/pixiv/user.js index b13bf7d19..644bb2ce4 100644 --- a/routes/pixiv/user.js +++ b/routes/pixiv/user.js @@ -8,11 +8,9 @@ if (!pixivConfig) { return; } -const axios = require('axios'); const template = require('../../utils/template'); -const FormData = require('form-data'); const getToken = require('./token'); -const maskHeader = require('./constants').maskHeader; +const getIllusts = require('./api/getIllusts'); module.exports = async (ctx) => { const id = ctx.params.id; @@ -22,19 +20,7 @@ module.exports = async (ctx) => { return; } - const response = await axios({ - method: 'get', - url: 'https://app-api.pixiv.net/v1/user/illusts', - headers: { - ...maskHeader, - 'Authorization': 'Bearer ' + getToken() - }, - params: { - user_id: id, - filter: 'for_ios', - type: 'illust' - }, - }); + const response = await getIllusts(id, getToken()); const illusts = response.data.illusts; const username = illusts[0].user.name