From 728490130ee0b70d46ad17bd7ccdf4924605bbf9 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 28 May 2023 16:38:48 +0300 Subject: [PATCH] feat(route): followin (#12574) * feat(route): followin * feat: add radar * fix: fix news title --- docs/en/finance.md | 36 ++++++++++++ docs/finance.md | 36 ++++++++++++ lib/v2/followin/index.js | 35 ++++++++++++ lib/v2/followin/kol.js | 25 +++++++++ lib/v2/followin/maintainer.js | 7 +++ lib/v2/followin/news.js | 21 +++++++ lib/v2/followin/radar.js | 37 ++++++++++++ lib/v2/followin/router.js | 7 +++ lib/v2/followin/tag.js | 44 +++++++++++++++ lib/v2/followin/templates/thread.art | 15 +++++ lib/v2/followin/topic.js | 25 +++++++++ lib/v2/followin/utils.js | 84 ++++++++++++++++++++++++++++ 12 files changed, 372 insertions(+) create mode 100644 lib/v2/followin/index.js create mode 100644 lib/v2/followin/kol.js create mode 100644 lib/v2/followin/maintainer.js create mode 100644 lib/v2/followin/news.js create mode 100644 lib/v2/followin/radar.js create mode 100644 lib/v2/followin/router.js create mode 100644 lib/v2/followin/tag.js create mode 100644 lib/v2/followin/templates/thread.art create mode 100644 lib/v2/followin/topic.js create mode 100644 lib/v2/followin/utils.js diff --git a/docs/en/finance.md b/docs/en/finance.md index ad0321b7a..cf78e4196 100644 --- a/docs/en/finance.md +++ b/docs/en/finance.md @@ -53,6 +53,42 @@ pageClass: routes +## Followin + +### Home + + + +Category ID + +| For You | Market | Meme | BRC20 | NFT | Thread | In-depth | Tutorials | Videos | +| ------- | ------ | ---- | ----- | --- | ------ | -------- | --------- | ------ | +| 1 | 9 | 13 | 14 | 3 | 5 | 6 | 8 | 11 | + +Language + +| English | 简体中文 | 繁體中文 | Tiếng Việt | +| ------- | ------- | -------- | ---------- | +| en | zh-Hans | zh-Hant | vi | + + + +### News + + + +### KOL + + + +### Topic + + + +### Tag + + + ## Futubull ### Headlines diff --git a/docs/finance.md b/docs/finance.md index ea835c1af..20514f554 100644 --- a/docs/finance.md +++ b/docs/finance.md @@ -92,6 +92,42 @@ pageClass: routes +## Followin + +### 首页 + + + +分类 ID + +| 推荐 | 行情 | Meme | BRC20 | NFT | 长推 | 深度 | 入门 | 视频 | +| ---- | ---- | ---- | ----- | --- | ---- | ---- | ---- | ---- | +| 1 | 9 | 13 | 14 | 3 | 5 | 6 | 8 | 11 | + +语言 + +| English | 简体中文 | 繁體中文 | Tiếng Việt | +| ------- | -------- | -------- | ---------- | +| en | zh-Hans | zh-Hant | vi | + + + +### 快讯 + + + +### KOL + + + +### 话题 + + + +### 标签 + + + ## FX Markets ### 分类 diff --git a/lib/v2/followin/index.js b/lib/v2/followin/index.js new file mode 100644 index 000000000..930f38df7 --- /dev/null +++ b/lib/v2/followin/index.js @@ -0,0 +1,35 @@ +const got = require('@/utils/got'); +const { apiUrl, favicon, getBParam, getBuildId, getGToken, parseList, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const { categoryId = '1', lang = 'en' } = ctx.params; + const { limit = 20 } = ctx.query; + const gToken = await getGToken(ctx.cache.tryGet); + const bParam = getBParam(lang); + + const { data: response } = await got.post(`${apiUrl}/feed/list/recommended`, { + headers: { + 'x-bparam': JSON.stringify(bParam), + 'x-gtoken': gToken, + }, + json: { + category_id: parseInt(categoryId), + count: parseInt(limit), + }, + }); + if (response.code !== 2000) { + throw Error(response.msg); + } + + const buildId = await getBuildId(ctx.cache.tryGet); + + const list = parseList(response.data.list, lang, buildId); + const items = await Promise.all(list.map((item) => parseItem(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: 'Followin', + link: 'https://followin.io', + image: favicon, + item: items, + }; +}; diff --git a/lib/v2/followin/kol.js b/lib/v2/followin/kol.js new file mode 100644 index 000000000..42f9d5657 --- /dev/null +++ b/lib/v2/followin/kol.js @@ -0,0 +1,25 @@ +const got = require('@/utils/got'); +const { baseUrl, getBuildId, parseList, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const { kolId, lang = 'en' } = ctx.params; + const { limit = 10 } = ctx.query; + + const buildId = await getBuildId(ctx.cache.tryGet); + const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/kol/${kolId}.json`); + + const { queries } = response.pageProps.dehydratedState; + const { data: profile } = queries.find((q) => q.queryKey[0] === '/user/get_profile').state; + + const list = parseList(queries.find((q) => q.queryKey[0] === '/feed/list/user').state.data.pages[0].list.slice(0, limit), lang, buildId); + const items = await Promise.all(list.map((item) => parseItem(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `${profile.nickname} - Followin`, + description: profile.bio, + link: `${baseUrl}/${lang}/kol/${kolId}`, + image: profile.avatar, + language: lang, + item: items, + }; +}; diff --git a/lib/v2/followin/maintainer.js b/lib/v2/followin/maintainer.js new file mode 100644 index 000000000..78006607c --- /dev/null +++ b/lib/v2/followin/maintainer.js @@ -0,0 +1,7 @@ +module.exports = { + '/kol/:kolId/:lang?': ['TonyRL'], + '/news/:lang?': ['TonyRL'], + '/tag/:tagId/:lang?': ['TonyRL'], + '/topic/:topicId/:lang?': ['TonyRL'], + '/:categoryId?/:lang?': ['TonyRL'], +}; diff --git a/lib/v2/followin/news.js b/lib/v2/followin/news.js new file mode 100644 index 000000000..917cbf658 --- /dev/null +++ b/lib/v2/followin/news.js @@ -0,0 +1,21 @@ +const got = require('@/utils/got'); +const { baseUrl, favicon, getBuildId, parseList, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const { lang = 'en' } = ctx.params; + const { limit = 20 } = ctx.query; + + const buildId = await getBuildId(ctx.cache.tryGet); + const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/news.json`); + + const list = parseList(response.pageProps.dehydratedState.queries.find((q) => q.queryKey[0] === '/feed/list/recommended/news').state.data.pages[0].list.slice(0, limit), lang, buildId); + const items = await Promise.all(list.map((item) => parseItem(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `${lang === 'en' ? 'News' : lang === 'vi' ? 'Bản tin' : '快讯'} - Followin`, + link: `${baseUrl}/${lang}/news`, + image: favicon, + language: lang, + item: items, + }; +}; diff --git a/lib/v2/followin/radar.js b/lib/v2/followin/radar.js new file mode 100644 index 000000000..167cca076 --- /dev/null +++ b/lib/v2/followin/radar.js @@ -0,0 +1,37 @@ +module.exports = { + 'followin.io': { + _name: 'Followin', + '.': [ + { + title: '首页', + docs: 'https://docs.rsshub.app/finance.html#followin', + source: ['/:lang'], + target: (params) => (params.lang !== 'news' ? '/followin/1/:lang' : ''), + }, + { + title: '快讯', + docs: 'https://docs.rsshub.app/finance.html#followin', + source: ['/:lang?/news', '/news'], + target: '/followin/news/:lang?', + }, + { + title: 'KOL', + docs: 'https://docs.rsshub.app/finance.html#followin', + source: ['/:lang/kol/:kolId', '/kol/:kolId'], + target: '/followin/kol/:kolId/:lang?', + }, + { + title: '话题', + docs: 'https://docs.rsshub.app/finance.html#followin', + source: ['/:lang/topic/:topicId', '/topic/:topicId'], + target: '/followin/topic/:topicId/:lang?', + }, + { + title: '标签', + docs: 'https://docs.rsshub.app/finance.html#followin', + source: ['/:lang/tag/:tagId', '/tag/:tagId'], + target: '/followin/tag/:tagId/:lang?', + }, + ], + }, +}; diff --git a/lib/v2/followin/router.js b/lib/v2/followin/router.js new file mode 100644 index 000000000..cd3ea5ba1 --- /dev/null +++ b/lib/v2/followin/router.js @@ -0,0 +1,7 @@ +module.exports = (router) => { + router.get('/kol/:kolId/:lang?', require('./kol')); + router.get('/news/:lang?', require('./news')); + router.get('/tag/:tagId/:lang?', require('./tag')); + router.get('/topic/:topicId/:lang?', require('./topic')); + router.get('/:categoryId?/:lang?', require('./index')); +}; diff --git a/lib/v2/followin/tag.js b/lib/v2/followin/tag.js new file mode 100644 index 000000000..7283a57c7 --- /dev/null +++ b/lib/v2/followin/tag.js @@ -0,0 +1,44 @@ +const got = require('@/utils/got'); +const { apiUrl, baseUrl, getBParam, getBuildId, getGToken, parseList, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const { tagId, lang = 'en' } = ctx.params; + const { limit = 20 } = ctx.query; + + const buildId = await getBuildId(ctx.cache.tryGet); + const tagInfo = await ctx.cache.tryGet(`followin:tag:${tagId}:${lang}`, async () => { + const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/tag/${tagId}.json`); + const { queries } = response.pageProps.dehydratedState; + const { base_info: tagInfo } = queries.find((q) => q.queryKey[0] === '/tag/info/v2').state.data; + return tagInfo; + }); + + const gToken = await getGToken(ctx.cache.tryGet); + const bParam = getBParam(lang); + const { data: tagResponse } = await got.post(`${apiUrl}/feed/list/tag`, { + headers: { + 'x-bparam': JSON.stringify(bParam), + 'x-gtoken': gToken, + }, + json: { + count: limit, + id: parseInt(tagId), + type: 'tag_discussion_feed', + }, + }); + if (tagResponse.code !== 2000) { + throw Error(tagResponse.msg); + } + + const list = parseList(tagResponse.data.list.slice(0, limit), lang, buildId); + const items = await Promise.all(list.map((item) => parseItem(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `${tagInfo.name} - Followin`, + description: tagInfo.description, + link: `${baseUrl}/${lang}/tag/${tagId}`, + image: tagInfo.logo, + language: lang, + item: items, + }; +}; diff --git a/lib/v2/followin/templates/thread.art b/lib/v2/followin/templates/thread.art new file mode 100644 index 000000000..22f9b8f24 --- /dev/null +++ b/lib/v2/followin/templates/thread.art @@ -0,0 +1,15 @@ +{{ each list l }} + {{@ l.translated_content.replaceAll('\n', '
') || l.content.replaceAll('\n', '
') }} +
+ {{ if l.images.length }} + {{ each l.images img }} + + {{ /each }} + {{ /if }} + {{ if l.link_previews.length }} + {{ each l.link_previews a }} + {{ a.title }}
{{ a.content }}
+ {{ /each }} + {{ /if }} +
+{{ /each }} diff --git a/lib/v2/followin/topic.js b/lib/v2/followin/topic.js new file mode 100644 index 000000000..54a63136c --- /dev/null +++ b/lib/v2/followin/topic.js @@ -0,0 +1,25 @@ +const got = require('@/utils/got'); +const { baseUrl, getBuildId, parseList, parseItem } = require('./utils'); + +module.exports = async (ctx) => { + const { topicId, lang = 'en' } = ctx.params; + const { limit = 20 } = ctx.query; + + const buildId = await getBuildId(ctx.cache.tryGet); + const { data: response } = await got(`${baseUrl}/_next/data/${buildId}/${lang}/topic/${topicId}.json`); + + const { queries } = response.pageProps.dehydratedState; + const { data: topicInfo } = queries.find((q) => q.queryKey[0] === '/topic/info').state; + + const list = parseList(queries.find((q) => q.queryKey[0] === '/feed/list/topic').state.data.pages[0].list.slice(0, limit), lang, buildId); + const items = await Promise.all(list.map((item) => parseItem(item, ctx.cache.tryGet))); + + ctx.state.data = { + title: `${topicInfo.title} - Followin`, + description: topicInfo.desc, + link: `${baseUrl}/${lang}/topic/${topicId}`, + image: topicInfo.logo, + language: lang, + item: items, + }; +}; diff --git a/lib/v2/followin/utils.js b/lib/v2/followin/utils.js new file mode 100644 index 000000000..a5aed75f7 --- /dev/null +++ b/lib/v2/followin/utils.js @@ -0,0 +1,84 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const config = require('@/config').value; +const { art } = require('@/utils/render'); +const { join } = require('path'); + +const apiUrl = 'https://api.followin.io'; +const baseUrl = 'https://followin.io'; +const favicon = `${baseUrl}/favicon.ico`; + +const getBParam = (lang) => ({ + a: 'web', + b: '', + c: lang, + d: 0, + e: '', + f: '', + g: '', + h: '0.1.0', + i: 'official', +}); + +const getBuildId = (tryGet) => + tryGet( + 'followin:buildId', + async () => { + const { data: pageResponse } = await got(baseUrl); + const $ = cheerio.load(pageResponse); + const { buildId } = JSON.parse($('script#__NEXT_DATA__').text()); + return buildId; + }, + config.cache.routeExpire, + false + ); + +const getGToken = (tryGet) => + tryGet('followin:gtoken', async () => { + const { data } = await got.post(`${apiUrl}/user/gtoken`); + return data.data.gtoken; + }); + +const parseList = (list, lang, buildId) => + list.map((item) => ({ + title: item.translated_title || item.title, + description: item.translated_content || item.content, + link: `${baseUrl}/${lang === 'en' ? '' : `${lang}/`}feed/${item.id}`, + pubDate: parseDate(item.publish_time, 'x'), + category: item.tags.map((tag) => tag.name), + author: item.nickname, + nextData: `${baseUrl}/_next/data/${buildId}/${lang}/feed/${item.id}.json`, + })); + +const parseItem = (item, tryGet) => + tryGet(item.link, async () => { + const { data } = await got(item.nextData); + + const { queries } = data.pageProps.dehydratedState; + const info = queries.find((q) => q.queryKey[0] === '/feed/info').state; + const thread = queries.find((q) => q.queryKey[0] === '/feed/thread'); + if (thread) { + item.description = art(join(__dirname, 'templates/thread.art'), { + list: thread.state.data.list, + }); + } else { + item.description = info.data.translated_full_content || info.data.full_content; + } + + item.updated = parseDate(info.dataUpdatedAt, 'x'); + item.category = [...new Set([...item.category, ...info.data.tags.map((tag) => tag.name)])]; + + return item; + }); + +module.exports = { + apiUrl, + baseUrl, + favicon, + getBParam, + getBuildId, + getGToken, + parseList, + parseItem, +};