Skip to content

Commit

Permalink
feat(route): add 正版中国分类 & 标签 & 搜索 (#13769)
Browse files Browse the repository at this point in the history
* feat(route): add 正版中国分类 & 标签 & 搜索

* fix: remove unnecessary parameter

---------
  • Loading branch information
nczitzk authored Nov 13, 2023
1 parent 073021d commit 4cd614f
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 194 deletions.
4 changes: 2 additions & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,8 @@ router.get('/wikihow/index', lazyloadRouteHandler('./routes/wikihow/index.js'));
router.get('/wikihow/category/:category/:type', lazyloadRouteHandler('./routes/wikihow/category.js'));

// 正版中国
router.get('/getitfree/category/:category?', lazyloadRouteHandler('./routes/getitfree/category.js'));
router.get('/getitfree/search/:keyword?', lazyloadRouteHandler('./routes/getitfree/search.js'));
// router.get('/getitfree/category/:category?', lazyloadRouteHandler('./routes/getitfree/category.js'));
// router.get('/getitfree/search/:keyword?', lazyloadRouteHandler('./routes/getitfree/search.js'));

// 万联网
router.get('/10000link/news/:category?', lazyloadRouteHandler('./routes/10000link/news'));
Expand Down
59 changes: 0 additions & 59 deletions lib/routes/getitfree/category.js

This file was deleted.

25 changes: 0 additions & 25 deletions lib/routes/getitfree/search.js

This file was deleted.

47 changes: 0 additions & 47 deletions lib/routes/getitfree/utils.js

This file was deleted.

76 changes: 42 additions & 34 deletions lib/v2/getitfree/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,61 @@ const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');

const {
apiSlug,
rootUrl,

bakeFilterSearchParams,
bakeFiltersWithPair,
bakeUrl,
fetchData,
getFilterNameForTitle,
getFilterParamsForUrl,
parseFilterStr,
} = require('./util');

module.exports = async (ctx) => {
const category = ctx.params.category ?? '';
const { filter } = ctx.params;
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 50;

const rootUrl = 'https://www.getitfree.cn';
const currentUrl = `${rootUrl}${category ? `/category/${category}` : ''}/page/1`;
const filters = parseFilterStr(filter);
const filtersWithPair = await bakeFiltersWithPair(filters);

const response = await got({
method: 'get',
url: currentUrl,
});
const searchParams = bakeFilterSearchParams(filters, 'name', false);
const apiSearchParams = bakeFilterSearchParams(filtersWithPair, 'id', true);

const $ = cheerio.load(response.data);
apiSearchParams.append('_embed', 'true');
apiSearchParams.append('per_page', limit);

let items = $('a.post-title')
.toArray()
.map((item) => {
item = $(item);
const apiUrl = bakeUrl(`${apiSlug}/posts`, rootUrl, apiSearchParams);
const currentUrl = bakeUrl(getFilterParamsForUrl(filtersWithPair) ?? '', rootUrl, searchParams);

return {
title: item.text(),
link: item.attr('href'),
};
});
const { data: response } = await got(apiUrl);

items = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const items = (Array.isArray(response) ? response : JSON.parse(response.match(/(\[.*\])$/)[1])).slice(0, limit).map((item) => {
const terminologies = item._embedded['wp:term'];

const content = cheerio.load(detailResponse.data);
const content = cheerio.load(item.content?.rendered ?? item.content);

content('h3, footer').remove();
content('div.mycred-sell-this-wrapper').prevUntil('hr').nextAll().remove();

item.description = content('.post-content-text').html();
item.pubDate = parseDate(content('time.entry-date').first().attr('datetime'));
return {
title: item.title?.rendered ?? item.title,
link: item.link,
description: content.html(),
author: item._embedded.author.map((a) => a.name).join('/'),
category: [...new Set([].concat(...terminologies).map((c) => c.name))],
guid: item.guid?.rendered ?? item.guid,
pubDate: parseDate(item.date_gmt),
updated: parseDate(item.modified_gmt),
};
});

return item;
})
)
);
const subtitle = getFilterNameForTitle(filtersWithPair);

ctx.state.data = {
title: $('title').text(),
link: currentUrl,
...(await fetchData(currentUrl)),
item: items,
title: `Getitfree${subtitle ? ` | ${subtitle}` : ''}`,
};
};
4 changes: 3 additions & 1 deletion lib/v2/getitfree/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module.exports = {
'/:category?': ['nczitzk'],
'/category/:id': ['sanmmm', 'nczitzk'],
'/tag/:id': ['nczitzk'],
'/search/:keyword': ['sanmmm', 'nczitzk'],
};
21 changes: 19 additions & 2 deletions lib/v2/getitfree/radar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ module.exports = {
{
title: '分类',
docs: 'https://docs.rsshub.app/routes/shopping#zheng-ban-zhong-guo-fen-lei',
source: ['/category/:category', '/'],
target: '/getitfree/:category?',
source: ['/category/:id'],
target: '/getitfree/category/:id',
},
{
title: '标签',
docs: 'https://docs.rsshub.app/routes/shopping#zheng-ban-zhong-guo-biao-qian',
source: ['/tag/:id'],
target: '/getitfree/tag/:id',
},
{
title: '搜索',
docs: 'https://docs.rsshub.app/routes/shopping#zheng-ban-zhong-guo-sou-suo',
source: ['/'],
target: (_, url) => {
url = new URL(url);
const keyword = url.searchParams.get('s');

return `/getitfree/search${keyword ? `/${keyword}` : ''}`;
},
},
],
},
Expand Down
12 changes: 10 additions & 2 deletions lib/v2/getitfree/router.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module.exports = function (router) {
router.get('/:category?', require('./index'));
module.exports = (router) => {
router.get('/:filter*', (ctx) => {
const { filter } = ctx.params;

if (filter && !filter.includes('/') && !filter.includes(',')) {
ctx.redirect(`/getitfree/category/${filter}`);
} else {
return require('./')(ctx);
}
});
};
Loading

0 comments on commit 4cd614f

Please sign in to comment.