-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (66 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {fetch as _fetch} from 'cross-fetch'
import {parse as parseContentType} from 'content-type'
import createThrottle from 'p-throttle'
const pkg = require('./package.json')
const ENDPOINT = 'https://www.klimaschutz-planer.de/ajax.php'
const USER_AGENT = pkg.homepage
const rawFetch = async (query, expectedContentType = 'application/json') => {
const url = new URL(ENDPOINT)
url.search = new URLSearchParams(query)
const res = await _fetch(url.href, {
mode: 'cors',
redirect: 'follow',
headers: {
'User-Agent': USER_AGENT,
'Accept': 'application/json',
},
})
console.error(url.href, res.status)
if (!res.ok) {
const err = new Error(res.statusText)
err.query = query
err.statusCode = res.status
try {
err.body = await res.text()
} catch (err) {}
err.res = res
throw err
}
const cType = res.headers.has('content-type') && parseContentType(res.headers.get('content-type'))
if (cType && cType.type !== expectedContentType) {
const err = new Error(`unexpeted content-type ${cType.type}`)
err.query = query
err.statusCode = res.status
try {
err.body = await res.text()
} catch (err) {}
err.res = res
throw err
}
return await (expectedContentType === 'application/json' ? res.json() : res.text())
}
const throttle = createThrottle({
limit: 50, // find proper limit
interval: 60 * 1000,
})
const throttledFetch = throttle(rawFetch)
const fetchMunicipalities = async (year) => {
const reports = await throttledFetch({
action: 'thgMap',
year: year + '',
onlyShowCached: '1', // todo: what does this do?
})
// Gemeindeschlüssel (community/municipality ID/key) -> municipality
const municipalities = new Map()
for (const [gemeindeschlüssel, rawReport] of Object.entries(reports)) {
// todo: parse report
const report = rawReport
// todo: can there be more than one report per gemeindeschlüssel?
municipalities.set(gemeindeschlüssel, report)
}
return municipalities
}
export {
fetchMunicipalities,
// todo
}