2021-11-02 00:01:11 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import https from 'https'
|
|
|
|
import initLog from '../utils/log.js'
|
|
|
|
import { exec } from 'child_process'
|
|
|
|
|
|
|
|
const log = initLog("Downloader")
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function can download and save data to files, or simply return the data.
|
|
|
|
* @param {string} url The URL of which to download from...
|
|
|
|
* @param {string} target This is the file path you want to save to. Alterntively use "_RETURN_VALUE_" to get value returned instead of saved to a file.
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*
|
|
|
|
* @author BurnyLlama
|
|
|
|
*/
|
|
|
|
export function download(url, target) {
|
|
|
|
return new Promise(
|
|
|
|
(resolve, reject) => {
|
|
|
|
log(`Starting a download >> ${url}`)
|
|
|
|
|
|
|
|
https.get(
|
|
|
|
url,
|
|
|
|
data => {
|
|
|
|
if (target === "_RETURN_VALUE_") {
|
2021-11-06 21:59:49 +01:00
|
|
|
let result = ""
|
2021-11-02 00:01:11 +01:00
|
|
|
|
|
|
|
data.on(
|
|
|
|
'data',
|
|
|
|
chunk => result += chunk
|
|
|
|
)
|
2021-11-06 21:59:49 +01:00
|
|
|
|
2021-11-02 00:01:11 +01:00
|
|
|
data.on(
|
|
|
|
'end',
|
|
|
|
() => resolve(result)
|
|
|
|
)
|
|
|
|
}
|
2021-11-06 21:59:49 +01:00
|
|
|
else {
|
|
|
|
const file = fs.createWriteStream(target)
|
|
|
|
data.pipe(file)
|
|
|
|
}
|
2021-11-02 00:01:11 +01:00
|
|
|
data.on(
|
|
|
|
'end',
|
|
|
|
() => {
|
|
|
|
log(`Done with a download >> ${url}`)
|
2021-11-06 21:59:49 +01:00
|
|
|
|
|
|
|
if (!target === "_RETURN_VALUE_")
|
|
|
|
file.close()
|
|
|
|
|
2021-11-02 00:01:11 +01:00
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function downloadEssentialData() {
|
|
|
|
return new Promise(
|
|
|
|
(resolve, reject) => {
|
|
|
|
log("Downloading 'ddnet.sqlite.zip' and 'players.msgpack'...")
|
|
|
|
Promise.all([
|
2021-11-04 23:12:51 +01:00
|
|
|
download("https://ddnet.tw/stats/ddnet.sqlite.zip", process.env.DATA_DIR ? `${process.env.DATA_DIR}/ddnet.sqlite.zip` : 'data/ddnet.sqlite.zip'),
|
|
|
|
download("https://ddnet.tw/players.msgpack", process.env.DATA_DIR ? `${process.env.DATA_DIR}/players.msgpack` : 'data/players.msgpack')
|
2021-11-02 00:01:11 +01:00
|
|
|
]).then(() => {
|
|
|
|
log("All downloads done! Going to unzip 'ddnet.sqlite.zip'...")
|
|
|
|
|
|
|
|
exec(
|
2021-11-04 23:12:51 +01:00
|
|
|
`unzip -o ${process.env.DATA_DIR ? `${process.env.DATA_DIR}/ddnet.sqlite.zip` : 'data/ddnet.sqlite.zip'} \
|
|
|
|
-d ${process.env.DATA_DIR ? process.env.DATA_DIR : 'data'}`,
|
2021-11-02 00:01:11 +01:00
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
log("Error while unzipping!")
|
|
|
|
reject()
|
|
|
|
}
|
|
|
|
|
|
|
|
log("Done unzipping!")
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|