feat(route): add eprice (#8247)

Co-authored-by: SettingDust <settingdust@gmail.com>
This commit is contained in:
Tony 2021-09-24 04:55:27 +05:00 committed by GitHub
parent 33de44df34
commit d8aa7357e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 138 additions and 0 deletions

View File

@ -224,6 +224,18 @@ pageClass: routes
</Route>
## ePrice
<Route author="TonyRL" example="/eprice/tw" path="/eprice/:region?" :paramsDesc="['地区,预设为 tw']">
地区:
| hk | tw |
| ---- | ---- |
| 香港 | 台湾 |
</Route>
## Esquirehk
### Tag

View File

@ -0,0 +1,3 @@
module.exports = {
'/:region?': ['TonyRL'],
};

24
lib/v2/eprice/radar.js Normal file
View File

@ -0,0 +1,24 @@
module.exports = {
'eprice.com.tw': {
_name: 'ePrice',
'.': [
{
title: 'ePrice 比價王',
docs: 'https://docs.rsshub.app/new-media.html#eprice',
source: ['/'],
target: '/:region?',
},
],
},
'eprice.com.hk': {
_name: 'ePrice',
'.': [
{
title: 'ePrice 香港',
docs: 'https://docs.rsshub.app/new-media.html#eprice',
source: ['/'],
target: '/:region?',
},
],
},
};

3
lib/v2/eprice/router.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/:region?', require('./rss'));
};

95
lib/v2/eprice/rss.js Normal file
View File

@ -0,0 +1,95 @@
const got = require('@/utils/got');
const parser = require('@/utils/rss-parser');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');
module.exports = async (ctx) => {
const region = ctx.params.region ?? 'tw';
const feed = await parser.parseURL(`https://www.eprice.com.${region}/news/rss.xml`);
feed.items.forEach((e) => {
e.link = e.link.replace(/^http:\/\//i, 'https://');
});
const renderDesc = (desc) =>
art(path.join(__dirname, 'templates/description.art'), {
desc,
});
const items = await Promise.all(
feed.items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const response = await got({
method: 'get',
url: item.link,
headers: {
Referer: `https://www.eprice.com.${region}`,
},
});
const $ = cheerio.load(response.data);
// remove unwanted elements
$('noscript').remove();
$('div.ad-336x280-g').remove();
$('div.ad-728x90-g').remove();
$('.adsbygoogle').remove();
$('.join-eprice-fb').remove();
$('div.clear').remove();
$('div.text-left').remove();
$('div.signature').remove();
$('div[id^=dablewidget]').remove();
// extract categories
item.category = item.categories;
// fix lazyload image
$('img').each((_, e) => {
e = $(e);
if (e.attr('data-original')) {
e.attr('src', e.attr('data-original'));
e.removeAttr('class');
e.removeAttr('data-original');
e.removeAttr('data-title');
e.removeAttr('data-thumbnail');
e.removeAttr('data-url');
}
});
// remove unwanted key value
delete item.categories;
delete item.content;
delete item.contentSnippet;
delete item.creator;
delete item.enclosure;
delete item.isoDate;
item.description = renderDesc($('div.user-comment-block').html() ?? $('div.content').html());
item.pubDate = parseDate(item.pubDate);
return item;
})
)
);
ctx.state.data = {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
image: feed.image.url,
language: feed.language,
};
ctx.state.json = {
title: feed.title,
link: feed.link,
description: feed.description,
item: items,
image: feed.image.url,
language: feed.language,
};
};

View File

@ -0,0 +1 @@
{{ desc }}