forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): the atlantic (DIYgod#12092)
* 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
1 parent
b33996b
commit afc93ac
Showing
8 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'/:category': ['NavePnow'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = (router) => { | ||
router.get('/:category', require('./news')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |