Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Download geolocation DB for previous month if current month fails

Blue Mouse27e18060e91a
diff --git a/backend/meta/dbip-sync.js b/backend/meta/dbip-sync.js
index 3ca6697cc..7bc5cf0b6 100644
--- a/backend/meta/dbip-sync.js
+++ b/backend/meta/dbip-sync.js
@@ -4,11 +4,15 @@ const fs = require('fs')
const zlib = require('zlib')
const path = require('path')
-// Generate the download link with the current date
-const currentDate = new Date()
-const year = currentDate.getFullYear()
-const month = (currentDate.getMonth() + 1).toString().padStart(2, '0')
-const DOWNLOAD_LINK = `https://download.db-ip.com/free/dbip-city-lite-${year}-${month}.mmdb.gz`
+// Add helper function to get download link for a specific date
+function getDownloadLink(date) {
+ const year = date.getFullYear()
+ const month = (date.getMonth() + 1).toString().padStart(2, '0')
+ return `https://download.db-ip.com/free/dbip-city-lite-${year}-${month}.mmdb.gz`
+}
+
+// Update the DOWNLOAD_LINK to use the helper function
+const DOWNLOAD_LINK = getDownloadLink(new Date())
const DESTINATION_FILE = 'dbip-city-lite.mmdb'
const ORIGINAL_FILE = 'dbip-city-lite.mmdb.gz'
@@ -23,31 +27,45 @@ function downloadFile(url, destPath) {
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(destPath)
- https.get(url, (response) => {
- response.pipe(fileStream)
+ https
+ .get(url, response => {
+ response.pipe(fileStream)
- fileStream.on('finish', () => {
- fileStream.close()
- console.log('[OK] Download finished!')
- resolve()
- })
+ fileStream.on('finish', () => {
+ fileStream.close()
+ console.log('[OK] Download finished!')
+ resolve()
+ })
- fileStream.on('error', (error) => {
+ fileStream.on('error', error => {
+ fs.unlinkSync(destPath)
+ console.log('[ERROR] Download failed.')
+ reject(error)
+ })
+ })
+ .on('error', error => {
fs.unlinkSync(destPath)
console.log('[ERROR] Download failed.')
reject(error)
})
- }).on('error', (error) => {
- fs.unlinkSync(destPath)
- console.log('[ERROR] Download failed.')
- reject(error)
- })
})
}
-(async () => {
+;(async () => {
try {
- await downloadFile(DOWNLOAD_LINK, GEOIP_DB_PATH)
+ try {
+ // Try current month first
+ await downloadFile(DOWNLOAD_LINK, GEOIP_DB_PATH)
+ } catch (error) {
+ // If current month fails, try previous month
+ const lastMonth = new Date()
+ lastMonth.setMonth(lastMonth.getMonth() - 1)
+ const previousMonthLink = getDownloadLink(lastMonth)
+ console.log(
+ '[INFO] Current month download failed, trying previous month...',
+ )
+ await downloadFile(previousMonthLink, GEOIP_DB_PATH)
+ }
const gunzip = zlib.createGunzip()
const inputStream = fs.createReadStream(GEOIP_DB_PATH)
@@ -58,11 +76,11 @@ function downloadFile(url, destPath) {
console.log('[OK] File unzipped! .gz file removed.')
fs.unlinkSync(GEOIP_DB_PATH) // Remove the original .gz file
})
- outputStream.on('error', (error) => {
+ outputStream.on('error', error => {
fs.unlinkSync(GEOIP_DB_PATH)
console.error('[ERROR] Could not unzip file:', error.message)
})
} catch (error) {
- console.error('[ERROR] An error occured:', error.message)
+ console.error('[ERROR] An error occurred:', error.message)
}
})()