Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 50 additions & 0 deletions modules/tool/packages/dailyHot/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { defineTool } from '@tool/type';
import { FlowNodeInputTypeEnum, WorkflowIOValueTypeEnum } from '@tool/type/fastgpt';
import { ToolTypeEnum } from '@tool/type/tool';

export default defineTool({
name: {
'zh-CN': '热榜工具',
en: 'Hot List Tool'
},
type: ToolTypeEnum.tools,
description: {
'zh-CN': '获取热榜信息,支持36氪、知乎、微博、掘金、头条等多个平台',
en: 'Get hot list information from multiple platforms including 36kr, Zhihu, Weibo, Juejin, and Toutiao'
},
toolDescription:
'Get hot trending content from multiple platforms including 36kr, Zhihu, Weibo, Juejin, and Toutiao with accurate publish times',
versionList: [
{
value: '0.1.0',
description: 'Default version',
inputs: [
{
key: 'sources',
label: '热榜来源',
renderTypeList: [FlowNodeInputTypeEnum.multipleSelect],
valueType: WorkflowIOValueTypeEnum.arrayString,
required: true,
description: '选择热榜来源网站(可多选)',
toolDescription: '选择热榜来源网站',
defaultValue: ['36kr'],
list: [
{ label: '36氪', value: '36kr' },
{ label: '知乎', value: 'zhihu' },
{ label: '微博', value: 'weibo' },
{ label: '掘金', value: 'juejin' },
{ label: '头条', value: 'toutiao' }
]
}
],
outputs: [
{
valueType: WorkflowIOValueTypeEnum.arrayObject,
key: 'hotNewsList',
label: '新闻热榜列表',
description: '新闻热榜数据列表'
}
]
}
]
});
10 changes: 10 additions & 0 deletions modules/tool/packages/dailyHot/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import config from './config';
import { InputType, OutputType, tool as toolCb } from './src';
import { exportTool } from '@tool/utils/tool';

export default exportTool({
toolCb,
InputType,
OutputType,
config
});
20 changes: 20 additions & 0 deletions modules/tool/packages/dailyHot/logo.svg
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logo没找到合适的

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions modules/tool/packages/dailyHot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@fastgpt-plugins/tool-daily-hot",
"module": "index.ts",
"type": "module",
"scripts": {
"build": "bun ../../../../scripts/build.ts"
},
"devDependencies": {
"@types/bun": "^1.2.2"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"zod": "^3.24.3"
}
}
232 changes: 232 additions & 0 deletions modules/tool/packages/dailyHot/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import { z } from 'zod';
import { POST, GET } from '@tool/utils/request';

export const InputType = z.object({
sources: z.array(z.enum(['36kr', 'zhihu', 'weibo', 'juejin', 'toutiao'])).default(['36kr'])
});

export const OutputType = z.object({
hotNewsList: z.array(
z.union([
z.object({
title: z.string().describe('hot news title').optional(),
description: z.string().describe('hot news description').optional(),
source: z.string().describe('hot news source website'),
time: z.string().describe('hot news publish time').optional()
}),
z.object({
source: z.string().describe('hot news source website'),
error: z.string().describe('failed to get hot news, error message').optional()
})
])
)
});

type Response = z.infer<typeof OutputType>['hotNewsList'][number];

type Kr36Response = {
data: {
hotRankList: {
templateMaterial: {
widgetTitle: string;
};
publishTime: string;
}[];
};
};
async function get36krList(): Promise<Response[]> {
const url = `https://gateway.36kr.com/api/mis/nav/home/nav/rank/hot`;

const { data: Kr36Response } = await POST<Kr36Response>(url, {
partner_id: 'wap',
param: {
siteId: 1,
platformId: 2
},
timestamp: new Date().getTime()
});
const kr36List = Kr36Response?.data?.hotRankList;
if (!kr36List) {
return Promise.reject({
error: 'Failed to get kr36 list'
});
}

const hotNewsList: Response[] = kr36List.map((item) => {
return {
title: item.templateMaterial.widgetTitle,
source: '36氪',
time: new Date(item.publishTime).toLocaleString('zh-CN')
};
});

return hotNewsList;
}

type ZhihuResponse = {
data: {
target: {
title: string;
created: number;
excerpt: string;
};
}[];
};
async function getZhihuList(): Promise<Response[]> {
const url = `https://api.zhihu.com/topstory/hot-lists/total?limit=50`;

const { data: ZhihuResponse } = await GET<ZhihuResponse>(url);
const zhihuList = ZhihuResponse?.data;
if (!zhihuList) {
return Promise.reject({
error: 'Failed to get zhihu list'
});
}

const hotNewsList: Response[] = zhihuList.map((item) => {
const data = item.target;
return {
title: data.title,
description: data.excerpt,
source: '知乎',
time: new Date(data.created * 1000).toLocaleString('zh-CN')
};
});

return hotNewsList;
}

type WeiboResponse = {
data: {
cards: {
card_group?: {
desc: string;
}[];
}[];
};
};
async function getWeiboList(): Promise<Response[]> {
const url =
'https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=filter_type%3Drealtimehot%26mi_cid%3D100103%26pos%3D0_0%26c_type%3D30%26display_time%3D1540538388&luicode=10000011&lfid=231583';

const { data: WeiboResponse } = await GET<WeiboResponse>(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
});
const weiboList = WeiboResponse.data.cards[0].card_group;
if (!weiboList) {
return Promise.reject({
error: 'Failed to get weibo list'
});
}

const hotNewsList: Response[] = weiboList.map((item) => {
return {
title: item.desc,
source: '微博'
};
});

return hotNewsList;
}

type JuejinResponse = {
data: {
content: {
content_id: string;
title: string;
created_at: number;
ctime: number;
};
author: {
name: string;
};
content_counter: {
hot_rank: number;
};
}[];
};
async function getJuejinList(): Promise<Response[]> {
const url = 'https://api.juejin.cn/content_api/v1/content/article_rank?category_id=1&type=hot';

const { data: JuejinResponse } = await GET<JuejinResponse>(url);
const juejinList = JuejinResponse?.data;
if (!juejinList) {
return Promise.reject({
error: 'Failed to get juejin list'
});
}

const hotNewsList: Response[] = juejinList.map((item) => {
return {
title: item.content.title,
source: '掘金'
};
});

return hotNewsList;
}

type ToutiaoResponse = {
data: {
Title: string;
}[];
};
async function getToutiaoList(): Promise<Response[]> {
const url = `https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc`;

const { data: ToutiaoResponse } = await GET<ToutiaoResponse>(url);
const toutiaoList = ToutiaoResponse?.data;
if (!toutiaoList) {
return Promise.reject({
error: 'Failed to get toutiao list'
});
}
const hotNewsList: Response[] = toutiaoList.map((item) => {
return {
title: item.Title,
source: '头条'
};
});

return hotNewsList;
}

export async function tool({
sources
}: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> {
const map = {
'36kr': get36krList,
zhihu: getZhihuList,
weibo: getWeiboList,
juejin: getJuejinList,
toutiao: getToutiaoList
};
const promises = sources.map(async (source) => {
return await map[source]();
});

const results = await Promise.allSettled(promises);
const allHotNewsList: Response[] = [];
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
allHotNewsList.push(...result.value);
} else {
allHotNewsList.push({
error: `Failed to get hot news from ${sources[index]}`,
source: sources[index]
});
}
});

if (!allHotNewsList.length) {
return Promise.reject({
error: 'Failed to get hot news list'
});
}

return {
hotNewsList: allHotNewsList
};
}
8 changes: 8 additions & 0 deletions modules/tool/packages/dailyHot/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { expect, test } from 'vitest';
import tool from '..';

test(async () => {
expect(tool.name).toBeDefined();
expect(tool.description).toBeDefined();
expect(tool.cb).toBeDefined();
});