diff --git a/lib/v2/mingpao/index.js b/lib/v2/mingpao/index.js index 133b66623c..7b82c3a792 100644 --- a/lib/v2/mingpao/index.js +++ b/lib/v2/mingpao/index.js @@ -5,6 +5,17 @@ const { parseDate } = require('@/utils/parse-date'); const { art } = require('@/utils/render'); const path = require('path'); +const renderFanBox = (media) => + art(path.join(__dirname, 'templates/fancybox.art'), { + media, + }); + +const renderDesc = (media, desc) => + art(path.join(__dirname, 'templates/description.art'), { + media: renderFanBox(media), + desc, + }); + module.exports = async (ctx) => { const type = ctx.params.type ?? 'ins'; const category = ctx.params.category ?? (type === 'ins' ? 'all' : 's00001'); @@ -12,23 +23,10 @@ module.exports = async (ctx) => { const feed = await parser.parseURL(link); - const renderFanBox = (pics) => - art(path.join(__dirname, 'templates/fancybox.art'), { - pics, - }); - - const renderDesc = (pics, desc) => - art(path.join(__dirname, 'templates/description.art'), { - pics: renderFanBox(pics), - 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, + const response = await got(item.link, { headers: { Referer: 'https://news.mingpao.com/', }, @@ -48,15 +46,21 @@ module.exports = async (ctx) => { item.category = item.categories; // fix fancybox image - const fancybox = fancyboxImg - .map((_, e) => { - e = $(e); - return { - href: e.attr('href'), - title: e.attr('title'), - }; - }) - .get(); + const fancybox = fancyboxImg.toArray().map((e) => { + e = $(e); + const href = new URL(e.attr('href')); + let video; + if (href.hostname === 'videop.mingpao.com') { + video = new URL(href.searchParams.get('file')); + video.hostname = 'cfrvideo.mingpao.com'; // use cloudflare cdn + video = video.href; + } + return { + href: href.href, + title: e.attr('title'), + video, + }; + }); // remove unwanted key value delete item.categories; @@ -65,7 +69,7 @@ module.exports = async (ctx) => { delete item.creator; delete item.isoDate; - item.description = renderDesc(fancybox, $('.txt4').html() ?? $('div.article_content.line_1_5em').html()); + item.description = renderDesc(fancybox, $('.txt4').html() ?? $('.article_content.line_1_5em').html()); item.pubDate = parseDate(item.pubDate); item.guid = item.link.indexOf('?') > 0 ? item.link : item.link.substring(0, item.link.lastIndexOf('/')); @@ -82,13 +86,4 @@ module.exports = async (ctx) => { 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/mingpao/templates/description.art b/lib/v2/mingpao/templates/description.art index 7a0e7673e9..3439085cd5 100644 --- a/lib/v2/mingpao/templates/description.art +++ b/lib/v2/mingpao/templates/description.art @@ -1,2 +1,2 @@ -{{@ pics }} +{{@ media }} {{@ desc }} diff --git a/lib/v2/mingpao/templates/fancybox.art b/lib/v2/mingpao/templates/fancybox.art index 3be1bf3dda..430d1c40f1 100644 --- a/lib/v2/mingpao/templates/fancybox.art +++ b/lib/v2/mingpao/templates/fancybox.art @@ -1,3 +1,7 @@ -{{ each pics }} -
{{ $value.title }}
{{ $value.title }}
+{{ each media }} + {{ if $value.video }} + + {{ else }} +
{{ $value.title }}
{{ $value.title }}
+ {{ /if }} {{ /each }}