-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-update.js
116 lines (94 loc) · 2.87 KB
/
check-update.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const { execSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const axios = require('axios')
const cheerio = require('cheerio')
const anzip = require('anzip')
const baseUrl = 'https://www.bundesnetzagentur.de'
const siteUrl =
baseUrl +
'/SharedDocs/Downloads/DE/Sachgebiete/Telekommunikation/Unternehmen_Institutionen/Nummerierung/Rufnummern/ONVerzeichnisse/ONBGrenzen/1onbgrenzen_zip.html'
const hrefRegex = /Grenz.*\.zip$/i
const tmpDir = path.resolve(__dirname, '.tmp')
const tmpPath = path.resolve(tmpDir, 'download.zip')
const rawPath = path.resolve(__dirname, 'raw')
main()
async function main() {
console.log('Removing temporary downloads ...')
await cleanup()
console.log('Looking for download url ...')
const { downloadUrl, releaseDate } = await getDownloadUrl()
if (!downloadUrl) {
console.error('ZIP Link not found. :(')
process.exit(1)
}
const stringDate = releaseDate.toISOString().split('T')[0]
console.log('Found ONB-Grenzen with date of ' + stringDate)
console.log('Downloading ONB-Grenzen.zip ...')
await downloadFileToTmp(downloadUrl)
console.log('Unzipping ...')
await anzip(tmpPath, { outputPath: rawPath })
const hasChanged = await checkIfChanged()
if (!hasChanged) {
console.log('Nothing has changed since last download. Finished.')
process.exit(0)
}
console.log('Converting to geojson ...')
execSync('./convert.sh')
console.log('Commit changes ...')
execSync('git add -A')
execSync(`git commit -m "Update data to ${stringDate}"`)
console.log('Done. Publish a new release by running')
console.log(' npm run release')
}
async function cleanup() {
try {
fs.unlinkSync(tmpPath)
} catch (_error) {
// intentionally left blank
}
}
async function getDownloadUrl() {
const result = await axios.get(siteUrl)
const $ = cheerio.load(result.data)
let downloadUrl, releaseDate
$('a[href]').each(function () {
const href = $(this).attr('href')
if (hrefRegex.test(href)) {
downloadUrl = href.startsWith('https://') ? href : baseUrl + href
const [day, month, year] = $(this)
.text()
.match(/Stand: ([\d|\.]+)/i)[1]
.split('.')
.map((i) => +i)
releaseDate = new Date(year, month - 1, day, 12)
}
})
return { downloadUrl, releaseDate }
}
async function downloadFileToTmp(url) {
try {
fs.mkdirSync(tmpDir)
} catch (_error) {
// intentionally left blank
}
const writer = fs.createWriteStream(tmpPath, { flags: 'w' })
const download = await axios({
url,
method: 'GET',
responseType: 'stream',
})
download.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}
async function checkIfChanged() {
try {
execSync('git diff --exit-code --quiet raw')
return false
} catch (_error) {
return true
}
}