Skip to content

Commit

Permalink
fix(route): geo daily photo (#7555)
Browse files Browse the repository at this point in the history
* 完成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>
  • Loading branch information
OrangeEd1t and TonyRL authored Mar 26, 2022
1 parent e42bdf3 commit e61a90d
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 64 deletions.
6 changes: 6 additions & 0 deletions docs/en/picture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion docs/picture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

## 煎蛋

Expand Down
2 changes: 1 addition & 1 deletion docs/travel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`']"/>

## 活动行

Expand Down
7 changes: 4 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
27 changes: 0 additions & 27 deletions lib/routes/natgeo/dailyphoto.js

This file was deleted.

32 changes: 32 additions & 0 deletions lib/v2/natgeo/dailyphoto.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
41 changes: 41 additions & 0 deletions lib/v2/natgeo/dailyselection.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
5 changes: 5 additions & 0 deletions lib/v2/natgeo/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
'/dailyselection': ['OrangeEd1t'],
'/dailyphoto': ['LogicJake', 'OrangeEd1t', 'TonyRL'],
'/:cat/:type?': ['fengkx'],
};
50 changes: 18 additions & 32 deletions lib/routes/natgeo/natgeo.js → lib/v2/natgeo/natgeo.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
24 changes: 24 additions & 0 deletions lib/v2/natgeo/radar.js
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions lib/v2/natgeo/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = (router) => {
router.get('/dailyphoto', require('./dailyphoto'));
router.get('/dailyselection', require('./dailyselection'));
router.get('/:cat/:type?', require('./natgeo'));
};
5 changes: 5 additions & 0 deletions lib/v2/natgeo/templates/dailyPhoto.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<img src="{{@ img.src }}" alt="{{@ img.altText }}">
<br>
<h3>{{@ img.ttl }}</h3>
<p>{{@ img.dsc }}</p>
<p>{{@ img.crdt }}</p>

0 comments on commit e61a90d

Please sign in to comment.