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>
This commit is contained in:
忧郁的紫色毛衣男孩 2022-03-27 00:09:34 +08:00 committed by GitHub
parent e42bdf3d97
commit e61a90db77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 146 additions and 64 deletions

View File

@ -197,6 +197,12 @@ For example:
<RouteEn author="nczitzk" example="/nasa/apod-ncku" path="/nasa/apod-ncku" />
## National Geographic
### Photo of the Day
<RouteEn author="LogicJake OrangeEd1t TonyRL" example="/natgeo/dailyphoto" path="/natgeo/dailyphoto"/>
## nHentai
### Filter

View File

@ -475,9 +475,13 @@ R18 显示
## 国家地理
### 每日精选
<Route author="OrangeEd1t" example="/natgeo/dailyselection" path="/natgeo/dailyselection"/>
### 每日一图
<Route author="LogicJake" example="/natgeo/dailyphoto" path="/natgeo/dailyphoto"/>
<Route author="LogicJake OrangeEd1t TonyRL" example="/natgeo/dailyphoto" path="/natgeo/dailyphoto"/>
## 煎蛋

View File

@ -92,7 +92,7 @@ IATA 国际航空运输协会机场代码,参见[维基百科 国际航空运
### 分类
<Route author="fengkx" example="/natgeo/environment/article" path="/natgeo/:cat/:type?" :paramsDesc="['分类', '类型, 例如`https://www.natgeomedia.com/environment/photo/`对应 cat, type 分别为 environment, photo']"/>
<Route author="fengkx" example="/natgeo/environment/article" path="/natgeo/:cat/:type?" :paramsDesc="['分类', '类型, 例如`https://www.natgeomedia.com/environment/photo/`对应 `cat`, `type` 分别为 `environment`, `photo`']"/>
## 活动行

View File

@ -301,9 +301,10 @@ router.get('/t66y/:id/:type?', lazyloadRouteHandler('./routes/t66y/index'));
// 色中色
router.get('/sexinsex/:id/:type?', lazyloadRouteHandler('./routes/sexinsex/index'));
// 国家地理
router.get('/natgeo/dailyphoto', lazyloadRouteHandler('./routes/natgeo/dailyphoto'));
router.get('/natgeo/:cat/:type?', lazyloadRouteHandler('./routes/natgeo/natgeo'));
// 国家地理 migrated to v2
// router.get('/natgeo/dailyselection', lazyloadRouteHandler('./routes/natgeo/dailyselection'));
// router.get('/natgeo/dailyphoto', lazyloadRouteHandler('./routes/natgeo/dailyphoto'));
// router.get('/natgeo/:cat/:type?', lazyloadRouteHandler('./routes/natgeo/natgeo'));
// 一个
router.get('/one', lazyloadRouteHandler('./routes/one/index'));

View File

@ -1,27 +0,0 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth() + 1;
const api = `https://www.nationalgeographic.com/content/photography/en_US/photo-of-the-day/_jcr_content/.gallery.${year}-${month}.json`;
const response = await got.get(api);
const items = response.data.items;
const out = items.slice(0, 10).map((item) => {
const info = {
title: item.image.title,
author: item.image.credit && item.image.credit.replace('Photograph by ', ''),
link: item.pageUrl,
description: `<img src="${item.image.uri}">` + item.image.caption,
};
return info;
});
ctx.state.data = {
title: 'Photo of the Day',
link: 'https://www.nationalgeographic.com/photography/photo-of-the-day/',
item: out,
};
};

View File

@ -0,0 +1,32 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
const config = require('@/config').value;
module.exports = async (ctx) => {
const rootUrl = 'https://www.nationalgeographic.com';
const apiUrl = `${rootUrl}/photo-of-the-day`;
const response = await ctx.cache.tryGet(apiUrl, async () => (await got(apiUrl)).data, config.cache.contentExpire, false);
const $ = cheerio.load(response);
const natgeo = JSON.parse($.html().match(/window\['__natgeo__'\]=(.*);/)[1]);
const media = natgeo.page.content.mediaspotlight.frms[0].mods[0].edgs[1].media;
const items = media.map((item) => ({
title: item.meta.title,
description: art(path.join(__dirname, 'templates/dailyPhoto.art'), {
img: item.img,
}),
link: rootUrl + item.locator,
pubDate: parseDate(item.caption.preHeading),
author: item.img.crdt,
}));
ctx.state.data = {
title: 'Nat Geo Photo of the Day',
link: apiUrl,
item: items,
};
};

View File

@ -0,0 +1,41 @@
const got = require('@/utils/got');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const host = 'http://dili.bdatu.com/jiekou/mains/p1.html';
const data = await got(host);
let sort = 0;
let addtime = '';
for (let i = 0; i < data.data.album.length; i++) {
if (parseInt(data.data.album[i].ds) === 1) {
sort = data.data.album[i].sort;
addtime = data.data.album[i].addtime;
break;
}
}
const api = 'http://dili.bdatu.com/jiekou/albums/a' + sort + '.html';
const response = await got(api);
const items = response.data.picture;
const out = new Array();
items.map((item) => {
const info = {
title: item.title,
link: item.url,
description: `<img src="${item.url}"><br>` + item.content,
pubDate: timezone(parseDate(addtime), +0),
guid: item.id,
};
out.push(info);
return info;
});
ctx.state.data = {
title: 'Photo of the Daily Selection',
link: api,
item: out,
};
};

View File

@ -0,0 +1,5 @@
module.exports = {
'/dailyselection': ['OrangeEd1t'],
'/dailyphoto': ['LogicJake', 'OrangeEd1t', 'TonyRL'],
'/:cat/:type?': ['fengkx'],
};

View File

@ -1,65 +1,51 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
// https://www.natgeomedia.com//article/
const MonthMap = {
JAN: 0,
FEB: 1,
MAR: 2,
APR: 3,
MAY: 4,
JUN: 5,
JUL: 6,
AUG: 7,
SEP: 8,
OCT: 9,
NOV: 10,
DEC: 11,
};
async function load(link) {
const { data } = await got.get(link);
const { data } = await got(link);
const $ = cheerio.load(data);
const dtStr = $('.content-title-area')
.find('h6')
.first()
.text()
.split(/[\s.]+/);
const dt = new Date();
dt.setMonth(MonthMap[dtStr[0].toUpperCase()]);
dt.setDate(dtStr[1]);
.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: dt.toUTCString(),
pubDate: parseDate(dtStr, 'MMM. DD YYYY'),
description,
};
}
module.exports = async (ctx) => {
const type = ctx.params.type || '';
const type = ctx.params.type ?? '';
const url = `https://www.natgeomedia.com/${ctx.params.cat}/${type}`;
const res = await got.get(url);
const res = await got(url);
const $ = cheerio.load(res.data);
const urlList = Array.prototype.map.call($('.article-link-w100').find('.read-btn'), (i) => $(i).find('a[href]').first().attr('href'));
const urlList = $('.article-link-w100')
.find('.read-btn')
.toArray()
.map((i) => ({
link: $(i).find('a[href]').first().attr('href'),
}));
const count = [];
for (let i = 0; i < Math.min(urlList.length, 10); i++) {
count.push(i);
}
const out = await Promise.all(
count.map(async (i) => {
const link = urlList[i];
urlList.map(async (i) => {
const link = i.link;
const single = {
link,
};
const other = await ctx.cache.tryGet(link, () => load(link));
return Object.assign({}, single, other);
return { ...single, ...other };
})
);
ctx.state.data = {

24
lib/v2/natgeo/radar.js Normal file
View File

@ -0,0 +1,24 @@
module.exports = {
'nationalgeographic.com': {
_name: '国家地理',
'.': [
{
title: '每日一图',
docs: 'https://docs-rsshub.pages.dev/picture.html#guo-jia-di-li',
source: ['/photo-of-the-day/*', '/'],
target: '/natgeo/dailyphoto',
},
],
},
'natgeomedia.com': {
_name: '国家地理',
'.': [
{
title: '分类',
docs: 'https://docs.rsshub.app/travel.html#guo-jia-di-li',
source: ['/:cat/:type', '/'],
target: '/natgeo/:cat/:type',
},
],
},
};

5
lib/v2/natgeo/router.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = (router) => {
router.get('/dailyphoto', require('./dailyphoto'));
router.get('/dailyselection', require('./dailyselection'));
router.get('/:cat/:type?', require('./natgeo'));
};

View File

@ -0,0 +1,5 @@
<img src="{{@ img.src }}" alt="{{@ img.altText }}">
<br>
<h3>{{@ img.ttl }}</h3>
<p>{{@ img.dsc }}</p>
<p>{{@ img.crdt }}</p>