refactor(route): migrate qdaily to v2 (#12033)

* refactor: migrate qdaily to v2

* fix: combine category
This commit is contained in:
Tony 2023-03-04 00:57:48 +08:00 committed by GitHub
parent 2b233e878c
commit 7e28a20aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 124 additions and 121 deletions

View File

@ -108,14 +108,6 @@
{ title: '栏目', docs: 'https://docs.rsshub.app/other.html#wegene', source: '/crowdsourcing', target: '/wegene/column/all/all' },
],
},
'qdaily.com': {
_name: '好奇心日报',
www: [
{ title: '标签', docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao', source: '/tags/:idd', target: (params) => `/qdaily/tag/${params.idd.replace('.html', '')}` },
{ title: '栏目', docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao', source: '/special_columns/:idd', target: (params) => `/qdaily/column/${params.idd.replace('.html', '')}` },
{ title: '分类', docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao', source: '/categories/:idd', target: (params) => `/qdaily/category/${params.idd.replace('.html', '')}` },
],
},
'3ycy.com': { _name: '三界异次元', www: [{ title: '最近更新', docs: 'https://docs.rsshub.app/anime.html#san-jie-yi-ci-yuan', source: '/', target: '/3ycy/home' }] },
'emi-nitta.net': {
_name: 'Emi Nitta',

View File

@ -231,30 +231,6 @@ module.exports = {
},
],
},
'qdaily.com': {
_name: '好奇心日报',
www: [
{
title: '标签',
docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao',
source: '/tags/:idd',
target: (params) => `/qdaily/tag/${params.idd.replace('.html', '')}`,
},
{
title: '栏目',
docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao',
source: '/special_columns/:idd',
target: (params) => `/qdaily/column/${params.idd.replace('.html', '')}`,
},
{
title: '分类',
docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao',
source: '/categories/:idd',
target: (params) => `/qdaily/category/${params.idd.replace('.html', '')}`,
},
],
},
'3ycy.com': {
_name: '三界异次元',
www: [

View File

@ -173,7 +173,7 @@ router.get('/geektime/news', lazyloadRouteHandler('./routes/geektime/news'));
// router.get('/jiemian/list/:cid', lazyloadRouteHandler('./routes/jiemian/list.js'));
// 好奇心日报
router.get('/qdaily/:type/:id', lazyloadRouteHandler('./routes/qdaily/index'));
// router.get('/qdaily/:type/:id', lazyloadRouteHandler('./routes/qdaily/index'));
// 爱奇艺
// router.get('/iqiyi/dongman/:id', lazyloadRouteHandler('./routes/iqiyi/dongman'));

View File

@ -1,88 +0,0 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
let type;
switch (ctx.params.type) {
case 'tag':
type = 'tags';
break;
case 'category':
type = 'categories';
break;
case 'column':
type = 'special_columns';
break;
}
const url = `http://www.qdaily.com/${type}/${ctx.params.id}.html`;
const res = await got.get(url);
const $ = cheerio.load(res.data);
const list = $('.article').get();
const proList = [];
const indexList = [];
const out = await Promise.all(
list.map(async (item, i) => {
const $ = cheerio.load(item);
let time = $('.smart-date').attr('data-origindate');
let title = $('.title').text();
if (!title) {
title = $('.smart-dotdotdot').text();
}
if (!title) {
const mmTitle = $('.makemoney-title').text();
const description = $('.makemoney-discription').text();
title = mmTitle + '' + description;
time = new Date();
}
const itemUrl = $('a').attr('href');
const allUrl = `http://www.qdaily.com${itemUrl}`;
const cache = await ctx.cache.get(allUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const single = {
title,
pubDate: new Date(time).toUTCString(),
link: allUrl,
guid: allUrl,
};
proList.push(got.get(allUrl));
indexList.push(i);
return Promise.resolve(single);
})
);
const responses = await got.all(proList);
for (let i = 0; i < responses.length; i++) {
const res = responses[i];
const $ = cheerio.load(res.data);
$('.article-detail-bd .author-share').remove();
$('.article-detail-ft').remove();
$('.lazylood').remove();
$('img[data-src*="201609181246"]').remove();
$('.category-title').remove();
const images = $('img[data-src]');
for (let k = 0; k < images.length; k++) {
$(images[k]).replaceWith(`<img src="${$(images[k]).attr('data-src')}" />`);
}
if (out[indexList[i]].link.match('/weeklies/')) {
out[indexList[i]].description = $('.packery-container').html();
} else {
out[indexList[i]].description = $('.main .com-article-detail').html();
}
ctx.cache.set(out[indexList[i]].link, JSON.stringify(out[i]));
}
ctx.state.data = {
title: $('title').text(),
link: url,
item: out,
};
};

87
lib/v2/qdaily/index.js Normal file
View File

@ -0,0 +1,87 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const baseUrl = 'http://www.qdaily.com';
const { type, id } = ctx.params;
const typeMap = {
tag: {
type: 'tags',
apiPath: 'tags/tagmore',
apiSuffix: '.json',
},
category: {
type: 'categories',
apiPath: 'categories/categorymore',
apiSuffix: '.json',
},
column: {
type: 'special_columns',
apiPath: 'special_columns/show_more',
apiSuffix: '',
},
};
const url = `${baseUrl}/${typeMap[type].type}/${id}.html`;
const res = await got(url);
const $ = cheerio.load(res.data);
const lastKey = $('.packery-container').attr('data-lastkey');
const { data } = await got(`${baseUrl}/${typeMap[type].apiPath}/${id}/${lastKey}${typeMap[type].apiSuffix}`);
const list = data.data.feeds.map((item) => ({
title: item.post.title,
description: item.post.description,
pubDate: parseDate(item.post.publish_time),
link: `${baseUrl}/articles/${item.post.id}.html`,
category: item.post.category.title,
image: item.image,
}));
const out = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const res = await got(item.link);
const $ = cheerio.load(res.data);
$('.author-share, .embed-mask, .lazylad, .lazyloa, .lazylod, .lazylaad, .lazylood, .lazyloadd').remove();
$('.article-detail-bd')
.find('img')
.each((_, img) => {
if (img.attribs['data-src']) {
img.attribs.src = img.attribs['data-src'].split('-w600')[0];
delete img.attribs['data-src'];
}
});
item.description = art(path.join(__dirname, 'templates/article.art'), {
image: item.image.split('?')[0],
description: $('.article-detail-bd').html(),
});
item.category = [
...new Set([
item.category,
...$('.tags .tag a')
.toArray()
.map((item) => $(item).text()),
]),
];
return item;
})
)
);
ctx.state.data = {
title: $('head title').text(),
description: $('head meta[name="description"]').attr('content'),
image: 'http://www.qdaily.com/favicon.ico',
link: url,
item: out,
};
};

View File

@ -0,0 +1,3 @@
module.exports = {
'/:type/:id': ['WenhuWee', 'emdoe', 'SivaGao', 'HenryQW'],
};

3
lib/v2/qdaily/router.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:type/:id', require('./index'));
};

View File

@ -0,0 +1,5 @@
{{ if image }}
<img src="{{ image }}"><br>
{{ /if }}
{{@ description }}

View File

@ -0,0 +1,25 @@
module.exports = {
'qdaily.com': {
_name: '好奇心日报',
'.': [
{
title: '标签',
docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao',
source: ['/tags/:id'],
target: (params) => `/qdaily/tag/${params.id.replace('.html', '')}`,
},
{
title: '栏目',
docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao',
source: ['/special_columns/:id'],
target: (params) => `/qdaily/column/${params.id.replace('.html', '')}`,
},
{
title: '分类',
docs: 'https://docs.rsshub.app/new-media.html#hao-qi-xin-ri-bao',
source: ['/categories/:id'],
target: (params) => `/qdaily/category/${params.id.replace('.html', '')}`,
},
],
},
};