feat(route): add KBS today (#8130)

This commit is contained in:
Ethan Shen 2022-01-23 00:07:35 +08:00 committed by GitHub
parent d1c371dfd8
commit 46075a072e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 0 deletions

View File

@ -375,6 +375,18 @@ Provides a better reading experience (full text articles) over the official one.
<RouteEn author="nczitzk" example="/mathunion/fields-medal" path="/mathunion/fields-medal"/>
## KBS
### Today
<RouteEn author="nczitzk" example="/kbs/today" path="/kbs/today/:language?" :paramsDesc="['Language, see below, e as English by default']">
| 한국어 | عربي | 中国语 | English | Français | Deutsch | Bahasa Indonesia | 日本語 | Русский | Español | Tiếng Việt |
| ------ | ---- | ------ | ------- | -------- | ------- | ---------------- | ------ | ------- | ------- | ---------- |
| k | a | c | e | f | g | i | j | r | s | v |
</RouteEn>
## Letterboxd
### User diary

View File

@ -597,6 +597,18 @@ Tag
<Route author="xyqfer" example="/itjuzi/merge" path="/itjuzi/merge"/>
## KBS
### Today
<Route author="nczitzk" example="/kbs/today" path="/kbs/today/:language?" :paramsDesc="['语言,见下表,默认为 e 即 English']">
| 한국어 | عربي | 中国语 | English | Français | Deutsch | Bahasa Indonesia | 日本語 | Русский | Español | Tiếng Việt |
| ------ | ---- | ------ | ------- | -------- | ------- | ---------------- | ------ | ------- | ------- | ---------- |
| k | a | c | e | f | g | i | j | r | s | v |
</Route>
## Kotaku
### Story

View File

@ -4254,4 +4254,7 @@ router.get('/fashionnetwork/headline/:country?', lazyloadRouteHandler('./routes/
// Deprecated: DO NOT ADD ROUTE HERE
// KBS
router.get('/kbs/today/:language?', lazyloadRouteHandler('./routes/kbs/today'));
module.exports = router;

56
lib/routes/kbs/today.js Normal file
View File

@ -0,0 +1,56 @@
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 || 'e';
const rootUrl = 'http://world.kbs.co.kr';
const currentUrl = `${rootUrl}/service/news_today.htm?lang=${language}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('.comp_text_1x article')
.map((_, item) => {
item = $(item);
const a = item.find('h2 a');
return {
title: a.text(),
category: item.find('.cate').text(),
link: `${rootUrl}/service${a.attr('href').replace('./', '/')}`,
pubDate: timezone(parseDate(item.find('.date').text()), +9),
};
})
.get();
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content('.body_txt').html();
return item;
})
)
);
ctx.state.data = {
title: `Latest News | KBS WORLD`,
link: currentUrl,
item: items,
};
};