From d8aa7357e55d3ee20401d02bdde32a7742016388 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 24 Sep 2021 04:55:27 +0500 Subject: [PATCH] feat(route): add eprice (#8247) Co-authored-by: SettingDust --- docs/new-media.md | 12 ++++ lib/v2/eprice/maintainer.js | 3 + lib/v2/eprice/radar.js | 24 +++++++ lib/v2/eprice/router.js | 3 + lib/v2/eprice/rss.js | 95 +++++++++++++++++++++++++ lib/v2/eprice/templates/description.art | 1 + 6 files changed, 138 insertions(+) create mode 100644 lib/v2/eprice/maintainer.js create mode 100644 lib/v2/eprice/radar.js create mode 100644 lib/v2/eprice/router.js create mode 100644 lib/v2/eprice/rss.js create mode 100644 lib/v2/eprice/templates/description.art diff --git a/docs/new-media.md b/docs/new-media.md index 48db72c6d..8bf18c45f 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -224,6 +224,18 @@ pageClass: routes +## ePrice + + + +地区: + +| hk | tw | +| ---- | ---- | +| 香港 | 台湾 | + + + ## Esquirehk ### Tag diff --git a/lib/v2/eprice/maintainer.js b/lib/v2/eprice/maintainer.js new file mode 100644 index 000000000..d07fc4e93 --- /dev/null +++ b/lib/v2/eprice/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/:region?': ['TonyRL'], +}; diff --git a/lib/v2/eprice/radar.js b/lib/v2/eprice/radar.js new file mode 100644 index 000000000..d6e622a2f --- /dev/null +++ b/lib/v2/eprice/radar.js @@ -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?', + }, + ], + }, +}; diff --git a/lib/v2/eprice/router.js b/lib/v2/eprice/router.js new file mode 100644 index 000000000..c12c2da59 --- /dev/null +++ b/lib/v2/eprice/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/:region?', require('./rss')); +}; diff --git a/lib/v2/eprice/rss.js b/lib/v2/eprice/rss.js new file mode 100644 index 000000000..5552db831 --- /dev/null +++ b/lib/v2/eprice/rss.js @@ -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, + }; +}; diff --git a/lib/v2/eprice/templates/description.art b/lib/v2/eprice/templates/description.art new file mode 100644 index 000000000..fb76410af --- /dev/null +++ b/lib/v2/eprice/templates/description.art @@ -0,0 +1 @@ +{{ desc }}