feat: fix tencent->video 更多链接支持 (#4563)

This commit is contained in:
talengu
2020-04-28 11:12:05 +08:00
committed by GitHub
parent 89aef16868
commit 1bb6cb51cf
3 changed files with 113 additions and 9 deletions
+4
View File
@@ -6,6 +6,10 @@ module.exports = async (ctx) => {
// 每周质量报告
const getMzzlbg = require('./utils/mzzlbg');
responseData = await getMzzlbg();
} else if (category === 'xinwen1j1') {
// 新闻1+1
const xinwen1j1 = require('./utils/xinwen1j1');
responseData = await xinwen1j1(category, ctx);
} else {
// 央视新闻
const getNews = require('./utils/news');
+79
View File
@@ -0,0 +1,79 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
//
// 测试http://localhost:1200/cctv/xinwen1j1
// 完整文章页
async function load(link) {
const res = await got({ method: 'get', url: link });
const $ = cheerio.load(res.data);
// console.log($('div.image').get().text())
// console.log('********')
const js_txt = '' + $('script');
const guid = js_txt.split('guid_Ad_VideoCode = "')[1].split('";')[0];
// console.log(guid+' js_txt********')
const { data: videoDetail } = await got({
method: 'get',
url: `http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid=${guid}`,
});
// poster="?" 以后再弄
// <video src="${videoDetail.hls_url}" controls="controls" poster="?" style="width: 100%"></video>
const description = `<video src="${videoDetail.hls_url}" controls="controls" style="width: 100%"></video>`;
// 提取内容
return { description };
}
// 同步 列表
const ProcessFeed = async (list, caches) =>
// host = "hostname"
await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('a').text();
const itemUrl = $('a').attr('href').split('?')[0];
const itemDate = new Date(itemUrl.split('/VI')[0].split('com/')[1]).toUTCString();
// 列表上提取到的信息
const single = {
title: title,
link: itemUrl,
author: '央视新闻',
guid: itemUrl,
pubDate: itemDate,
};
// 使用tryGet方法从缓存获取内容。
// 当缓存中无法获取到链接内容的时候,则使用load方法加载文章内容。
const other = await caches.tryGet(itemUrl, async () => await load(itemUrl));
// 合并解析后的结果集作为该篇文章最终的输出结果
return Promise.resolve(Object.assign({}, single, other));
})
);
module.exports = async (category, ctx) => {
const baseUrl = 'http://tv.cctv.com/lm/xinwen1j1/videoset/index.shtml';
// 获取要处理的页面
const res = await got({ method: 'get', url: baseUrl });
const $ = cheerio.load(res.data);
let list = $('div.text').slice(0, 30); // 取前30个
// $('div.image').slice(0,3).find('img').attr('src')
// $('div.image').slice(0,3).find('a').attr('href').split('?')[0]
list = list.get();
// 处理所有的页面
const result = await ProcessFeed(list, ctx.cache);
return {
title: '《新闻1+1》- 中央电视台新闻频道',
link: baseUrl,
description:
'《新闻1+1》是中央电视台新闻频道的一档访谈类栏目。周一至周五,从时事政策、公共话题、突发事件等大型选题中选取当天最新、最热、最快的新闻话题,还原新闻全貌、解读事件真相,力求以精度、纯度和锐度为新闻导向,呈现最质朴的新闻。首播:CCTV-新闻:周一至周五21:30—21:55;重播:次日01:30,04:30。',
item: result,
};
};
+30 -9
View File
@@ -6,15 +6,36 @@ module.exports = async (ctx) => {
const link = `https://v.qq.com/detail/${id[0]}/${id}.html`;
const page = await got.get(link);
const $ = cheerio.load(page.data);
const episodes = await got.get(`https://s.video.qq.com/get_playsource?id=${id}&plat=2&type=4&data_type=2&video_type=3&range=1&plname=qq&otype=json&num_mod_cnt=20&callback=a&_t=${Date.now()}`);
const playList = JSON.parse(episodes.data.slice(2, -1)).PlaylistItem.videoPlayList;
const items = playList.map((video) => ({
title: video.title,
link: video.playUrl,
description: `
<iframe frameborder="0" src="https://v.qq.com/txp/iframe/player.html?vid=${video.id}" allowFullScreen="true"></iframe>
`,
}));
let vtype = 4;
// 支持地址 https://v.qq.com/detail/8/84842.html
let items = null;
if (`${id[0]}` === '8') {
vtype = 1;
const episodes = await got.get(`https://s.video.qq.com/get_playsource?id=${id}&plat=2&type=${vtype}&data_type=2&video_type=3&range=1&plname=qq&otype=json&num_mod_cnt=20&callback=a&_t=${Date.now()}`);
const playList = JSON.parse(episodes.data.slice(2, -1)).playlist[0].videoPlayList;
items = playList.map(function (video) {
const video_id = video.playUrl.split('=')[1];
const info = {
title: video.title,
link: video.playUrl,
description: `
<iframe src="https://v.qq.com/txp/iframe/player.html?vid=${video_id}" allowFullScreen="true" width="640" height="430"></iframe>
`,
};
return info;
});
} else {
const episodes = await got.get(`https://s.video.qq.com/get_playsource?id=${id}&plat=2&type=${vtype}&data_type=2&video_type=3&range=1&plname=qq&otype=json&num_mod_cnt=20&callback=a&_t=${Date.now()}`);
const playList = JSON.parse(episodes.data.slice(2, -1)).PlaylistItem.videoPlayList;
items = playList.map((video) => ({
title: video.title,
link: video.playUrl,
description: `
<iframe src="https://v.qq.com/txp/iframe/player.html?vid=${video.id}" allowFullScreen="true" width="640" height="430"></iframe>
`,
}));
}
ctx.state.data = {
title: $('title').text(),