feat(discover-api): handle topics

This commit is contained in:
ItzCrazyKns
2025-07-29 13:17:07 +05:30
parent 3bee01cfa7
commit 45b51ab156
2 changed files with 31 additions and 18 deletions

View File

@ -1,37 +1,52 @@
import { searchSearxng } from '@/lib/searxng'; import { searchSearxng } from '@/lib/searxng';
const articleWebsites = [ const websitesForTopic = {
'yahoo.com', tech: {
'www.exchangewire.com', query: ['technology news', 'latest tech', 'AI', 'science and innovation'],
'businessinsider.com', links: ['techcrunch.com', 'wired.com', 'theverge.com'],
/* 'wired.com', },
'mashable.com', finance: {
'theverge.com', query: ['finance news', 'economy', 'stock market', 'investing'],
'gizmodo.com', links: ['bloomberg.com', 'cnbc.com', 'marketwatch.com'],
'cnet.com', },
'venturebeat.com', */ art: {
]; query: ['art news', 'culture', 'modern art', 'cultural events'],
links: ['artnews.com', 'hyperallergic.com', 'theartnewspaper.com'],
},
sports: {
query: ['sports news', 'latest sports', 'cricket football tennis'],
links: ['espn.com', 'bbc.com/sport', 'skysports.com'],
},
entertainment: {
query: ['entertainment news', 'movies', 'TV shows', 'celebrities'],
links: ['hollywoodreporter.com', 'variety.com', 'deadline.com'],
},
};
const topics = ['AI', 'tech']; /* TODO: Add UI to customize this */ type Topic = keyof typeof websitesForTopic;
export const GET = async (req: Request) => { export const GET = async (req: Request) => {
try { try {
const params = new URL(req.url).searchParams; const params = new URL(req.url).searchParams;
const mode: 'normal' | 'preview' = const mode: 'normal' | 'preview' =
(params.get('mode') as 'normal' | 'preview') || 'normal'; (params.get('mode') as 'normal' | 'preview') || 'normal';
const topic: Topic = (params.get('topic') as Topic) || 'tech';
const selectedTopic = websitesForTopic[topic];
let data = []; let data = [];
if (mode === 'normal') { if (mode === 'normal') {
data = ( data = (
await Promise.all([ await Promise.all([
...new Array(articleWebsites.length * topics.length) ...new Array(selectedTopic.links.length * selectedTopic.query.length)
.fill(0) .fill(0)
.map(async (_, i) => { .map(async (_, i) => {
return ( return (
await searchSearxng( await searchSearxng(
`site:${articleWebsites[i % articleWebsites.length]} ${ `site:${selectedTopic.links[i % selectedTopic.links.length]} ${
topics[i % topics.length] selectedTopic.query[i % selectedTopic.query.length]
}`, }`,
{ {
engines: ['bing news'], engines: ['bing news'],
@ -45,11 +60,10 @@ export const GET = async (req: Request) => {
) )
.map((result) => result) .map((result) => result)
.flat() .flat()
.sort(() => Math.random() - 0.5);
} else { } else {
data = ( data = (
await searchSearxng( await searchSearxng(
`site:${articleWebsites[Math.floor(Math.random() * articleWebsites.length)]} ${topics[Math.floor(Math.random() * topics.length)]}`, `site:${selectedTopic.links[Math.floor(Math.random() * selectedTopic.links.length)]} ${selectedTopic.query[Math.floor(Math.random() * selectedTopic.query.length)]}`,
{ {
engines: ['bing news'], engines: ['bing news'],
pageno: 1, pageno: 1,

View File

@ -43,7 +43,6 @@ const Page = () => {
const fetchArticles = async (topic: string) => { const fetchArticles = async (topic: string) => {
setLoading(true); setLoading(true);
console.log(topic);
try { try {
const res = await fetch(`/api/discover?topic=${topic}`, { const res = await fetch(`/api/discover?topic=${topic}`, {
method: 'GET', method: 'GET',