org-report-stats/sms_to_csv.mjs

128 lines
3.7 KiB
JavaScript
Raw Normal View History

2023-09-21 22:00:15 +02:00
import fs from 'node-fs'
import convert from 'xml-js'
import { writeFileInOuputFolderFromJsonObject } from './utils.mjs'
2023-03-14 18:04:54 +01:00
/**********************
* initialize configs
**********************/
2023-09-21 22:00:15 +02:00
const sourceFileBaseName = 'sms-20180423162531'
const sourceFileName = `${sourceFileBaseName}.xml`
const outputFileJson = `${sourceFileBaseName}.json`
const outputFileCsv = `${sourceFileBaseName}.tsv`
const folder_base = '/home/tykayn/Nextcloud/ressources/social sorting/'
const outputFileJsonPathFull = `${folder_base}output/${outputFileJson}`
const sourceFileJson = `${folder_base}output/${outputFileJson}`
const sourceFilePath = folder_base + sourceFileName
2023-03-14 18:04:54 +01:00
let headers = []
let tasksObjectsForJsonExport = []
let headersByKind = {}
2023-09-21 22:00:15 +02:00
let writeJsonAfterParse = false
writeJsonAfterParse = true
2023-03-14 18:04:54 +01:00
/**************************************************************
* fetch the source file to read its contents
*************************************************************/
2023-09-21 22:00:15 +02:00
// console.log('parse some org file', sourceFilePath)
2023-03-14 18:04:54 +01:00
if (!sourceFilePath) {
2023-09-21 22:00:15 +02:00
console.error('pas de fichier à ouvrir')
2023-03-14 18:04:54 +01:00
}
2023-09-21 22:00:15 +02:00
function convertToCsv (elementsArray) {
elementsArray.forEach(item => {
// console.log('item._attributes.date_sent', item._attributes.date_sent)
// console.log('item._attributes.contact_name', item._attributes.date_sent)
// console.log('item._attributes.body', item._attributes.date_sent)
})
2023-03-14 18:04:54 +01:00
}
2023-09-21 22:00:15 +02:00
function convertJsonToCsvAndPersistTSVFile (jsonFilePath, outputFileTSVName) {
// console.log('convertJsonToCsvAndWriteItAs', jsonFilePath, outputFileTSVName)
fs.readFile( folder_base+ jsonFilePath, 'utf8', function (err, data) {
// console.log('data', data)
if (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.length', data.length)
// // 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(outputFileTSVName, events.join('\n')).then(res => {
// console.log('res', typeof res)
})
} else {
console.error('no data in xml file ' + jsonFilePath)
}
})
2023-03-14 18:04:54 +01:00
}
2023-09-21 22:00:15 +02:00
function openSourceFile () {
2023-03-14 18:04:54 +01:00
2023-09-21 22:00:15 +02:00
fs.stat(sourceFilePath, function (err, data) {
if (err == null) {
// console.log(`File ${sourceFilePath} exists`)
2023-03-14 18:04:54 +01:00
2023-09-21 22:00:15 +02:00
/**********************
* loop to parse all
*********************/
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
// // console.log('data', data)
var jsonConversion = convert.xml2json(data, { compact: true, spaces: 2 })
// // console.log('jsonConversion keys', Object.keys(jsonConversion))
// console.log('jsonConversion.length', parseFloat(jsonConversion.length / 1024 / 1024, 2), 'Mo')
2023-03-14 18:04:54 +01:00
2023-09-21 22:00:15 +02:00
writeFileInOuputFolderFromJsonObject(outputFileJson, jsonConversion)
convertJsonToCsvAndPersistTSVFile(outputFileJson, outputFileCsv)
2023-03-14 18:04:54 +01:00
2023-09-21 22:00:15 +02:00
})
2023-03-14 18:04:54 +01:00
2023-09-21 22:00:15 +02:00
} else if (err.code === 'ENOENT') {
// file does not exist
console.error(`le fichier ${sourceFilePath} est introuvable. Impossible d en extraire des infos.`, err)
2023-03-14 18:04:54 +01:00
2023-09-21 22:00:15 +02:00
} else {
console.log('Some other error: ', err.code)
}
})
2023-03-14 18:04:54 +01:00
}
2023-09-21 18:15:45 +02:00
openSourceFile()