feat(route): 机核网添加分类标签和标签路由 (#9239)

* Gcores tagging

* Gcore tags

* Comfort v2

* Update router

* Update router.js

* refactor: use cache.tryGet
This commit is contained in:
StevenRCE0 2022-03-06 20:06:37 +08:00 committed by GitHub
parent 0cb66d19b3
commit 6a4b939509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 287 additions and 130 deletions

View File

@ -2031,6 +2031,14 @@ others = 热点新闻 + 滚动新闻
</Route>
### 标签
<Route author="StevenRCE0" example="/gcores/tag/42/articles" path="/gcores/tag/:tag/:category?" :paramsDesc="['标签名,可在选定标签分类页面的 URL 中找到如视觉动物——42', '分类名']" radar="1">
分类名同上。
</Route>
## 加美财经
<Route author="nczitzk" example="/caus" path="/caus/:category?" :paramsDesc="['分类,见下表,默认为全部']">

View File

@ -338,9 +338,6 @@ router.get('/t66y/:id/:type?', lazyloadRouteHandler('./routes/t66y/index'));
// 色中色
router.get('/sexinsex/:id/:type?', lazyloadRouteHandler('./routes/sexinsex/index'));
// 机核
router.get('/gcores/category/:category', lazyloadRouteHandler('./routes/gcores/category'));
// 国家地理
router.get('/natgeo/dailyphoto', lazyloadRouteHandler('./routes/natgeo/dailyphoto'));
router.get('/natgeo/:cat/:type?', lazyloadRouteHandler('./routes/natgeo/natgeo'));

View File

@ -1,127 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const category = ctx.params.category;
const url = `https://www.gcores.com/${category}`;
const res = await got({
method: 'get',
url,
});
const data = res.data;
const $ = cheerio.load(data);
const feedTitle = $('title').text();
const list = $('.original.am_card.original-normal')
.map(function () {
const item = {
url: $(this).find('a.am_card_content.original_content').attr('href'),
title: $(this).find('h3.am_card_title').text(),
};
return item;
})
.get();
const out = await Promise.all(
list.map(async (item) => {
const articleUrl = `https://www.gcores.com${item.url}`;
const cache = await ctx.cache.get(articleUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const itemRes = await got({
method: 'get',
url: articleUrl,
});
const itemPage = itemRes.data;
const $ = cheerio.load(itemPage);
let articleData = await got(`https://www.gcores.com/gapi/v1${item.url}?include=media`);
articleData = articleData.data.data;
let cover;
if (articleData.attributes.cover) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.cover}" />`;
} else if (articleData.attributes.thumb) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.thumb}" />`;
} else {
cover = '';
}
// replace figure with img
const articleContent = JSON.parse(articleData.attributes.content);
const entityRangeMap = {};
for (const block of articleContent.blocks || []) {
if (block.entityRanges.length) {
entityRangeMap[block.key] = block.entityRanges;
}
}
$('figure').each((i, elem) => {
const keyAttr = elem.attribs['data-offset-key'];
const keyMatch = /^(\w+)-(\d+)-(\d)$/.exec(keyAttr);
let actualContent = '';
if (keyMatch) {
const [, key, index] = keyMatch;
if (entityRangeMap[key] && entityRangeMap[key][index]) {
const entityKey = entityRangeMap[key] && entityRangeMap[key][index].key;
const entity = articleContent.entityMap[entityKey];
actualContent = convertEntityToContent(entity);
}
}
if (actualContent) {
$(elem).replaceWith(actualContent);
}
});
// remove editor toolbar img
$('.md-editor-toolbar').replaceWith('');
// remove hidden tip block
$('.story_hidden').replaceWith('');
const content = $('.story.story-show').html();
const single = {
title: item.title,
description: cover + content,
link: articleUrl,
guid: articleUrl,
};
ctx.cache.set(articleUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: feedTitle,
link: url,
item: out,
};
};
function convertEntityToContent(entity) {
const { type, data } = entity;
switch (type) {
case 'IMAGE':
return `
<figure>
<img src="https://image.gcores.com/${data.path}" alt="${data.caption || ''}">
${data.caption ? `<figcaption>${data.caption}</figcaption>` : ''}
</figure>`;
case 'GALLERY':
return data.images
.map(
(image, i, arr) => `
<figure>
<img src="https://image.gcores.com/${image.path}" alt="${image.caption || ''}">
<figcaption>${data.caption || ''} (${i + 1}/${arr.length}) ${image.caption || ''}</figcaption>
</figure>
`
)
.join('');
default:
return '';
}
}

126
lib/v2/gcores/category.js Normal file
View File

@ -0,0 +1,126 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const category = ctx.params.category;
const url = `https://www.gcores.com/${category}`;
const res = await got({
method: 'get',
url,
});
const data = res.data;
const $ = cheerio.load(data);
const feedTitle = $('title').text();
const list = $('.original.am_card.original-normal')
.map(function () {
const item = {
url: $(this).find('a.am_card_content.original_content').attr('href'),
title: $(this).find('h3.am_card_title').text(),
category: $(this).find('span.original_category>a').text(),
};
return item;
})
.get();
const out = await Promise.all(
list.map((item) => {
const articleUrl = `https://www.gcores.com${item.url}`;
return ctx.cache.tryGet(articleUrl, async () => {
const itemRes = await got({
method: 'get',
url: articleUrl,
});
const itemPage = itemRes.data;
const $ = cheerio.load(itemPage);
let articleData = await got(`https://www.gcores.com/gapi/v1${item.url}?include=media`);
articleData = articleData.data.data;
let cover;
if (articleData.attributes.cover) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.cover}" />`;
} else if (articleData.attributes.thumb) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.thumb}" />`;
} else {
cover = '';
}
// replace figure with img
const articleContent = JSON.parse(articleData.attributes.content);
const entityRangeMap = {};
for (const block of articleContent.blocks || []) {
if (block.entityRanges.length) {
entityRangeMap[block.key] = block.entityRanges;
}
}
$('figure').each((i, elem) => {
const keyAttr = elem.attribs['data-offset-key'];
const keyMatch = /^(\w+)-(\d+)-(\d)$/.exec(keyAttr);
let actualContent = '';
if (keyMatch) {
const [, key, index] = keyMatch;
if (entityRangeMap[key] && entityRangeMap[key][index]) {
const entityKey = entityRangeMap[key] && entityRangeMap[key][index].key;
const entity = articleContent.entityMap[entityKey];
actualContent = convertEntityToContent(entity);
}
}
if (actualContent) {
$(elem).replaceWith(actualContent);
}
});
// remove editor toolbar img
$('.md-editor-toolbar').replaceWith('');
// remove hidden tip block
$('.story_hidden').replaceWith('');
const content = $('.story.story-show').html();
return {
title: item.title,
description: cover + content,
category: item.category,
link: articleUrl,
guid: articleUrl,
};
});
})
);
ctx.state.data = {
title: feedTitle,
link: url,
item: out,
};
};
function convertEntityToContent(entity) {
const { type, data } = entity;
switch (type) {
case 'IMAGE':
return `
<figure>
<img src="https://image.gcores.com/${data.path}" alt="${data.caption || ''}">
${data.caption ? `<figcaption>${data.caption}</figcaption>` : ''}
</figure>`;
case 'GALLERY':
return data.images
.map(
(image, i, arr) => `
<figure>
<img src="https://image.gcores.com/${image.path}" alt="${image.caption || ''}">
<figcaption>${data.caption || ''} (${i + 1}/${arr.length}) ${image.caption || ''}</figcaption>
</figure>
`
)
.join('');
default:
return '';
}
}

View File

@ -0,0 +1,4 @@
module.exports = {
'/category/:category': ['MoguCloud', 'StevenRCE0'],
'/tag/:tag/:category?': ['StevenRCE0'],
};

19
lib/v2/gcores/radar.js Normal file
View File

@ -0,0 +1,19 @@
module.exports = {
'gcores.com': {
_name: '机核网',
'.': [
{
title: '分类',
docs: 'https://docs.rsshub.app/new-media.html#ji-he-wang-fen-lei',
source: ['/:category'],
target: '/gcores/category/:category',
},
{
title: '标签',
docs: 'https://docs.rsshub.app/new-media.html#ji-he-wang-biao-qian',
source: ['/categories/:tag', '/'],
target: '/gcores/tag/:tag',
},
],
},
};

4
lib/v2/gcores/router.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = function (router) {
router.get('/category/:category', require('./category'));
router.get('/tag/:tag/:category?', require('./tag'));
};

126
lib/v2/gcores/tag.js Normal file
View File

@ -0,0 +1,126 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const tag = ctx.params.tag;
const category = ctx.params.category;
const url = `https://www.gcores.com/categories/${tag + (category ? `?tab=${category}` : '')}`;
const res = await got({
method: 'get',
url,
});
const data = res.data;
const $ = cheerio.load(data);
const feedTitle = $('title').text();
const list = $('.original.am_card.original-normal')
.map(function () {
const item = {
url: $(this).find('a.am_card_content.original_content').attr('href'),
title: $(this).find('h3.am_card_title').text(),
category: $(this).find('span.original_category>a').text(),
};
return item;
})
.get();
const out = await Promise.all(
list.map((item) => {
const articleUrl = `https://www.gcores.com${item.url}`;
return ctx.cache.tryGet(articleUrl, async () => {
const itemRes = await got({
method: 'get',
url: articleUrl,
});
const itemPage = itemRes.data;
const $ = cheerio.load(itemPage);
let articleData = await got(`https://www.gcores.com/gapi/v1${item.url}?include=media`);
articleData = articleData.data.data;
let cover;
if (articleData.attributes.cover) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.cover}" />`;
} else if (articleData.attributes.thumb) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.thumb}" />`;
} else {
cover = '';
}
// replace figure with img
const articleContent = JSON.parse(articleData.attributes.content);
const entityRangeMap = {};
for (const block of articleContent.blocks || []) {
if (block.entityRanges.length) {
entityRangeMap[block.key] = block.entityRanges;
}
}
$('figure').each((i, elem) => {
const keyAttr = elem.attribs['data-offset-key'];
const keyMatch = /^(\w+)-(\d+)-(\d)$/.exec(keyAttr);
let actualContent = '';
if (keyMatch) {
const [, key, index] = keyMatch;
if (entityRangeMap[key] && entityRangeMap[key][index]) {
const entityKey = entityRangeMap[key] && entityRangeMap[key][index].key;
const entity = articleContent.entityMap[entityKey];
actualContent = convertEntityToContent(entity);
}
}
if (actualContent) {
$(elem).replaceWith(actualContent);
}
});
// remove editor toolbar img
$('.md-editor-toolbar').replaceWith('');
// remove hidden tip block
$('.story_hidden').replaceWith('');
const content = $('.story.story-show').html();
return {
title: item.title,
description: cover + content,
link: articleUrl,
guid: articleUrl,
};
});
})
);
ctx.state.data = {
title: feedTitle,
link: url,
item: out,
};
};
function convertEntityToContent(entity) {
const { type, data } = entity;
switch (type) {
case 'IMAGE':
return `
<figure>
<img src="https://image.gcores.com/${data.path}" alt="${data.caption || ''}">
${data.caption ? `<figcaption>${data.caption}</figcaption>` : ''}
</figure>`;
case 'GALLERY':
return data.images
.map(
(image, i, arr) => `
<figure>
<img src="https://image.gcores.com/${image.path}" alt="${image.caption || ''}">
<figcaption>${data.caption || ''} (${i + 1}/${arr.length}) ${image.caption || ''}</figcaption>
</figure>
`
)
.join('');
default:
return '';
}
}