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): add kyodonews (DIYgod#9435)
Signed-off-by: Rongrong <15956627+Rongronggg9@users.noreply.github.com>
- Loading branch information
1 parent
9ca9ada
commit 40f6401
Showing
5 changed files
with
137 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const got = require('@/utils/got'); | ||
const cheerio = require('cheerio'); | ||
const timezone = require('@/utils/timezone'); | ||
const { parseDate } = require('@/utils/parse-date'); | ||
|
||
module.exports = async (ctx) => { | ||
const language = ctx.params.language ?? 'china'; | ||
const keyword = ctx.params.keyword === 'RSS' ? 'rss' : ctx.params.keyword ?? ''; | ||
|
||
// raise error for invalid languages | ||
if (!['china', 'tchina'].includes(language)) { | ||
throw Error('Invalid language'); | ||
} | ||
|
||
const rootUrl = `https://${language}.kyodonews.net`; | ||
const currentUrl = `${rootUrl}/${keyword ? (keyword === 'rss' ? 'rss/news.xml' : `news/${keyword}`) : ''}`; | ||
|
||
let response; | ||
try { | ||
response = await got(currentUrl); | ||
} catch (e) { | ||
throw e.response && e.response.statusCode === 404 ? new Error('Invalid keyword') : e; | ||
} | ||
|
||
const $ = cheerio.load(response.data, { xmlMode: keyword === 'rss' }); | ||
|
||
let title, description, items; | ||
if (keyword === 'rss') { | ||
title = $('channel > title').text(); | ||
description = $('channel > description').text(); | ||
items = $('item') | ||
.map((_, item) => { | ||
const $item = $(item); | ||
const link = $item.find('link').text(); | ||
// const pubDate = $item.find('pubDate').text(); | ||
return { | ||
link, | ||
// pubDate, // no need to normalize because it's from a valid RSS feed | ||
}; | ||
}) | ||
.get(); | ||
} else { | ||
title = $('head > title').text(); | ||
description = $('meta[name="description"]').attr('content'); | ||
items = $('div.sec-latest > ul > li') | ||
.map((_, item) => { | ||
item = $(item); | ||
const link = item.find('a').attr('href'); | ||
return { | ||
link: `${link.startsWith('http') ? '' : rootUrl}${link}`, | ||
}; | ||
}) | ||
.get(); | ||
} | ||
|
||
items = await Promise.all( | ||
items.map((item) => | ||
ctx.cache.tryGet(item.link, async () => { | ||
const detailResponse = await got({ | ||
method: 'get', | ||
url: item.link, | ||
}); | ||
|
||
const $ = cheerio.load(detailResponse.data); | ||
item.title = $('head > title').text(); | ||
item.author = $('meta[name="author"]').attr('content'); | ||
item.description = $('div.article-body') | ||
.html() | ||
.replace(/(完)(?=<\/p>\s*$)/m, ''); | ||
|
||
const pubDate_match = $('script[type="application/ld+json"]') | ||
.html() | ||
.match(/"datePublished":"([\d\s-:]*?)"/); | ||
if (pubDate_match) { | ||
item.pubDate = timezone(parseDate(pubDate_match[1]), 9); | ||
} | ||
return item; | ||
}) | ||
) | ||
); | ||
|
||
ctx.state.data = { | ||
title, | ||
description, | ||
link: currentUrl, | ||
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,3 @@ | ||
module.exports = { | ||
'/:language?/:keyword?': ['Rongronggg9'], | ||
}; |
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 @@ | ||
module.exports = { | ||
'kyodonews.net': { | ||
_name: '共同网', | ||
china: [ | ||
{ | ||
title: '最新报道', | ||
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao', | ||
source: '/', | ||
target: '/kyodonews/china', | ||
}, | ||
{ | ||
title: '关键词', | ||
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao', | ||
source: '/news/:keyword', | ||
target: '/kyodonews/china/:keyword?', | ||
}, | ||
], | ||
tchina: [ | ||
{ | ||
title: '最新報道', | ||
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao', | ||
source: '/', | ||
target: '/kyodonews/tchina/:keyword?', | ||
}, | ||
{ | ||
title: '關鍵詞', | ||
docs: 'https://docs.rsshub.app/traditional-media.html#gong-tong-wang-zui-xin-bao-dao', | ||
source: '/news/:keyword', | ||
target: '/kyodonews/tchina/:keyword?', | ||
}, | ||
], | ||
}, | ||
}; |
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 = function (router) { | ||
router.get('/:language?/:keyword?', require('./index')); | ||
}; |