From 2673fae330bff956f08623cea7093282bfb26a00 Mon Sep 17 00:00:00 2001 From: Fatpandac <1779196284@qq.com> Date: Sun, 23 Jan 2022 22:42:08 +0800 Subject: [PATCH] =?UTF-8?q?Add(route):=20add=20=E9=93=BE=E6=96=B0=E9=97=BB?= =?UTF-8?q?abmedia=20(#8862)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: DIYgod --- docs/new-media.md | 16 ++++++++++++++++ lib/v2/abmedia/category.js | 30 ++++++++++++++++++++++++++++++ lib/v2/abmedia/index.js | 26 ++++++++++++++++++++++++++ lib/v2/abmedia/maintainer.js | 3 +++ lib/v2/abmedia/radar.js | 19 +++++++++++++++++++ lib/v2/abmedia/router.js | 4 ++++ 6 files changed, 98 insertions(+) create mode 100644 lib/v2/abmedia/category.js create mode 100644 lib/v2/abmedia/index.js create mode 100644 lib/v2/abmedia/maintainer.js create mode 100644 lib/v2/abmedia/radar.js create mode 100644 lib/v2/abmedia/router.js diff --git a/docs/new-media.md b/docs/new-media.md index f366f589e0..76e19a0ad5 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -2057,6 +2057,22 @@ column 为 third 时可选的 category: +## 链新闻 ABMedia + +### 首页最新新闻 + + + +### 类别 + + + +参数可以从链接中拿到,如: + +`https://www.abmedia.io/category/technology-development` 对应 `/abmedia/technology-development` + + + ## 雷峰网 ### 最新文章 diff --git a/lib/v2/abmedia/category.js b/lib/v2/abmedia/category.js new file mode 100644 index 0000000000..4eb435f250 --- /dev/null +++ b/lib/v2/abmedia/category.js @@ -0,0 +1,30 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +const rootUrl = 'https://www.abmedia.io'; +const cateAPIUrl = `${rootUrl}/wp-json/wp/v2/categories`; +const postsAPIUrl = `${rootUrl}/wp-json/wp/v2/posts`; + +const getCategoryId = async (category) => await got.get(`${cateAPIUrl}?slug=${category}`).then((res) => res.data[0].id); + +module.exports = async (ctx) => { + const category = ctx.params.category ?? 'technology-development'; + const limit = ctx.params.limit ?? 25; + const categoryId = await getCategoryId(category); + + const response = await got.get(`${postsAPIUrl}?categories=${categoryId}&page=1&per_page=${limit}`); + const data = response.data; + + const items = data.map((item) => ({ + title: item.title.rendered, + link: item.link, + description: item.content.rendered, + pubDate: parseDate(item.date), + })); + + ctx.state.data = { + title: `abmedia - ${category}`, + link: `${rootUrl}/category/${category}`, + item: items, + }; +}; diff --git a/lib/v2/abmedia/index.js b/lib/v2/abmedia/index.js new file mode 100644 index 0000000000..e6d5d341f3 --- /dev/null +++ b/lib/v2/abmedia/index.js @@ -0,0 +1,26 @@ +const got = require('@/utils/got'); +const { parseDate } = require('@/utils/parse-date'); + +const rootUrl = 'https://www.abmedia.io'; +const postsAPIUrl = `${rootUrl}/wp-json/wp/v2/posts`; + +module.exports = async (ctx) => { + const limit = ctx.params.limit ?? 10; + const url = `${postsAPIUrl}?per_page=${limit}`; + + const response = await got.get(url); + const data = response.data; + + const items = data.map((item) => ({ + title: item.title.rendered, + link: item.link, + description: item.content.rendered, + pubDate: parseDate(item.date), + })); + + ctx.state.data = { + title: 'ABMedia - 最新消息', + link: rootUrl, + item: items, + }; +}; diff --git a/lib/v2/abmedia/maintainer.js b/lib/v2/abmedia/maintainer.js new file mode 100644 index 0000000000..0d1cfb442b --- /dev/null +++ b/lib/v2/abmedia/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/abmedia/:category?': ['Fatpandac'], +}; diff --git a/lib/v2/abmedia/radar.js b/lib/v2/abmedia/radar.js new file mode 100644 index 0000000000..890afc8a98 --- /dev/null +++ b/lib/v2/abmedia/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'abmedia.io': { + _name: 'abmedia.io', + www: [ + { + title: '首页最新新闻', + docs: 'https://docs.rsshub.app/new-media.html#lian-xin-wen-abmedia-shou-ye-zui-xin-xin-wen', + source: ['/'], + target: '/abmedia/index', + }, + { + title: '频道', + docs: 'https://docs.rsshub.app/new-media.html#lian-xin-wen-abmedia-lei-bie', + source: ['/category/:catehory'], + target: '/abmedia/:category', + }, + ], + }, +}; diff --git a/lib/v2/abmedia/router.js b/lib/v2/abmedia/router.js new file mode 100644 index 0000000000..7d86e97427 --- /dev/null +++ b/lib/v2/abmedia/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/index', require('./index')); + router.get('/:category?', require('./category')); +};