Files
RSSHub/lib/v2/natgeo/natgeo.js
忧郁的紫色毛衣男孩 e61a90db77 fix(route): geo daily photo (#7555)
* 完成issues#7485,在每日一图-国家地理中增加每日精选节点

* 修改文档和router位置

* 对日期pubDate规范进行了调整

* 优化调整获取内容逻辑,修改pubDate字段的获取方式

* 调整pubDate参数

* 删除代码中多余的console

* 调整parse-date引用

* 调整时区

* issues-#7471,解决国家地理每日一图获取失败问题,因为网站更改,并且网站对图片进行了处理,所以调整获取方式。

* 删除无用代码

* 调整代码,错删了代码

* refactor: migrate to v2

* fix: nat geo photo of the day

* fix: natgeo parseDate

* style: lint

* feat: radar for natgeomedia.com

Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
2022-03-27 00:09:34 +08:00

57 lines
1.5 KiB
JavaScript

const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
// https://www.natgeomedia.com//article/
async function load(link) {
const { data } = await got(link);
const $ = cheerio.load(data);
const dtStr = $('.content-title-area')
.find('h6')
.first()
.html()
.replace(/&nbsp;/gi, ' ')
.trim();
let description = $('article').first().html() + $('article').eq(1).html();
if (/photo/.test(link)) {
description = $('#content-album').html() + description;
}
return {
title: $('title').text(),
pubDate: parseDate(dtStr, 'MMM. DD YYYY'),
description,
};
}
module.exports = async (ctx) => {
const type = ctx.params.type ?? '';
const url = `https://www.natgeomedia.com/${ctx.params.cat}/${type}`;
const res = await got(url);
const $ = cheerio.load(res.data);
const urlList = $('.article-link-w100')
.find('.read-btn')
.toArray()
.map((i) => ({
link: $(i).find('a[href]').first().attr('href'),
}));
const out = await Promise.all(
urlList.map(async (i) => {
const link = i.link;
const single = {
link,
};
const other = await ctx.cache.tryGet(link, () => load(link));
return { ...single, ...other };
})
);
ctx.state.data = {
title: $('title').text(),
link: url,
item: out,
};
};