-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Download the data directly from the migration site
- Loading branch information
Showing
5 changed files
with
447 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { groupBy } from "fp-ts/lib/NonEmptyArray"; | ||
import { z } from "zod"; | ||
|
||
const mutationTypes = { | ||
1: "INCLUSION", | ||
2: "FUSION", | ||
}; | ||
export const MunicipalityMigrationData = z | ||
.object({ | ||
ds: z.array( | ||
z | ||
.tuple([ | ||
z.number(), | ||
z.number(), | ||
z.string(), | ||
z.number(), | ||
z.string(), | ||
z.number(), | ||
z.string(), | ||
z.string(), | ||
z.string(), | ||
]) | ||
.transform((val) => ({ | ||
migrationNumber: val[0], | ||
histNumber: val[1], | ||
canton: val[2], | ||
bezirkNumber: val[3], | ||
bezirkName: val[4], | ||
ofsNumber: val[5], | ||
municipalityName: val[6], | ||
radiationReason: val[7], | ||
inscriptionReason: val[8], | ||
})) | ||
), | ||
mutations: z.record( | ||
z | ||
.object({ | ||
t: z.number(), | ||
d: z.string(), | ||
}) | ||
.transform((val) => ({ | ||
type: val.t, | ||
date: val.d, | ||
})) | ||
), | ||
}) | ||
.transform((val) => { | ||
const ds = val.ds; | ||
const items = ds.map((row) => { | ||
const mutation = val.mutations[row.migrationNumber]; | ||
return { | ||
...row, | ||
date: mutation.date, | ||
year: Number(mutation.date.split(".")[2]), | ||
type: mutationTypes[mutation.type as keyof typeof mutationTypes], | ||
}; | ||
}); | ||
const grouped = groupBy<(typeof items)[number]>( | ||
(x) => `${x.migrationNumber}` | ||
)(items); | ||
return Object.entries(grouped).map(([migrationNumber, items]) => { | ||
const added = items.filter((x) => x.inscriptionReason); | ||
const removed = items.filter((x) => x.radiationReason); | ||
return { | ||
added, | ||
removed, | ||
year: items[0].year, | ||
migrationNumber: Number(migrationNumber), | ||
type: items[0].type, | ||
label: `+${added.map((x) => x.municipalityName).join(", ")} / -${removed | ||
.map((x) => x.municipalityName) | ||
.join(", ")}`, | ||
}; | ||
}); | ||
}); | ||
|
||
export type MunicipalityMigrationData = z.infer< | ||
typeof MunicipalityMigrationData | ||
>; | ||
|
||
export type MunicipalityMigrationDataItem = MunicipalityMigrationData[number]; |
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,45 @@ | ||
import { JSDOM } from "jsdom"; | ||
import { NextApiHandler } from "next"; | ||
import { MunicipalityMigrationData } from "src/domain/municipality-migrations"; | ||
|
||
async function fetchAndParse(dateFrom: string, dateTo: string) { | ||
const url = `https://www.agvchapp.bfs.admin.ch/fr/mutations/results?EntriesFrom=${dateFrom}&EntriesTo=${dateTo}`; | ||
|
||
const response = await (await fetch(url)).text(); | ||
const html = response; | ||
|
||
const dom = new JSDOM(html); | ||
const document = dom.window.document; | ||
|
||
const scriptsWithDs = document.querySelectorAll("script"); | ||
let dsValue; | ||
|
||
scriptsWithDs.forEach((script) => { | ||
const content = script.textContent; | ||
if (content && content.includes("var ds")) { | ||
const trimmed = content.slice(0, content.indexOf("var getGroupLabel")); | ||
dsValue = eval(`${trimmed}; ({ ds: ds, mutations: mutations })`); | ||
return; | ||
} | ||
}); | ||
|
||
if (dsValue) { | ||
return MunicipalityMigrationData.parse(dsValue); | ||
} else { | ||
throw new Error("Script with var ds not found"); | ||
} | ||
} | ||
|
||
const handler: NextApiHandler = async (req, res) => { | ||
const { from: dateFrom, to: dateTo } = req.query; | ||
if (!dateFrom || !dateTo) { | ||
return res | ||
.status(400) | ||
.send('Please provide "from" and "to" query parameters'); | ||
} | ||
const content = await fetchAndParse(dateFrom as string, dateTo as string); | ||
|
||
return res.send(content); | ||
}; | ||
|
||
export default handler; |
Oops, something went wrong.