Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add Nicesss呦糖社 #14364

Closed
wants to merge 11 commits into from
27 changes: 27 additions & 0 deletions lib/v2/nicesss/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const utils = require('./utils');

module.exports = async (ctx) => {
const limit = ctx.params.limit || 20;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the common parameter limit from ctx.query.limit.
docs: https://docs.rsshub.app/parameter#limit-entries

const items = [];
const categoryID = ctx.params.category_id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • camelCase
  • Please search the id in your proposed route instead of asking user to do so.


let pages = Math.ceil(limit / 10);
pages = pages > 10 ? 10 : pages;
const urls = Array.from({ length: pages }, (_, i) => `https://www.nicesss.com/wp-json/wp/v2/posts?categories=${categoryID}&page=${i + 1}`);

const itemPromises = urls.map((url) => utils.processUrl(url, ctx));
const allItems = await Promise.all(itemPromises);
items.push(...allItems.flat());
Comment on lines +8 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract data from the first page only.

As mentioned before in #14364 (review), you can adjust the no. of articles returned in the API using per_page search parameter.


const category = await utils.processCategory(ctx, categoryID);

ctx.state.data = {
title: `呦糖社 - “${category.name}”Category结果`,
link: category.link,
description: `呦糖社 - “${category.name}”Category结果`,
image: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/logo/logo-light.png',
icon: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
logo: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
item: items.slice(0, limit),
};
};
24 changes: 24 additions & 0 deletions lib/v2/nicesss/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const utils = require('./utils');

module.exports = async (ctx) => {
const limit = ctx.params.limit || 20;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the common parameter limit from ctx.query.limit.
docs: https://docs.rsshub.app/parameter#limit-entries

const items = [];

let pages = Math.ceil(limit / 10);
pages = pages > 10 ? 10 : pages;
const urls = Array.from({ length: pages }, (_, i) => `https://www.nicesss.com/wp-json/wp/v2/posts?page=${i + 1}`);

const itemPromises = urls.map((url) => utils.processUrl(url, ctx));
const allItems = await Promise.all(itemPromises);
items.push(...allItems.flat());
Comment on lines +7 to +13
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract data from the first page only.

As mentioned before in #14364 (review), you can adjust the no. of articles returned in the API using per_page search parameter.


ctx.state.data = {
title: '呦糖社 - 最新',
link: 'https://www.nicesss.com/',
description: '呦糖社 - 最新',
image: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/logo/logo-light.png',
icon: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
logo: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
item: items.slice(0, limit),
};
};
6 changes: 6 additions & 0 deletions lib/v2/nicesss/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
'/:limit?': ['Steven-Fake'],
'/search/:keyword/:limit?': ['Steven-Fake'],
'/tag/:tag_id/:limit?': ['Steven-Fake'],
'/category/:category_id/:limit?': ['Steven-Fake'],
};
34 changes: 34 additions & 0 deletions lib/v2/nicesss/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
'nicesss.com': {
_name: 'Nicesss 呦糖社 ',
www: [
{
title: '首页 Homepage',
docs: 'https://docs.rsshub.app/routes/picture#you-tang-she',
source: ['/', '/page/:page'],
target: `/nicesss/`,
},
{
title: '搜索 Search',
docs: 'https://docs.rsshub.app/routes/picture#you-tang-she',
source: ['/', '/page/:page'],
target: (_, url) => `/nicesss/search/${new URL(url).searchParams.get('s')}`,
},
{
title: '标签 Tag',
docs: 'https://docs.rsshub.app/routes/picture#you-tang-she',
source: ['/archives/tag/:tag', '/archives/tag/:tag/page/:page?'],
target: (params) => `/nicesss/tag/${params.tag}`,
},
{
title: '类别 Category',
docs: 'https://docs.rsshub.app/routes/picture#you-tang-she',
source: ['/:category/', '/:category/page/:page'],
target: (_, url) => {
const param = new URL(url).pathname.split('/').find(Boolean);
return param === 'archives' || param === 'page' || param === undefined ? `/nicesss/` : `/nicesss/category/${param}`;
},
},
],
},
};
6 changes: 6 additions & 0 deletions lib/v2/nicesss/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (router) => {
router.get('/:limit?', require('./index'));
router.get('/search/:keyword/:limit?', require('./search'));
router.get('/tag/:tag_id/:limit?', require('./tag'));
router.get('/category/:category_id/:limit?', require('./category'));
};
25 changes: 25 additions & 0 deletions lib/v2/nicesss/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const utils = require('./utils');

module.exports = async (ctx) => {
const limit = ctx.params.limit || 20;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the common parameter limit from ctx.query.limit.
docs: https://docs.rsshub.app/parameter#limit-entries

const items = [];
const keyword = decodeURIComponent(ctx.params.keyword);

let pages = Math.ceil(limit / 10);
pages = pages > 10 ? 10 : pages;
const urls = Array.from({ length: pages }, (_, i) => `https://www.nicesss.com/wp-json/wp/v2/posts?search=${keyword}&page=${i + 1}`);

const itemPromises = urls.map((url) => utils.processUrl(url, ctx));
const allItems = await Promise.all(itemPromises);
items.push(...allItems.flat());
Comment on lines +8 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract data from the first page only.

As mentioned before in #14364 (review), you can adjust the no. of articles returned in the API using per_page search parameter.


ctx.state.data = {
title: `呦糖社 - “${keyword}”搜索结果`,
link: `https://www.nicesss.com/?s=${keyword}`,
description: `呦糖社 - “${keyword}”搜索结果`,
image: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/logo/logo-light.png',
icon: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
logo: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
item: items.slice(0, limit),
};
};
27 changes: 27 additions & 0 deletions lib/v2/nicesss/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const utils = require('./utils');

module.exports = async (ctx) => {
const limit = ctx.params.limit || 20;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the common parameter limit from ctx.query.limit.
docs: https://docs.rsshub.app/parameter#limit-entries

const items = [];
const tagID = ctx.params.tag_id;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • camelCase
  • Please search the id in your proposed route instead of asking user to do so.


let pages = Math.ceil(limit / 10);
pages = pages > 10 ? 10 : pages;
const urls = Array.from({ length: pages }, (_, i) => `https://www.nicesss.com/wp-json/wp/v2/posts?tags=${tagID}&page=${i + 1}`);

const itemPromises = urls.map((url) => utils.processUrl(url, ctx));
const allItems = await Promise.all(itemPromises);
items.push(...allItems.flat());

const tag = await utils.processTag(ctx, tagID);
Comment on lines +8 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract data from the first page only.

As mentioned before in #14364 (review), you can adjust the no. of articles returned in the API using per_page search parameter.


ctx.state.data = {
title: `呦糖社 - “${tag.name}”Tag结果`,
link: tag.link,
description: `呦糖社 - “${tag.name}”Tag结果`,
image: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/logo/logo-light.png',
icon: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
logo: 'https://www.nicesss.com/wp-content/themes/ripro/assets/images/favicon/favicon.png',
item: items.slice(0, limit),
};
};
57 changes: 57 additions & 0 deletions lib/v2/nicesss/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');

function parseContent(content) {
// 处理Content
const $ = cheerio.load(content);
$('noscript').remove(); // 删除所有的<noscript>元素
$('img').each(function () {
// 更新所有<img>元素的src属性为data-srcset的值
const srcset = $(this).attr('data-srcset');
if (srcset) {
$(this).attr('src', srcset);
}
});
return $.html();
}

async function processCategory(ctx, id) {
// 获取Category
return await ctx.cache.tryGet(`nicesss_categories:${id}`, async () => {
const { data: response } = await got(`https://www.nicesss.com/wp-json/wp/v2/categories/${id}`);
return response;
});
}

async function processTag(ctx, id) {
// 获取Tag
return await ctx.cache.tryGet(`nicesss_tags:${id}`, async () => {
const { data: response } = await got(`https://www.nicesss.com/wp-json/wp/v2/tags/${id}`);
return response;
});
}

async function processItems(ctx, response) {
let items = response.map(async (item) => ({
title: item.title.rendered,
link: item.link,
description: parseContent(item.content.rendered),
author: await processCategory(ctx, item.categories[0]).then((category) => category.name),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

author should be the nickname of a post not category.

category: item.tags,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tags here are tag id instead of mapped tag.

As mentioned before in #14364 (review), you can include the _embed search parameter to obtain mapped categories, tags and author from _embedded properties

guid: item.guid.rendered,
pubDate: parseDate(item.date_gmt),
updated: parseDate(item.modified_gmt),
}));
items = await Promise.all(items);
return items;
}

module.exports = {
processUrl: async (url, ctx) => {
const { data: json } = await got(url);
return await processItems(ctx, json);
},
processCategory,
processTag,
};
28 changes: 28 additions & 0 deletions website/docs/routes/picture.mdx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,31 @@ For example [Latest Wallpapers](https://wallhaven.cc/latest), the route turning
### 频道 {#qi-pa-mai-jia-xiu-pin-dao}

<Route author="Fatpandac nczitzk" example="/qipamaijia/fuli" path="/qipamaijia/:cate?" paramsDesc={['频道名,可在对应网址中找到,默认为最新']} radar="1" />

## 呦糖社 {#you-tang-she}

### 首页 {#you-tang-she-shou-ye}

<Route author="Steven-Fake" example="/nicesss/30" path="/nicesss/:limit?" paramsDesc={['结果数量,不足时显示全部结果,默认为10']} />

### 搜索 {#you-tang-she-sou-suo}

<Route author="Steven-Fake" example="/nicesss/search/蠢沫沫/40" path="/nicesss/search/:keyword/:limit?" paramsDesc={['搜索关键词' ,'结果数量,不足时显示全部结果,默认为10']} />

### 类别 {#you-tang-she-lei-bie}

<Route author="Steven-Fake" example="/nicesss/category/9" path="/nicesss/category/:category_id/:limit?" paramsDesc={['类别的id,获取方式如下' ,'结果数量,不足时显示全部结果,默认为10']}>
:::tip[获取`category_id`的方式]
1. 访问`https://www.nicesss.com/wp-json/wp/v2/categories?search={keyword}`,将`{keyword}`替换为你要查找的 category 名称。
2. 该域名将返回一份 json,其中的`id`即为本路由中的`category_id`参数。
:::
</Route>

### 标签 {#you-tang-she-biao-qian}

<Route author="Steven-Fake" example="/nicesss/tag/122/40" path="/nicesss/tag/:tag_id/:limit?" paramsDesc={['标签的id,获取方式如下' ,'结果数量,不足时显示全部结果,默认为10']}>
:::tip[获取`tag_id`的方式]
1. 访问`https://www.nicesss.com/wp-json/wp/v2/tags?search={keyword}`,将`{keyword}`替换为你要查找的 tag 名称。
2. 该域名将返回一份 json,其中的`id`即为本路由中的`tag_id`参数。
:::
</Route>