From 53f6347189eff8b6c66945fd683043115ec5d450 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 2 Nov 2021 17:40:24 +0000 Subject: [PATCH] feat: pixiv PIXIV_IMG_PROXY --- docs/en/install/README.md | 2 ++ docs/install/README.md | 2 ++ lib/config.js | 1 + lib/routes/pixiv/bookmarks.js | 10 ++-------- lib/routes/pixiv/illustfollow.js | 11 +++-------- lib/routes/pixiv/ranking.js | 10 ++-------- lib/routes/pixiv/search.js | 10 ++-------- lib/routes/pixiv/user.js | 10 ++-------- lib/routes/pixiv/utils.js | 17 +++++++++++++++++ 9 files changed, 33 insertions(+), 40 deletions(-) create mode 100644 lib/routes/pixiv/utils.js diff --git a/docs/en/install/README.md b/docs/en/install/README.md index c2cb2052a..b9c058b91 100644 --- a/docs/en/install/README.md +++ b/docs/en/install/README.md @@ -480,6 +480,8 @@ See docs of specified route and `lib/config.js` for detail information. - `PIXIV_BYPASS_DOH`: DNS over HTTPS endpoint, it must be compatible with Cloudflare or Google DoH JSON schema, defaults to `https://1.1.1.1/dns-query` + - `PIXIV_IMG_PROXY`: Used as a proxy for image addresses, as pixiv images have anti-theft, default to `https://i.pixiv.cat` + - pixiv fanbox: Get paid content - `FANBOX_SESSION_ID`: equals to `FANBOXSESSID` in site cookies. diff --git a/docs/install/README.md b/docs/install/README.md index 48b2b4fd0..e507696bb 100644 --- a/docs/install/README.md +++ b/docs/install/README.md @@ -508,6 +508,8 @@ RSSHub 支持使用访问密钥 / 码,白名单和黑名单三种方式进行 - `PIXIV_BYPASS_DOH`: 用于解析 `PIXIV_BYPASS_HOSTNAME` 的 DoH 端点 URL,需要兼容 Cloudflare 或 Google 的 DoH 服务的 JSON 查询格式,默认为 `https://1.1.1.1/dns-query` + - `PIXIV_IMG_PROXY`: 用于图片地址的代理,因为 pixiv 图片有防盗链,默认为 `https://i.pixiv.cat` + - pixiv fanbox 用于获取付费内容 - `FANBOX_SESSION_ID`: 对应 cookies 中的`FANBOXSESSID`。 diff --git a/lib/config.js b/lib/config.js index 76a67cef2..6ac845cff 100644 --- a/lib/config.js +++ b/lib/config.js @@ -55,6 +55,7 @@ const calculateValue = () => { bypassCdn: envs.PIXIV_BYPASS_CDN !== '0' && envs.PIXIV_BYPASS_CDN !== 'false', bypassCdnHostname: envs.PIXIV_BYPASS_HOSTNAME || 'public-api.secure.pixiv.net', bypassCdnDoh: envs.PIXIV_BYPASS_DOH || 'https://1.1.1.1/dns-query', + imgProxy: envs.PIXIV_IMG_PROXY || 'https://i.pixiv.cat', }, fanbox: { session: envs.FANBOX_SESSION_ID, diff --git a/lib/routes/pixiv/bookmarks.js b/lib/routes/pixiv/bookmarks.js index ed76036be..a49b97e02 100644 --- a/lib/routes/pixiv/bookmarks.js +++ b/lib/routes/pixiv/bookmarks.js @@ -2,6 +2,7 @@ const getToken = require('./token'); const getBookmarks = require('./api/getBookmarks'); const getUserDetail = require('./api/getUserDetail'); const config = require('@/config').value; +const pixivUtils = require('./utils'); module.exports = async (ctx) => { if (!config.pixiv || !config.pixiv.refreshToken) { @@ -25,14 +26,7 @@ module.exports = async (ctx) => { link: `https://www.pixiv.net/users/${id}/bookmarks/artworks`, description: `${username} 的 pixiv 最新收藏`, item: illusts.map((illust) => { - const images = []; - if (illust.page_count === 1) { - images.push(`

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

`); - } - } + const images = pixivUtils.getImgs(illust); return { title: illust.title, author: illust.user.name, diff --git a/lib/routes/pixiv/illustfollow.js b/lib/routes/pixiv/illustfollow.js index 301e35c81..0a2ae4a9b 100644 --- a/lib/routes/pixiv/illustfollow.js +++ b/lib/routes/pixiv/illustfollow.js @@ -1,6 +1,8 @@ const getToken = require('./token'); const getIllustFollows = require('./api/getIllustFollows'); const config = require('@/config').value; +const pixivUtils = require('./utils'); + module.exports = async (ctx) => { if (!config.pixiv || !config.pixiv.refreshToken) { throw 'pixiv RSS is disabled due to the lack of relevant config'; @@ -18,14 +20,7 @@ module.exports = async (ctx) => { link: 'https://www.pixiv.net/bookmark_new_illust.php', description: `Pixiv关注的画师们的最新作品`, item: illusts.map((illust) => { - const images = []; - if (illust.page_count === 1) { - images.push(`

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

`); - } - } + const images = pixivUtils.getImgs(illust); return { title: illust.title, author: illust.user.name, diff --git a/lib/routes/pixiv/ranking.js b/lib/routes/pixiv/ranking.js index 5064999ca..a616171a0 100644 --- a/lib/routes/pixiv/ranking.js +++ b/lib/routes/pixiv/ranking.js @@ -1,6 +1,7 @@ const getToken = require('./token'); const getRanking = require('./api/getRanking'); const config = require('@/config').value; +const pixivUtils = require('./utils'); const titles = { day: 'pixiv 日排行', @@ -71,14 +72,7 @@ module.exports = async (ctx) => { link: links[mode], description: dateStr + titles[mode], 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(`

`); - } - } + const images = pixivUtils.getImgs(illust); return { title: `#${index + 1} ${illust.title}`, pubDate: new Date(illust.create_date).toUTCString(), diff --git a/lib/routes/pixiv/search.js b/lib/routes/pixiv/search.js index 8cf793ecf..77b6a4423 100644 --- a/lib/routes/pixiv/search.js +++ b/lib/routes/pixiv/search.js @@ -2,6 +2,7 @@ const getToken = require('./token'); const searchPopularIllust = require('./api/searchPopularIllust'); const searchIllust = require('./api/searchIllust'); const config = require('@/config').value; +const pixivUtils = require('./utils'); module.exports = async (ctx) => { if (!config.pixiv || !config.pixiv.refreshToken) { @@ -35,14 +36,7 @@ module.exports = async (ctx) => { title: `${keyword} 的 pixiv ${order === 'popular' ? '热门' : ''}内容`, link: `https://www.pixiv.net/tags/${keyword}/artworks`, item: illusts.map((illust) => { - const images = []; - if (illust.page_count === 1) { - images.push(`

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

`); - } - } + const images = pixivUtils.getImgs(illust); return { title: illust.title, author: illust.user.name, diff --git a/lib/routes/pixiv/user.js b/lib/routes/pixiv/user.js index e48a536be..af8c1fb70 100644 --- a/lib/routes/pixiv/user.js +++ b/lib/routes/pixiv/user.js @@ -1,6 +1,7 @@ const getToken = require('./token'); const getIllusts = require('./api/getIllusts'); const config = require('@/config').value; +const pixivUtils = require('./utils'); module.exports = async (ctx) => { if (!config.pixiv || !config.pixiv.refreshToken) { @@ -23,14 +24,7 @@ module.exports = async (ctx) => { link: `https://www.pixiv.net/users/${id}`, description: `${username} 的 pixiv 最新动态`, item: illusts.map((illust) => { - const images = []; - if (illust.page_count === 1) { - images.push(`

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

`); - } - } + const images = pixivUtils.getImgs(illust); return { title: illust.title, author: username, diff --git a/lib/routes/pixiv/utils.js b/lib/routes/pixiv/utils.js new file mode 100644 index 000000000..3810776a9 --- /dev/null +++ b/lib/routes/pixiv/utils.js @@ -0,0 +1,17 @@ +const config = require('@/config').value; + +module.exports = { + getImgs(illust) { + const images = []; + if (illust.meta_pages?.length) { + for (let i = 0; i < illust.meta_pages.length; i++) { + const original = illust.meta_pages[i].image_urls.original.replace('https://i.pximg.net', config.pixiv.imgProxy); + images.push(`

`); + } + } else if (illust.meta_single_page.original_image_url) { + const original = illust.meta_single_page.original_image_url.replace('https://i.pximg.net', config.pixiv.imgProxy); + images.push(`

`); + } + return images; + }, +};