Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add hket #8050

Merged
merged 1 commit into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,22 @@ category 对应的关键词有

</Route>

## 香港经济日报

### 新闻

香港经济日报已有提供简单 RSS,详细可前往官方网站: <https://www.hket.com/rss>

此路由主要补全官方 RSS 全文输出。

<Route author="TonyRL" example="/hket/sran001" path="/hket/:category?" :paramsDesc="['分类,默认为全部新闻']">

| sran001 | sran008 | sran010 | sran011 | srac002 | srat006 |
| -------- | -------- | -------- | -------- | -------- | -------- |
| 全部新闻 | 财经地产 | 科技信息 | 国际新闻 | 两岸新闻 | 香港新闻 |

</Route>

## 香港商报

### PDF 版
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4231,4 +4231,7 @@ router.get('/right/forum/:id?', lazyloadRouteHandler('./routes/right/forum'));
// 生物探索
router.get('/biodiscover/:channel?', lazyloadRouteHandler('./routes/biodiscover'));

// 香港經濟日報
router.get('/hket/:category?', lazyloadRouteHandler('./routes/hket/index'));

module.exports = router;
129 changes: 129 additions & 0 deletions lib/routes/hket/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');

const categories = {
sran001: {
baseUrl: `https://inews.hket.com`,
link: `https://inews.hket.com/sran001/全部`,
title: '全部',
},
sran008: {
baseUrl: `https://inews.hket.com`,
description: '財經新聞, 地產資訊',
link: `https://inews.hket.com/sran008/財金`,
title: '財經地產',
},
sran010: {
baseUrl: `https://inews.hket.com`,
description: '科技資訊',
link: `https://inews.hket.com/sran010/科技`,
title: '科技',
},
sran011: {
baseUrl: `https://inews.hket.com`,
description: '國際形勢',
link: `https://inews.hket.com/sran011/國際`,
title: '國際',
},
srac002: {
baseUrl: `https://china.hket.com`,
description: '中國及台灣新聞',
link: `https://china.hket.com/srac002/即時中國`,
title: '兩岸',
},
srat006: {
baseUrl: `https:///topick.hket.com`,
description: '香港新聞, 時事',
link: `https://topick.hket.com/srat006/新聞`,
title: '香港',
},
};

module.exports = async (ctx) => {
const category = ctx.params.category || 'sran001';
const cat = categories[category];

const response = await ctx.cache.tryGet(`${cat.link}`, async () => {
const resp = await got({
method: 'get',
url: `${cat.link}`,
header: {
Referer: `https://www.hket.com/`,
},
});
return resp.data;
});

const $ = cheerio.load(response);

const list = $('div.listing-title > a')
.map((_, item) => {
item = $(item);
return {
title: item.text().trim(),
link: item.attr('href').startsWith('/')
? // remove tracking parameters
cat.baseUrl + item.attr('href').split('?')[0].substring(0, item.attr('href').lastIndexOf('/'))
: item.attr('href').split('?')[0].substring(0, item.attr('href').lastIndexOf('/')),
};
})
.get();

const items = await Promise.all(
list &&
list.map((_, item) =>
ctx.cache.tryGet(item.link, async () => {
const article = await got({
method: 'get',
url: item.link,
header: {
Referer: `${cat.link}`,
},
});
const content = cheerio.load(article.data);
const categories = [];

// extract categories
content('.contentTags-container > .hotkey-container-wrapper > .hotkey-container > a').each((_, e) => {
categories.push(content(e).text().trim());
});

// remove unwanted elements
content('#ad_MobileInArticle, #ad_MobileMain, #Native01, #Native02, #Native03').remove();
content('div.template-default.hket-row.detail-widget.show-xs-img.relatedContents-container').remove();
content('div.template-default.hket-row.no-padding.detail-widget').remove();
content('div.contentTags-container').remove();
content('div.gallery-related-container').remove();
content('div.article-details-center-sharing-btn').remove();
content('source').remove();
content('span').each((_, e) => {
if (content(e).text().startsWith('+')) {
content(e).remove();
}
});

// fix lazyload image
content('img').each((_, e) => {
content(e).after(`<img src="${content(e).attr('data-src')}" alt="${content(e).attr('data-alt')}">`);
content(e).remove();
});

item.category = categories;
item.description = content('div.article-detail-body-container').html();
item.pubDate = timezone(parseDate(content('.article-details-info-container_date').text().trim()), +8);

return item;
})
)
);

ctx.state.data = {
title: `${cat.title}新聞RSS - 香港經濟日報 hket.com`,
link: `${cat.link}`,
description: `訂閱${cat.title}新聞RSS,獲取最新${cat.description} - RSS - 香港經濟日報 hket.com`,
item: items,
language: 'zh-hk',
};
};