Skip to content

Commit

Permalink
feat(wiki): get english data by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonestla committed Nov 15, 2024
1 parent eee46f1 commit 7e0fe36
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions client/src/components/wiki/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ export async function getWikidataPreviews(wikis, locale = "en") {
).then((r) => r.json()).catch((err) => { console.error("ERRORWIKIDATA", err); });

temp.forEach((el) => {
const entity = data?.entities?.[el.code];
el.title = entity?.sitelinks[`${locale}wiki`]?.url?.split("/").pop();
el.url = entity?.sitelinks[`${locale}wiki`]?.url;
const entity = data?.entities?.[el.code]
el.title =
entity?.sitelinks[`${locale}wiki`]?.url?.split("/").pop() || entity?.sitelinks[`enwiki`]?.url?.split("/").pop()
el.url = entity?.sitelinks[`${locale}wiki`]?.url || entity?.sitelinks[`enwiki`]?.url
})
const titles = temp.map((el) => el.title).join('|');

const titles = temp.map((el) => el.title).join("|")
const _url = `https://${locale}.wikipedia.org/w/api.php?titles=${titles}&action=query&format=json&prop=extracts|pageimages|pageprops&ppprop=wikibase_item&piprop=original&exintro&explaintext&origin=*`
const previews: WikiPreviewResult = await fetch(_url).then((res) => res.json());
const previews: WikiPreviewResult = await fetch(_url).then((res) => res.json())

const _urlEn = `https://en.wikipedia.org/w/api.php?titles=${titles}&action=query&format=json&prop=extracts|pageimages|pageprops&ppprop=wikibase_item&piprop=original&exintro&explaintext&origin=*`
const previewsEn: WikiPreviewResult = locale == "en" ? previews : await fetch(_urlEn).then((res) => res.json())

temp.forEach((el) => {
const page = Object.values(previews?.query?.pages || {}).find((page) => page?.pageprops?.wikibase_item === el.code);
el.title = page?.title;
const page =
Object.values(previews?.query?.pages || {}).find((page) => page?.pageprops?.wikibase_item === el.code) ||
Object.values(previewsEn?.query?.pages || {}).find((page) => page?.pageprops?.wikibase_item === el.code)
el.title = page?.title
el.extract = page?.extract;
el.image = page?.original?.source;
})
Expand Down

0 comments on commit 7e0fe36

Please sign in to comment.