From bbcb33e531b4c1e919dc49d86d7ffaf4d68facba Mon Sep 17 00:00:00 2001 From: Tykayn Date: Thu, 21 Sep 2023 18:15:45 +0200 Subject: [PATCH] up convert sms --- sms_to_csv.mjs | 137 ++++++++++++++++++------------------------------- utils.mjs | 4 +- 2 files changed, 52 insertions(+), 89 deletions(-) diff --git a/sms_to_csv.mjs b/sms_to_csv.mjs index ad10691..c47fe7c 100644 --- a/sms_to_csv.mjs +++ b/sms_to_csv.mjs @@ -1,16 +1,17 @@ import fs from "node-fs"; import convert from "xml-js"; -import {writeFileInOuputFolderFromJsonObject} from "./parse_orgmode_to_json.mjs"; +import {writeFileInOuputFolderFromJsonObject} from "./utils.mjs"; /********************** * initialize configs **********************/ -const sourceFileName = 'sms-20180423162531.xml' +const sourceFileName = 'sms.xml' const outputFileJson = 'sms-20180423162531.json' -const sourceFileJson = 'output/' + outputFileJson const outputFileCsv = 'sms-20180423162531.tsv' -const sourceFilePath = '/home/tykayn/Nextcloud/ressources/social sorting/' + sourceFileName; +const outputFileJsonPathFull = '/home/cipherbliss/Nextcloud/ressources/social sorting/output/' + outputFileJson +const sourceFileJson = '/home/cipherbliss/Nextcloud/ressources/social sorting/output/' + outputFileJson +const sourceFilePath = '/home/cipherbliss/Nextcloud/ressources/social sorting/' + sourceFileName; let headers = [] let tasksObjectsForJsonExport = [] @@ -38,44 +39,51 @@ function convertToCsv(elementsArray) { function convertJsonToCsv(sourceFilePath, outputFileName) { fs.readFile(sourceFilePath, 'utf8', function (err, data) { - const events = [ - - 'amount\t' + - 'content\t' + - 'description\t' + - 'destination\t' + - 'end\t' + - 'kind of activity\t' + - 'person\t' + - 'place\t' + - 'source\t' + - 'start\t' + - 'unique id\t' + - 'url\t' - ]; - data = JSON.parse(data) console.log('data', data) - // console.log('data', Object.keys(data[0])) - data['smses']['sms'].forEach(item => { + if (data) { - // convert all fields to common event description - events.push( - '\t' + - item._attributes.body.replace('\n', ' ') + '\t' + - 'sms ' + item._attributes.address + ' le ' + item._attributes.readable_date + '\t' + - item._attributes.address + '\t' + - '' + '\t' + - '' + '\t' + - item._attributes.contact_name + '\t' + - '' + '\t' + - '' + '\t' + - '' + '\t' + - '' + '\t' + - '' + '\t' - ) - }) - console.log('events', events) - writeFileInOuputFolderFromJsonObject(outputFileName, events.join("\n")) + const events = [ + + 'amount\t' + + 'content\t' + + 'description\t' + + 'destination\t' + + 'end\t' + + 'kind of activity\t' + + 'person\t' + + 'place\t' + + 'source\t' + + 'start\t' + + 'unique id\t' + + 'url\t' + ]; + data = JSON.parse(data) + console.log('data', data) + // console.log('data', Object.keys(data[0])) + data['smses']['sms'].forEach(item => { + + // convert all fields to common event description + events.push( + '\t' + + item._attributes.body.replace('\n', ' ') + '\t' + + 'sms ' + item._attributes.address + ' le ' + item._attributes.readable_date + '\t' + + item._attributes.address + '\t' + + '' + '\t' + + '' + '\t' + + item._attributes.contact_name + '\t' + + '' + '\t' + + '' + '\t' + + '' + '\t' + + '' + '\t' + + '' + '\t' + ) + }) + console.log('events', events) + writeFileInOuputFolderFromJsonObject(outputFileName, events.join("\n")) + + } else { + console.error('no data in xml file ' + sourceFilePath) + } }) } @@ -94,7 +102,7 @@ function openSourceFile() { // console.log('smses', Object.keys(jsonConversion)) console.log('jsonConversion[0]', jsonConversion['10']) - writeFileInOuputFolderFromJsonObject(outputFileJson, jsonConversion) + writeFileInOuputFolderFromJsonObject(outputFileJsonPathFull, jsonConversion) convertJsonToCsv(sourceFileJson, outputFileCsv) }) @@ -111,49 +119,4 @@ function openSourceFile() { } -// openSourceFile() - -function convertJsonToCsv(sourceFilePath, outputFileName) { - fs.readFile(sourceFilePath, 'utf8', function (err, data) { - - const events = [ - - 'amount\t' + - 'content\t' + - 'description\t' + - 'destination\t' + - 'end\t' + - 'kind of activity\t' + - 'person\t' + - 'place\t' + - 'source\t' + - 'start\t' + - 'unique id\t' + - 'url\t' - ]; - data = JSON.parse(data) - console.log('data', data) - // console.log('data', Object.keys(data[0])) - data['smses']['sms'].forEach(item => { - - // convert all fields to common event description - events.push( - '\t' + - item._attributes.body.replace('\n', ' ') + '\t' + - 'sms ' + item._attributes.address + ' le '+item._attributes.readable_date+'\t' + - item._attributes.address + '\t' + - '' + '\t' + - '' + '\t' + - item._attributes.contact_name + '\t' + - '' + '\t' + - '' + '\t' + - '' + '\t' + - '' + '\t' + - '' + '\t' - ) - }) - console.log('events', events) - writeFileInOuputFolderFromJsonObject(outputFileName, events.join("\n")) - }) -} - +openSourceFile() diff --git a/utils.mjs b/utils.mjs index c5639aa..f701527 100644 --- a/utils.mjs +++ b/utils.mjs @@ -20,8 +20,8 @@ export async function writeFileInOuputFolderFromJsonObject(fileName, jsonObjectT // console.log('statistics.dates', statistics.dates) return await fs.writeFile( - `${fileName}`, - JSON.stringify(jsonObjectThing), + `${outputAbsolutePath}${fileName}`, + JSON.stringify(jsonObjectThing, null, 2), "utf8", (err) => { if (err) {