-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
363 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
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 types = { | ||
article: 2, | ||
report: 3, | ||
visualization: 4, | ||
}; | ||
|
||
module.exports = async (ctx) => { | ||
const { type = 'article', category = '0' } = ctx.params; | ||
const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30; | ||
|
||
const rootUrl = 'https://dt.yicai.com'; | ||
const apiUrl = new URL('api/getNewsList', rootUrl).href; | ||
const currentUrl = new URL(type, rootUrl).href; | ||
|
||
const { data: response } = await got(apiUrl, { | ||
searchParams: { | ||
page: 1, | ||
rid: types[type], | ||
cid: category, | ||
pageSize: limit, | ||
}, | ||
}); | ||
|
||
let items = response.data.data.slice(0, limit).map((item) => ({ | ||
title: item.newstitle, | ||
link: new URL(item.url, rootUrl).href, | ||
description: art(path.join(__dirname, 'templates/description.art'), { | ||
image: { | ||
src: item.originPic, | ||
alt: item.newstitle, | ||
}, | ||
intro: item.newsnotes, | ||
}), | ||
author: item.creatername, | ||
category: [item.channelrootname, item.channelname, item.NewsTypeName].filter((c) => c), | ||
guid: `yicai-dt-${item.newsid}`, | ||
pubDate: parseDate(item.utc_createdate), | ||
updated: parseDate(item.utc_lastdate), | ||
enclosure_url: item.originVideo, | ||
enclosure_type: item.originVideo ? `video/${item.originVideo.split(/\./).pop()}` : undefined, | ||
upvotes: item.newsscore ?? 0, | ||
})); | ||
|
||
items = await Promise.all( | ||
items.map((item) => | ||
ctx.cache.tryGet(item.link, async () => { | ||
const { data: detailResponse } = await got(item.link); | ||
|
||
const content = cheerio.load(detailResponse); | ||
|
||
content('div.logintips').remove(); | ||
|
||
content('img').each((_, e) => { | ||
e = content(e); | ||
|
||
content(e).replaceWith( | ||
art(path.join(__dirname, 'templates/description.art'), { | ||
image: { | ||
src: e.prop('data-original') ?? e.prop('src'), | ||
alt: e.prop('alt'), | ||
width: e.prop('width'), | ||
height: e.prop('height'), | ||
}, | ||
}) | ||
); | ||
}); | ||
|
||
item.description += art(path.join(__dirname, 'templates/description.art'), { | ||
description: content('div.txt').html(), | ||
}); | ||
item.author = content('div.authortime h3').text(); | ||
item.enclosure_url = ''; | ||
item.enclosure_type = 'video/mp4'; | ||
|
||
return item; | ||
}) | ||
) | ||
); | ||
|
||
const { data: currentResponse } = await got(currentUrl); | ||
|
||
const $ = cheerio.load(currentResponse); | ||
|
||
const title = $('title').text(); | ||
const image = $('div.logo a img').prop('src'); | ||
const icon = new URL($('link[rel="shortcut icon"]').prop('href'), rootUrl).href; | ||
|
||
ctx.state.data = { | ||
item: items, | ||
title: `${$(`a[data-cid="${category}"]`).text()}${title}`, | ||
link: currentUrl, | ||
description: $('meta[name="keywords"]').prop('content'), | ||
language: 'zh', | ||
image, | ||
icon, | ||
logo: icon, | ||
subtitle: $('meta[name="description"]').prop('content'), | ||
author: title.split(/_/).pop(), | ||
allowEmpty: true, | ||
}; | ||
}; |
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
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 |
---|---|---|
@@ -1,11 +1,39 @@ | ||
{{ if thumb }} | ||
<img src="{{ thumb }}"> | ||
{{ if !video?.src && image?.src }} | ||
<figure> | ||
<img | ||
{{ if image.alt }} | ||
alt="{{ image.alt }}" | ||
{{ /if }} | ||
{{ if image.width }} | ||
alt="{{ image.width }}" | ||
{{ /if }} | ||
{{ if image.height }} | ||
alt="{{ image.height }}" | ||
{{ /if }} | ||
src="{{ image.src }}"> | ||
</figure> | ||
{{ /if }} | ||
{{ if video }} | ||
<video controls> | ||
<source src="{{ video }}" type="video/mp4"> | ||
</video> | ||
|
||
{{ if intro }} | ||
<p>{{ intro }}</p> | ||
{{ /if }} | ||
|
||
{{ if video?.src }} | ||
<video | ||
{{ set poster = video.poster || image?.src }} | ||
{{ if poster }} | ||
poster="{{ poster }}" | ||
{{ /if }} | ||
controls> | ||
<source | ||
src="{{ video.src }}" | ||
type="{{ video.type }}"> | ||
<object data="{{ video.src }}"> | ||
<embed src="{{ video.src }}"> | ||
</object> | ||
</video> | ||
{{ /if }} | ||
|
||
{{ if description }} | ||
<p>{{ description }}</p> | ||
{{@ description }} | ||
{{ /if }} |
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
Oops, something went wrong.