Skip to content

Instantly share code, notes, and snippets.

@theredpea
Last active February 11, 2023 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theredpea/da1ee3581c2e8c2fda5636c35b98f9c5 to your computer and use it in GitHub Desktop.
Save theredpea/da1ee3581c2e8c2fda5636c35b98f9c5 to your computer and use it in GitHub Desktop.
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel, appendString) {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
//This condition will generate the Label/Header
if (ShowLabel) {
var row = "";
//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
}
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = "";
//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV == '') {
alert("Invalid data");
return;
}
CSV += appendString;
//this trick will generate a temp "a" tag
var link = document.createElement("a");
link.id = "lnkDwnldLnk";
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
var csv = CSV;
blob = new Blob([csv], { type: 'text/csv' });
var csvUrl = window.webkitURL.createObjectURL(blob);
var filename = (ReportTitle || 'UserExport') + '.csv';
link.setAttribute('download', filename);
link.setAttribute('href',csvUrl);
link.click();
document.body.removeChild(link);
};
let result_array = Array.from(window.document.getElementsByClassName('cl-search-result')).map(el=>{
let
el_bedrooms = el.getElementsByClassName('post-bedrooms')[0],
el_price = el.getElementsByClassName('priceinfo')[0],
el_title = el.getElementsByClassName('titlestring')[0],
el_meta = el.getElementsByClassName('meta')[0],
el_date = el_meta.children[3],//.getAttribute('title'),
el_sq_ft = el.getElementsByClassName('post-sqft')[0],
el_hood = el_meta.children[1],
// https://stackoverflow.com/questions/7935689/what-is-the-difference-between-children-and-childnodes-in-javascript/7935719#7935719
posting_date = (el_date && el_date.getAttribute('title')) || (el_meta.childNodes[0] && el_meta.childNodes[0].textContent),
posting_title = el_title && el_title.innerText,
posting_hood =el_hood && el_hood.innerText,
posting_maptag_miles,
posting_link = el_title.getAttribute('href'),
posting_id,
price = +(el_price && el_price.innerText || '0').replace(/[^0-9\.]/g, ''),
bedrooms = +(((el_bedrooms && el_bedrooms.innerText || '' ) .match(/(\d+)br/) || [])[1] || 0) ,
sq_ft = +(((el_sq_ft && el_sq_ft.innerText || '').match(/([0-9\.]+)ft/) || [])[1] || 0) || '';
return {
// el,
posting_date,
posting_title,
posting_hood,
// posting_maptag_miles,
posting_link,
// posting_id,
price,
bedrooms,
sq_ft
};
})
JSONToCSVConvertor(result_array, window.location.search.replace(/\&/g,'_').replace(/bedroom/g, 'bdr').replace(/bathroom/g, 'btr').replace(/availabilityMode/g, 'avail').replace(/search_distance/g, 'dist').replace(/parking/g, 'prk'), true, window.location.href);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment