-
-
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.
District API route - Request electroral district data and cache the d…
- Loading branch information
Showing
7 changed files
with
156,181 additions
and
87 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import json | ||
|
||
# Load wk-vec-tree.json | ||
with open('wk-vec-tree.json', 'r', encoding='utf-8') as wk_vec_file: | ||
wk_vec_data = json.load(wk_vec_file) | ||
|
||
# Load geo.json | ||
with open('geo.json', 'r', encoding='utf-8') as geo_file: | ||
geo_data = json.load(geo_file) | ||
|
||
# Loop through the features in geo.json | ||
for feature in geo_data['features']: | ||
lwk = int(feature['properties']['LWK']) # Get LWK value, convert to int | ||
index = lwk # Adjust LWK to 0-based index | ||
|
||
# Check if the index is valid for wk-vec-tree.json | ||
if 0 <= index < len(wk_vec_data): | ||
bezeichnung = wk_vec_data[index].get('bezeichnung') | ||
# Add the "code" key with the corresponding bezeichnung | ||
feature['properties']['code'] = bezeichnung | ||
|
||
# Write the updated geo.json back to a file | ||
with open('geo.json', 'w', encoding='utf-8') as updated_geo_file: | ||
json.dump(geo_data, updated_geo_file, ensure_ascii=False, indent=4) | ||
|
||
print("geo.json updated successfully with 'code' from wk-vec-tree.json!") |
Large diffs are not rendered by default.
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,23 +1,37 @@ | ||
// pages/api/district.js | ||
|
||
export default async function handler(req, res) { | ||
const { code } = req.query; // Get the district code from the query string | ||
|
||
if (!code) { | ||
return res.status(400).json({ error: "District code is required" }); | ||
} | ||
|
||
const fetchResponse = async (url, res) => { | ||
try { | ||
const response = await fetch(`https://wahlen.rlp-ltw-2021.23degrees.eu/assets/json/${code}.json`); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to fetch data'); | ||
const response = await fetch(url); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to fetch data'); | ||
} | ||
|
||
const data = await response.json(); | ||
res.status(200).json(data); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
|
||
const data = await response.json(); | ||
res.status(200).json(data); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
} | ||
|
||
const fetchData = async (code, state, res) => { | ||
const stateStruc = { | ||
'rlp': `https://wahlen.rlp-ltw-2021.23degrees.eu/assets/json/${code}.json`, | ||
'hessen': `https://wahlen.hessen-ltw23.23degrees.eu/assets/json/${code}.json` | ||
} | ||
} | ||
|
||
switch(state) { | ||
case 'rlp': | ||
return fetchResponse(stateStruc[state], res); | ||
case 'hessen': | ||
return fetchResponse(stateStruc[state], res); | ||
default: | ||
return stateStruc['rlp']; | ||
} | ||
}; | ||
|
||
export default async function handler(req, res) { | ||
const { code } = req.query; | ||
const { state } = req.query; | ||
const data = await fetchData(code, state, res); | ||
} | ||
|