-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add news api with NPR and Al Jazeera test sources
- Loading branch information
1 parent
b7e6e4e
commit 2d578bc
Showing
7 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { load } from "cheerio"; | ||
import { z } from "zod"; | ||
|
||
const articleSchema = z.object({ | ||
title: z.string().min(5), | ||
link: z.string().url(), | ||
source: z.string(), | ||
}); | ||
|
||
type NewsSource = { | ||
name: string; | ||
url: (page: number) => string; | ||
listSelector: string; | ||
baseUrl?: string; | ||
}; | ||
|
||
const newsSources: NewsSource[] = [ | ||
{ | ||
name: "NPR", | ||
url: (page: number) => `http://text.npr.org?page=${page}`, | ||
listSelector: "ul > li > a", | ||
baseUrl: "http://text.npr.org", | ||
}, | ||
{ | ||
name: "Al Jazeera", | ||
url: (page: number) => `https://www.aljazeera.com/us-canada?page=${page}`, | ||
listSelector: "article .gc__content a", | ||
baseUrl: "https://www.aljazeera.com", | ||
}, | ||
]; | ||
|
||
const isValidArticle = (article: { title: string; link: string }) => { | ||
try { | ||
articleSchema.parse(article); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const fetchArticlesFromSource = async ( | ||
source: NewsSource, | ||
page: number = 1, | ||
) => { | ||
const response = await fetch(source.url(page)); | ||
const text = await response.text(); | ||
|
||
const $ = load(text); | ||
const articles: { title: string; link: string; source: string }[] = []; | ||
|
||
$(source.listSelector).each((_, element) => { | ||
const title = $(element).text().trim(); | ||
const link = source.baseUrl | ||
? `${source.baseUrl}${$(element).attr("href")}` | ||
: $(element).attr("href"); | ||
|
||
if (title && link) { | ||
articles.push({ | ||
title, | ||
link, | ||
source: source.name, | ||
}); | ||
} | ||
}); | ||
|
||
return articles.filter(isValidArticle); | ||
}; | ||
|
||
export { fetchArticlesFromSource, isValidArticle, newsSources }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { describe, expect, it } from "bun:test"; | ||
import { fetchArticlesFromSource, newsSources } from "../src/util/api"; | ||
|
||
describe("Article Fetching Functions", () => { | ||
it("Should fetch and parse NPR articles", async () => { | ||
const articles = await fetchArticlesFromSource(newsSources[0], 1); | ||
expect(articles.length).toBeGreaterThanOrEqual(10); | ||
articles.forEach((article) => { | ||
expect(article).toMatchObject({ | ||
title: expect.any(String), | ||
link: expect.any(String), | ||
source: "NPR", | ||
}); | ||
}); | ||
}); | ||
|
||
it("Should fetch and parse Al Jazeera articles", async () => { | ||
const articles = await fetchArticlesFromSource(newsSources[1], 1); | ||
expect(articles.length).toBeGreaterThanOrEqual(10); | ||
articles.forEach((article) => { | ||
expect(article).toMatchObject({ | ||
title: expect.any(String), | ||
link: expect.any(String), | ||
source: "Al Jazeera", | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"compilerOptions": { | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "hono/jsx", | ||
"allowImportingTsExtensions": true, | ||
"noEmit": true, | ||
"types": [ | ||
"bun-types" | ||
] | ||
"types": ["bun-types"], | ||
"esModuleInterop": true, | ||
"moduleResolution": "node" | ||
} | ||
} |