Skip to content

Commit

Permalink
feat(route): the atlantic (DIYgod#12092)
Browse files Browse the repository at this point in the history
* feat(route): the atlantic

* fix: docs

* chore: discard async-pool

* fix: docs

* feat: utils update

* fix: typo

* Update lib/v2/theatlantic/utils.js

* Update lib/v2/theatlantic/templates/article-description.art
---------
  • Loading branch information
EthanWng97 authored Mar 14, 2023
1 parent b33996b commit afc93ac
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/en/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,20 @@ Language

<RouteEn author="TonyRL" example="/taiwannews/hot" path="/taiwannews/hot/:lang?" :paramsDesc="['Language, `en` or `zh`, `en` by default']" radar="1" rssbud="1"/>

## The Atlantic

### News

<RouteEn author="NavePnow" example="/theatlantic/latest" path="/theatlantic/:category" :paramsDesc="['category, see below']">

| Popular | Latest | Politics | Technology | Business |
| ------------ | ------ | -------- | ---------- | -------- |
| most-popular | latest | politics | technology | business |

More categories (except photo) can be found within the navigation bar at <https://www.theatlantic.com/>

</RouteEn>

## The Economist

### Category
Expand Down
14 changes: 14 additions & 0 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,20 @@ Solidot 提供的 feed:

</Route>

## The Atlantic

### News

<Route author="NavePnow" example="/theatlantic/latest" path="/theatlantic/:category" :paramsDesc="['分类, 见下表']">

| Popular | Latest | Politics | Technology | Business |
| ------------ | ------ | -------- | ---------- | -------- |
| most-popular | latest | politics | technology | business |

More categories (except photo) can be found within the navigation bar at <https://www.theatlantic.com/>

</Route>

## The Economist

### 分类
Expand Down
3 changes: 3 additions & 0 deletions lib/v2/theatlantic/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/:category': ['NavePnow'],
};
33 changes: 33 additions & 0 deletions lib/v2/theatlantic/news.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { getArticleDetails } = require('./utils');
module.exports = async (ctx) => {
const host = 'https://www.theatlantic.com';
const category = ctx.params.category;
const url = `${host}/${category}/`;
const response = await got({
method: 'get',
url,
});
const $ = cheerio.load(response.data);
const contents = JSON.parse($('script#__NEXT_DATA__').text()).props.pageProps.urqlState;
const firstKey = Object.keys(contents)[0];
const data = JSON.parse(contents[firstKey].data);
let list = Object.values(data)[0].river.edges;
list = list.filter((item) => !item.node.url.startsWith('https://www.theatlantic.com/photo'));
list = list.map((item) => {
const data = {};
data.title = item.node.title;
data.link = item.node.url;
data.pubDate = item.node.datePublished;
return data;
});
const items = await getArticleDetails(list, ctx);

ctx.state.data = {
title: `The Atlantic - ${category.toUpperCase()}`,
link: url,
description: `The Atlantic - ${category.toUpperCase()}`,
item: items,
};
};
13 changes: 13 additions & 0 deletions lib/v2/theatlantic/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
'theatlantic.com': {
_name: 'The Atlantic',
www: [
{
title: '新闻',
docs: 'https://docs.rsshub.app/traditional-media.html#the-atlantic',
source: '/:category',
target: '/theatlantic/:category',
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/theatlantic/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:category', require('./news'));
};
19 changes: 19 additions & 0 deletions lib/v2/theatlantic/templates/article-description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div>
{{@ item.caption}}
<br>
{{ if item.imgUrl }}
<figure>
<img src="{{item.imgUrl}}" alt="{{item.imgAlt}}">
<figcaption>{{item.imgCaption}}</figcaption>
</figure>
{{ /if }}
{{each item.content}}
{{@ $value.innerHtml}}
{{if !$value.tagName}}
<br>
<br>
{{/if}}
{{/each}}

</div>

53 changes: 53 additions & 0 deletions lib/v2/theatlantic/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
const UA = require('@/utils/rand-user-agent')({ browser: 'chrome', os: 'android', device: 'mobile' });

const getArticleDetails = async (items, ctx) => {
const list = await Promise.all(
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const url = item.link;
const response = await got({
url,
method: 'get',
headers: {
'User-Agent': UA,
},
});
const html = response.data;
const $ = cheerio.load(html);
let data = JSON.parse($('script#__NEXT_DATA__').text());

const list = data.props.pageProps.urqlState;
const keyWithContent = Object.keys(list).filter((key) => list[key].data.includes('content'));
data = JSON.parse(list[keyWithContent].data).article;
item.category = data.categories.map((category) => category.slug);
data.channels.forEach((channel) => {
item.category.push(channel.slug);
});
item.content = data.content.filter((item) => item.innerHtml !== undefined && item.innerHtml !== '');
item.caption = data.dek;
item.imgUrl = data.leadArt.image?.url;
item.imgAlt = data.leadArt.image?.altText;
item.imgCaption = data.leadArt.image?.attributionText;
item.description = art(path.join(__dirname, 'templates/article-description.art'), {
item,
});
return {
title: item.title,
pubDate: parseDate(item.pubDate),
link: item.link,
description: item.description,
};
})
)
);
return list;
};

module.exports = {
getArticleDetails,
};

0 comments on commit afc93ac

Please sign in to comment.