fix(route): mingpao video (#11539)

This commit is contained in:
Tony
2023-01-02 23:07:22 +08:00
committed by GitHub
parent d2ab8ab3c0
commit f7925551ec
3 changed files with 35 additions and 36 deletions
+28 -33
View File
@@ -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,
};
};
+1 -1
View File
@@ -1,2 +1,2 @@
{{@ pics }}
{{@ media }}
{{@ desc }}
+6 -2
View File
@@ -1,3 +1,7 @@
{{ each pics }}
<figure><img src="{{ $value.href }}" alt="{{ $value.title }}"><figcaption>{{ $value.title }}</figcaption></figure>
{{ each media }}
{{ if $value.video }}
<video controls preload="none" poster="{{ $value.video.replace('.mp4', '.jpg') }}"><source src="{{ $value.video }}" type="video/mp4"></video>
{{ else }}
<figure><img src="{{ $value.href }}" alt="{{ $value.title }}"><figcaption>{{ $value.title }}</figcaption></figure>
{{ /if }}
{{ /each }}