diff --git a/lib/routes/taoguba/blog.ts b/lib/routes/taoguba/blog.ts index 6d2c085f7e..6b15d56a5f 100644 --- a/lib/routes/taoguba/blog.ts +++ b/lib/routes/taoguba/blog.ts @@ -1,12 +1,10 @@ import { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; -import timezone from '@/utils/timezone'; -import { parseDate } from '@/utils/parse-date'; +import { rootUrl, renderPostDetail } from './util'; export const route: Route = { - path: ['/blog/:id', '/user/:id'], + path: '/blog/:id', categories: ['finance'], example: '/taoguba/blog/252069', parameters: { id: '博客 id,可在对应博客页中找到' }, @@ -20,7 +18,7 @@ export const route: Route = { }, radar: [ { - source: ['taoguba.com.cn/blog/:id', 'taoguba.com.cn/'], + source: ['tgb.cn/blog/:id', 'tgb.cn/'], }, ], name: '用户博客', @@ -31,7 +29,6 @@ export const route: Route = { async function handler(ctx) { const id = ctx.req.param('id'); - const rootUrl = 'https://www.taoguba.com.cn'; const currentUrl = `${rootUrl}/blog/${id}`; const response = await got({ @@ -51,53 +48,13 @@ async function handler(ctx) { const a = item.find('a').first(); return { - title: a.text(), + title: a.text().trim(), link: `${rootUrl}/${a.attr('href')}`, author, }; }); - items = await Promise.all( - items.map((item) => - cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - if (detailResponse.url?.startsWith('https://www.taoguba.com.cn/topic/transfer')) { - item.description = '登录后查看完整文章'; - return item; - } - - const content = load(detailResponse.data); - - content('#videoImg').remove(); - content('img').each((_, img) => { - if (img.attribs.src2) { - img.attribs.src = img.attribs.src2; - delete img.attribs.src2; - delete img.attribs['data-original']; - } - }); - - item.description = content('#first').html(); - item.pubDate = timezone( - parseDate( - content('.article-data span') - .eq(1) - .text() - .match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/) - ), - +8 - ); - item.category = content('.article-topic-list span') - .toArray() - .map((item) => $(item).text().trim()); - - return item; - }) - ) - ); + items = await Promise.all(items.map(async (item) => await renderPostDetail(item))); return { title: `淘股吧 - ${author}`, diff --git a/lib/routes/taoguba/index.ts b/lib/routes/taoguba/index.ts index aabf2e6f5b..8d475dfd2d 100644 --- a/lib/routes/taoguba/index.ts +++ b/lib/routes/taoguba/index.ts @@ -1,13 +1,14 @@ import { Route } from '@/types'; -import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; -import timezone from '@/utils/timezone'; -import { parseDate } from '@/utils/parse-date'; +import { rootUrl, renderPostDetail } from './util'; export const route: Route = { - path: ['/index', '/:category?'], - name: 'Unknown', + path: '/:category?', + categories: ['finance'], + example: '/taoguba', + parameters: { id: '分类,见下表,默认为社区总版' }, + name: '淘股论坛', maintainers: ['nczitzk'], handler, description: `| 淘股论坛 | 社区总版 | 精华加油 | 网友点赞 | @@ -18,7 +19,6 @@ export const route: Route = { async function handler(ctx) { const category = ctx.req.param('category') ?? 'zongban'; - const rootUrl = 'https://www.taoguba.com.cn'; const currentUrl = `${rootUrl}/${category}/`; const response = await got({ @@ -28,63 +28,22 @@ async function handler(ctx) { const $ = load(response.data); - let items = $('.items-comment-list') + let items = $('.Nbbs-tiezi-lists') .slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 70) .toArray() .map((item) => { item = $(item); - const a = item.find('.items-list-tittle a'); - a.find('b').remove(); + const a = item.find('.middle-list-tittle a'); return { - title: a.text(), + title: a.text().trim(), link: `${rootUrl}/${a.attr('href')}`, - author: item.find('.items-list-user a').text().trim(), + author: item.find('.middle-list-user a').text().trim(), }; }); - items = await Promise.all( - items.map((item) => - cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); - if (detailResponse.url?.startsWith('https://www.taoguba.com.cn/topic/transfer')) { - item.description = '登录后查看完整文章'; - return item; - } - - const content = load(detailResponse.data); - - content('#videoImg').remove(); - content('img').each((_, img) => { - if (img.attribs.src2) { - img.attribs.src = img.attribs.src2; - delete img.attribs.src2; - delete img.attribs['data-original']; - } - }); - - item.description = content('#first').html(); - item.pubDate = timezone( - parseDate( - content('.article-data span') - .eq(1) - .text() - .match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/) - ), - +8 - ); - item.category = content('.article-topic-list span') - .toArray() - .map((item) => $(item).text().trim()); - - return item; - }) - ) - ); + items = await Promise.all(items.map(async (item) => await renderPostDetail(item))); return { title: $('head title').text().trim().split('_')[0], diff --git a/lib/routes/taoguba/namespace.ts b/lib/routes/taoguba/namespace.ts index c92d34a567..2e08e11bac 100644 --- a/lib/routes/taoguba/namespace.ts +++ b/lib/routes/taoguba/namespace.ts @@ -2,6 +2,6 @@ import type { Namespace } from '@/types'; export const namespace: Namespace = { name: '淘股吧', - url: 'taoguba.com.cn', + url: 'tgb.cn', lang: 'zh-CN', }; diff --git a/lib/routes/taoguba/util.ts b/lib/routes/taoguba/util.ts new file mode 100644 index 0000000000..8d2400ef9e --- /dev/null +++ b/lib/routes/taoguba/util.ts @@ -0,0 +1,48 @@ +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import timezone from '@/utils/timezone'; +import { parseDate } from '@/utils/parse-date'; + +const rootUrl = 'https://www.tgb.cn'; + +const renderPostDetail = async (item) => + await cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + + const content = load(detailResponse.data); + + content('#videoImg').remove(); + content('img').each((_, img) => { + if (img.attribs.src2) { + img.attribs.src = img.attribs.src2; + delete img.attribs.src2; + delete img.attribs['data-original']; + } + }); + + item.description = content('#first').html(); + if (detailResponse.url?.startsWith('https://www.tgb.cn/topic/transfer') || content('.login-view-button').length !== 0) { + item.description += '
登录后可查看完整文章'; + } + + item.pubDate = timezone( + parseDate( + content('.article-data > span:nth-child(2)') + .text() + .match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}/)[0] + ), + +8 + ); + + item.category = content('.classify') + .toArray() + .map((item) => content(item).text().trim()); + + return item; + }); + +export { rootUrl, renderPostDetail };