Last active
May 26, 2023 19:42
-
-
Save ferrannp/74767cc60632bb918ca58c7079a92429 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import fetch from 'node-fetch'; | |
import AdmZip from 'adm-zip'; | |
import zlib from 'zlib'; | |
import fs from 'fs'; | |
import path from 'path'; | |
const username = ''; // Add secret username | |
const password = ''; // Add secret password | |
const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`; | |
const outputDir = './output'; | |
if (!fs.existsSync(outputDir)) { | |
fs.mkdirSync(outputDir); | |
} | |
// From October 1, 2022 till May 3, 2023 | |
const queries = [['20221001T00', '20230503T00']]; | |
queries.forEach(queryParams => { | |
const apiUrl = `https://amplitude.com/api/2/export?start=${queryParams[0]}&end=${queryParams[1]}`; | |
fetch(apiUrl, { | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': authHeader, | |
} | |
}) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error('Network response was not ok'); | |
} | |
return response.buffer(); | |
}) | |
.then(buffer => { | |
const zip = new AdmZip(buffer); | |
const zipEntries = zip.getEntries(); | |
zipEntries.forEach(entry => { | |
const filename = `${entry.entryName}`.replace('.gz', ''); | |
const filePath = path.join(outputDir, filename.split('/')[2]); | |
const fileData = zip.readFile(entry); | |
const decompressedData = zlib.gunzipSync(fileData).toString('utf8'); | |
const jsonData = `[${decompressedData.replace(/\n/g, ',')}]`.replace(',]', ']'); | |
const data = JSON.parse(jsonData, null, 2).map(d => ({ | |
ampltiudeId: d.amplitude_id, | |
uuid: d.uuid, | |
deviceId: d.device_id, | |
eventTime: d.event_time, | |
eventId: d.event_id, | |
eventType: d.event_type, | |
eventProperties: d.event_properties, | |
deviceType: d.device_type, | |
platform: d.platform, | |
language: d.language, | |
country: d.country, | |
city: d.city, | |
region: d.region, | |
})); | |
fs.writeFileSync(filePath, JSON.stringify(data)); | |
console.log(`Wrote ${filePath} to disk.`); | |
}); | |
}) | |
.catch(error => { | |
console.error(queryParams, error); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment