-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use wikipedia wrapper instead of axios
Signed-off-by: Ruihang Xia <[email protected]>
- Loading branch information
Showing
7 changed files
with
54 additions
and
57 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import axios from 'axios' | ||
import * as cheerio from 'cheerio' | ||
|
||
const WIKIPEDIA_AD_URL = 'https://en.wikipedia.org/wiki/AD_' | ||
// const WIKIPEDIA_AD_URL = 'https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/AD_' | ||
import wiki from 'wikipedia' | ||
|
||
export async function fetch_people_in(year: number) { | ||
return axios.get(WIKIPEDIA_AD_URL + year).then((response) => { | ||
const $ = cheerio.load(response.data) | ||
const birthsHeader = $('#Births').parent() | ||
const births = birthsHeader.nextUntil('h2').find('li').map((_, el) => { | ||
const desc = $(el).text() | ||
const link = $(el).find('a').attr('href') | ||
const deathMatch = desc.match(/\(d\. \d+/i)?.[0] | ||
const death = deathMatch ? Number.parseInt(deathMatch.substring(3)) : undefined | ||
return { desc, link, death } | ||
}, | ||
).get() | ||
return births | ||
}) | ||
const page = await wiki.page(`AD_${year}`) | ||
const html = await page.html({ redirect: true }) | ||
return extract_people_from_html(html) | ||
} | ||
|
||
export function extract_people_from_html(html: string) { | ||
const $ = cheerio.load(html) | ||
const birthsHeader = $('#Births').parent() | ||
const births = birthsHeader.nextUntil('h2').find('li').map((_, el) => { | ||
const desc = $(el).text() | ||
const link = $(el).find('a').attr('href') | ||
const deathMatch = desc.match(/\(d\. \d+/i)?.[0] | ||
const death = deathMatch ? Number.parseInt(deathMatch.substring(3)) : undefined | ||
return { desc, link, death } | ||
}).get() | ||
|
||
return births | ||
} |
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