From d1e93616659224ad94e14368583d43b6a2a37e89 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Wed, 19 Mar 2025 13:37:54 +0530 Subject: [PATCH] feat(routes): add discover route --- ui/app/api/discover/route.ts | 61 ++++++++++++++++++++++++++++++++++++ ui/app/discover/page.tsx | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ui/app/api/discover/route.ts diff --git a/ui/app/api/discover/route.ts b/ui/app/api/discover/route.ts new file mode 100644 index 0000000..0c95498 --- /dev/null +++ b/ui/app/api/discover/route.ts @@ -0,0 +1,61 @@ +import { searchSearxng } from '@/lib/searxng'; + +const articleWebsites = [ + 'yahoo.com', + 'www.exchangewire.com', + 'businessinsider.com', + /* 'wired.com', + 'mashable.com', + 'theverge.com', + 'gizmodo.com', + 'cnet.com', + 'venturebeat.com', */ +]; + +const topics = ['AI', 'tech']; /* TODO: Add UI to customize this */ + +export const GET = async (req: Request) => { + try { + const data = ( + await Promise.all([ + ...new Array(articleWebsites.length * topics.length) + .fill(0) + .map(async (_, i) => { + return ( + await searchSearxng( + `site:${articleWebsites[i % articleWebsites.length]} ${ + topics[i % topics.length] + }`, + { + engines: ['bing news'], + pageno: 1, + }, + ) + ).results; + }), + ]) + ) + .map((result) => result) + .flat() + .sort(() => Math.random() - 0.5); + + return Response.json( + { + blogs: data, + }, + { + status: 200, + }, + ); + } catch (err) { + console.error(`An error ocurred in discover route: ${err}`); + return Response.json( + { + message: 'An error has occurred', + }, + { + status: 500, + }, + ); + } +}; diff --git a/ui/app/discover/page.tsx b/ui/app/discover/page.tsx index eb94040..eb7de7f 100644 --- a/ui/app/discover/page.tsx +++ b/ui/app/discover/page.tsx @@ -19,7 +19,7 @@ const Page = () => { useEffect(() => { const fetchData = async () => { try { - const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/discover`, { + const res = await fetch(`/api/discover`, { method: 'GET', headers: { 'Content-Type': 'application/json',