Reputation: 915
I am using the node.js tool Osmosis to pull a bunch of data as an array of json objects
The nature of Osmosis functions seems to be that the array exists only within the scope of the function, so I need to write the file inside the function as well before it stops running and disposes of the json file.
Here is my code pull the data:
'use strict';
const osmosis = require('osmosis');
const fs = require('fs');
const converter = require('json2csv');
var stringify = require('csv-stringify');
function getOpenGraphMeta() {
return new Promise((resolve, reject) => {
let response;
osmosis
.get('https://www.winepeople.com.au/wines/Dry-Red/_/N-1z13zte')
.find('.product-row.product-bottle')
.set({
title: "h2.wine-title",
headline: "h3.wine-headline",
text: "p.wine-text"
})
.data(res => response = res)
.error(err => reject(err))
.done(() => resolve(response));
});
}
getOpenGraphMeta().then(res => {
console.log(res);
});
function getHomePageTrending() {
return new Promise((resolve, reject) => {
let response = [];
let stringToReplaceComas = '!!!!';
osmosis
.get('https://www.winepeople.com.au/wines/Dry-Red/_/N-1z13zte')
.paginate('#search > div.content.col-xs-12.col-md-9.wine-search-results.search-page > div:nth-child(7) > div > nav > ul > li:nth-child(5) > a', 2)
.find('.product-row.product-bottle')
.set({
title: 'h2.wine-title',
headline: 'h3.wine-headline',
text: 'p.wine-text',
RRP: 'label.product-list-price',
VPP: 'h2.vpp-price',
TwelveBottle: 'label.product-price',
SixBottle: '#js-search-price > div:nth-child(2) > label > b',
})
.find('.row.wine-attributes')
.set({
Country: '//*[@id="search"]/div[2]/div[6]/div[3]/div/div/div[2]/div[2]/div[1]/text()'
})
.data(res => response.push(res))
.error(err => reject(err))
.done(() => resolve(response));
});
}
getHomePageTrending().then(res => {
console.log(res);
});
How can I then write the result[]
array of json objects to a csv on disk? Or even just a json file that I can then convert with a CLT? Any method of writing the json array to disk will suffice really.
Upvotes: 1
Views: 366
Reputation: 19957
I don't think you need csv-stringify
for your job.
getOpenGraphMeta().then(res => {
var jsonstrMinified = JSON.stringify(res); //unformatted
var jsonstr = JSON.stringify(res, null, 2); //formatted
fs.writeFileSync('/path/to/file-on-fisk.json', jsonstr)
var fields = ['title', 'headline', 'text'];
var parser = new converter.Parser({ fields });
var csvstr = parser.parse(res);
fs.writeFileSync('/path/to/file-on-fisk.csv', csvstr));
})
Upvotes: 1