Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(route/taoguba): 修复淘股吧更换域名以及修改获取帖子详情结构 #18103

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(route/taoguba): 修复淘股吧更换域名以及修改获取帖子详情结构
  • Loading branch information
ueiu committed Jan 12, 2025
commit 503e2706b15c5c9f708eb50d933e680b2ea7471e
51 changes: 4 additions & 47 deletions lib/routes/taoguba/blog.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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'],
Expand All @@ -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: '用户博客',
Expand All @@ -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({
Expand All @@ -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}`,
Expand Down
58 changes: 7 additions & 51 deletions lib/routes/taoguba/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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',
name: '论坛帖子',
maintainers: ['nczitzk'],
handler,
description: `| 淘股论坛 | 社区总版 | 精华加油 | 网友点赞 |
Expand All @@ -18,7 +16,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({
Expand All @@ -28,63 +25,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],
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/taoguba/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '淘股吧',
url: 'taoguba.com.cn',
url: 'tgb.cn',
lang: 'zh-CN',
};
48 changes: 48 additions & 0 deletions lib/routes/taoguba/util.ts
Original file line number Diff line number Diff line change
@@ -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 += '<br>登录后可查看完整文章';
}

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 };
Loading