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

Update sources.ts #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
66 changes: 41 additions & 25 deletions pages/api/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const searchHandler = async (req: NextApiRequest, res: NextApiResponse<Data>) =>
const sourceCount = 4;

// GET LINKS
const response = await fetch(`https://www.google.com/search?q=${query}`);
const encodedQuery = encodeURIComponent(query);
const response = await fetch(`https://www.google.com/search?q=${encodedQuery}`);
const html = await response.text();
const $ = cheerio.load(html);
const linkTags = $("a");
Expand All @@ -38,43 +39,58 @@ const searchHandler = async (req: NextApiRequest, res: NextApiResponse<Data>) =>
}
});

const filteredLinks = links.filter((link, idx) => {
const domain = new URL(link).hostname;

const excludeList = ["google", "facebook", "twitter", "instagram", "youtube", "tiktok"];
if (excludeList.some((site) => domain.includes(site))) return false;

return links.findIndex((link) => new URL(link).hostname === domain) === idx;
const filteredLinks = links.filter((link) => {
try {
const url = new URL(link);
const domain = url.hostname;

const excludeList = ["google", "facebook", "twitter", "instagram", "youtube", "tiktok"];
if (excludeList.some((site) => domain.includes(site))) return false;

return links.findIndex((l) => {
try {
return new URL(l).hostname === domain;
} catch {
return false;
}
}) === links.indexOf(link);
} catch (error) {
console.error("Invalid URL:", link);
return false;
}
});

const finalLinks = filteredLinks.slice(0, sourceCount);

// SCRAPE TEXT FROM LINKS
const sources = (await Promise.all(
finalLinks.map(async (link) => {
const response = await fetch(link);
const html = await response.text();
const dom = new JSDOM(html);
const doc = dom.window.document;
const parsed = new Readability(doc).parse();

if (parsed) {
let sourceText = cleanSourceText(parsed.textContent);

return { url: link, text: sourceText };
try {
const response = await fetch(link);
const html = await response.text();
const dom = new JSDOM(html);
const doc = dom.window.document;
const parsed = new Readability(doc).parse();

if (parsed) {
let sourceText = cleanSourceText(parsed.textContent);

return { url: link, text: sourceText };
}
} catch (error) {
console.error("Error scraping:", link, error);
}
})
)) as Source[];

const filteredSources = sources.filter((source) => source !== undefined);
)).filter((source): source is Source => source !== undefined);

for (const source of filteredSources) {
source.text = source.text.slice(0, 1500);
}
const filteredSources = sources.map(source => ({
...source,
text: source.text.slice(0, 1500)
}));

res.status(200).json({ sources: filteredSources });
} catch (err) {
console.log(err);
console.error(err);
res.status(500).json({ sources: [] });
}
};
Expand Down