From 1bb6cb51cfe2c85b5a0ca2e8531172681907aa59 Mon Sep 17 00:00:00 2001 From: talengu Date: Tue, 28 Apr 2020 11:12:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20fix=20tencent->video=20=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E9=93=BE=E6=8E=A5=E6=94=AF=E6=8C=81=20(#4563)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/cctv/category.js | 4 ++ lib/routes/cctv/utils/xinwen1j1.js | 79 ++++++++++++++++++++++++++++ lib/routes/tencent/video/playlist.js | 39 ++++++++++---- 3 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 lib/routes/cctv/utils/xinwen1j1.js diff --git a/lib/routes/cctv/category.js b/lib/routes/cctv/category.js index 9ef2050d8..d0e268cf1 100644 --- a/lib/routes/cctv/category.js +++ b/lib/routes/cctv/category.js @@ -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'); diff --git a/lib/routes/cctv/utils/xinwen1j1.js b/lib/routes/cctv/utils/xinwen1j1.js new file mode 100644 index 000000000..488ad4aaf --- /dev/null +++ b/lib/routes/cctv/utils/xinwen1j1.js @@ -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="?" 以后再弄 + // + const description = ``; + + // 提取内容 + 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, + }; +}; diff --git a/lib/routes/tencent/video/playlist.js b/lib/routes/tencent/video/playlist.js index 3ecdc378b..4cafd3f56 100644 --- a/lib/routes/tencent/video/playlist.js +++ b/lib/routes/tencent/video/playlist.js @@ -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: ` - - `, - })); + + 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: ` + + `, + }; + 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: ` + + `, + })); + } ctx.state.data = { title: $('title').text(),