Skip to content

Commit

Permalink
District API route - Request electroral district data and cache the d…
Browse files Browse the repository at this point in the history
…ata #74
  • Loading branch information
Scobiform committed Oct 20, 2024
1 parent 5cd22c2 commit 5b80190
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/components/Map/DynamicMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const DynamicMap = ({ polygons = [],
setLoading(true);
try {
let electoralDistrict;
let stateNumber;
switch (state.toLowerCase()) {
case 'sh': // Schleswig-Holstein
electoralDistrict = properties.WKNR_int;
Expand All @@ -143,16 +144,14 @@ const DynamicMap = ({ polygons = [],
break;
case 'bundestag':
electoralDistrict = properties.WKR_NR;
stateNumber = parseInt(properties.LAND_NR, 10);
break;
default:
console.warn(`State '${state}' not recognized.`);
electoralDistrict = null; // Fallback in case of an unrecognized state
break;

}

// API endpoint for fetching district data
// api/download?electoralDistrict=1&state=sh
const response = await fetch(`/api/download?electoralDistrict=${electoralDistrict}&state=${state}`);
const response = await fetch(`/api/download?electoralDistrict=${electoralDistrict}&state=${state}&stateNumber=${stateNumber}`);
if (!response.ok) {
throw new Error('Failed to fetch data');
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/api/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async function scrapeVoterTurnoutChart(page) {

export default async function handler(req, res) {
let browser;
const { electoralDistrict, state } = req.query;
const { electoralDistrict, state, stateNumber } = req.query;
const cacheDir = path.join(process.cwd(), 'public', 'cache');
const cacheFile = path.join(cacheDir, `${electoralDistrict}_${state}.json`);

Expand Down Expand Up @@ -211,7 +211,7 @@ export default async function handler(req, res) {
break;
case 'bundestag':
console.log('Scraping data for Bundestag');
let url = `https://www.bundeswahlleiter.de/bundestagswahlen/2021/ergebnisse/bund-99/land-12/wahlkreis-${electoralDistrict}.html`;
let url = `https://www.bundeswahlleiter.de/bundestagswahlen/2021/ergebnisse/bund-99/land-${stateNumber}/wahlkreis-${electoralDistrict}.html`;
console.log(url);
await page.goto(url, { waitUntil: 'domcontentloaded' });
break;
Expand Down

0 comments on commit 5b80190

Please sign in to comment.