From 8e3a4ac7a8082b97123e71f7c0f377710d1f4e83 Mon Sep 17 00:00:00 2001 From: eternasuno Date: Wed, 22 Mar 2023 21:16:26 +0900 Subject: [PATCH] feat(route): add gcores podcast (#12150) * feat(route): add gcores podcast * feat(route): add gcores podcast * Update lib/v2/gcores/maintainer.js * Update lib/v2/gcores/router.js * [fix] radar.js * [fix] radar.js * Update lib/v2/gcores/radio.js * Update docs/new-media.md --------- --- docs/new-media.md | 4 ++ lib/v2/gcores/maintainer.js | 1 + lib/v2/gcores/radar.js | 12 ++++ lib/v2/gcores/radio.js | 90 +++++++++++++++++++++++++++++ lib/v2/gcores/router.js | 1 + lib/v2/gcores/templates/content.art | 16 +++++ 6 files changed, 124 insertions(+) create mode 100644 lib/v2/gcores/radio.js create mode 100644 lib/v2/gcores/templates/content.art diff --git a/docs/new-media.md b/docs/new-media.md index e061e16fe..a69340644 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2540,6 +2540,10 @@ others = 热点新闻 + 滚动新闻 +### 播客 + + + ## 加美财经 diff --git a/lib/v2/gcores/maintainer.js b/lib/v2/gcores/maintainer.js index f1842790b..59e6ee703 100644 --- a/lib/v2/gcores/maintainer.js +++ b/lib/v2/gcores/maintainer.js @@ -1,4 +1,5 @@ module.exports = { '/category/:category': ['MoguCloud', 'StevenRCE0'], + '/radios/:category?': ['eternasuno'], '/tag/:tag/:category?': ['StevenRCE0'], }; diff --git a/lib/v2/gcores/radar.js b/lib/v2/gcores/radar.js index 40bfcdb52..b05708148 100644 --- a/lib/v2/gcores/radar.js +++ b/lib/v2/gcores/radar.js @@ -14,6 +14,18 @@ module.exports = { source: ['/categories/:tag', '/'], target: '/gcores/tag/:tag', }, + { + title: '播客', + docs: 'https://docs.rsshub.app/new-media.html#ji-he-wang-bo-ke', + source: ['/radios'], + target: '/gcores/radios', + }, + { + title: '播客-分类', + docs: 'https://docs.rsshub.app/new-media.html#ji-he-wang-bo-ke', + source: ['/categories/:category'], + target: '/gcores/radios/:category', + }, ], }, }; diff --git a/lib/v2/gcores/radio.js b/lib/v2/gcores/radio.js new file mode 100644 index 000000000..ed921dc52 --- /dev/null +++ b/lib/v2/gcores/radio.js @@ -0,0 +1,90 @@ +const { art } = require('@/utils/render'); +const { parseDate } = require('@/utils/parse-date'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const md5 = require('@/utils/md5'); +const path = require('path'); + +module.exports = async (ctx) => { + const category = ctx.params.category || 'all'; + const limit = parseInt(ctx.query.limit) || 12; + + const link = getLink(category); + const $ = cheerio.load(await get(link)); + const title = $('head>title').text(); + const description = $('head>meta[name="description"]').attr('content'); + const image = $('head>link[rel="apple-touch-icon"]').attr('href'); + + const api = getApi(category); + api.searchParams.set('include', 'media'); + api.searchParams.set('page[limit]', limit); + api.searchParams.set('sort', '-published-at'); + api.searchParams.set('filter[list-all]', '0'); + api.searchParams.set('fields[radios]', 'title,cover,published-at,duration,content,media'); + const { data, included } = await get(api); + + const audios = included.reduce((result, media) => { + result[media.id] = media.attributes.audio; + return result; + }, {}); + + const item = data.map((radio) => { + const { id, attributes, relationships } = radio; + + const link = `https://www.gcores.com/radios/${id}`; + const itunes_item_image = `https://image.gcores.com/${attributes.cover}`; + const media_id = relationships.media.data.id; + const enclosure_url = new URL(audios[media_id], 'https://alioss.gcores.com/uploads/audio/').toString(); + const description = art(path.join(__dirname, 'templates/content.art'), { + content: JSON.parse(attributes.content), + }); + + return { + title: attributes.title, + author: '机核 GCORES', + description, + pubDate: parseDate(attributes['published-at']), + guid: md5(link), + link, + itunes_item_image, + itunes_duration: attributes.duration, + enclosure_url, + enclosure_type: 'audio/mpeg', + }; + }); + + ctx.state.data = { + title, + link, + description, + language: 'zh-cn', + itunes_author: '机核 GCORES', + image: `https://www.gcores.com/${image}`, + item, + }; +}; + +const get = async (url) => { + const response = await got({ + method: 'get', + url: new URL(url, 'https://www.gcores.com'), + }); + + return response.data; +}; + +const getLink = (category) => { + if (category === 'all') { + return 'https://www.gcores.com/radios'; + } else { + return `https://www.gcores.com/categories/${category}`; + } +}; + +const getApi = (category) => { + if (category === 'all') { + return new URL('https://www.gcores.com/gapi/v1/radios'); + } else { + return new URL(`https://www.gcores.com/gapi/v1/categories/${category}/radios`); + } +}; diff --git a/lib/v2/gcores/router.js b/lib/v2/gcores/router.js index 4cf4aa275..cf555d2e5 100644 --- a/lib/v2/gcores/router.js +++ b/lib/v2/gcores/router.js @@ -1,4 +1,5 @@ module.exports = function (router) { router.get('/category/:category', require('./category')); + router.get('/radios/:category?', require('./radio')); router.get('/tag/:tag/:category?', require('./tag')); }; diff --git a/lib/v2/gcores/templates/content.art b/lib/v2/gcores/templates/content.art new file mode 100644 index 000000000..5cce07e6b --- /dev/null +++ b/lib/v2/gcores/templates/content.art @@ -0,0 +1,16 @@ +{{if content}} + {{each content.blocks line}} + {{if line.type === 'unstyled'}} +

{{line.text}}

+ {{/if}} + {{/each}} + + {{each content.entityMap ent}} + {{if ent.type === 'WIDGET'}} +

{{ent.data.title}}

+ {{/if}} + {{/each}} +{{else}} +

机核从2010年开始一直致力于分享游戏玩家的生活,以及深入探讨游戏相关的文化。我们开发原创的播客以及视频节目,一直在不断寻找民间高质量的内容创作者。

+

我们坚信游戏不止是游戏,游戏中包含的科学,文化,历史等各个层面的知识和故事,它们同时也会辐射到二次元甚至电影的领域,这些内容非常值得分享给热爱游戏的您。

+{{/if}}