Created
April 22, 2020 15:41
-
-
Save kyletaylored/b93ca960bc9173baa125ee9125af69b5 to your computer and use it in GitHub Desktop.
Alexa Parser
This file contains 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
// Node script for extracting Alexa data | |
const path = require('path'); | |
const fs = require('fs'); | |
const flatten = require('flat'); | |
const jsonexport = require('jsonexport'); | |
// Get all files in the JSON directory | |
const directoryPath = path.join(__dirname, 'json'); | |
// Passing directoryPath and callback function | |
var writer = fs.createWriteStream('alexa.csv'); | |
let results = []; | |
fs.readdir(directoryPath, function (err, files) { | |
//handling error | |
if (err) { | |
return console.log('Unable to scan directory: ' + err); | |
} | |
// Reach each file. | |
files.forEach(function (file) { | |
// Do whatever you want to do with the file | |
let full_file = directoryPath + "/" + file; | |
let days = extractData(full_file); | |
days.forEach(i => results.push(flatten(i))); | |
}); | |
// Write to CSV | |
console.log(results); | |
jsonexport(results, function(err, csv){ | |
if (err) { | |
return console.log(err); | |
} | |
writer.write(csv); | |
}); | |
}); | |
/** | |
* Get data from JSON file | |
* @param {string} file Path to a JSON file. | |
*/ | |
function extractData(file) { | |
let rawdata = fs.readFileSync(file, 'utf8').substring(1); | |
let stats = JSON.parse(rawdata); | |
return stats.Awis.Results.Result.Alexa.TrafficHistory.HistoricalData.Data; | |
} |
This file contains 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
{ | |
"name": "alexa_stats", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"flat": "^5.0.0", | |
"jsonexport": "^2.4.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment