/** * conversion de sms en XML exportés par Sms backup and restore * au format des évènements csv */ import fs from 'node-fs' import convert from 'xml-js' import { writeFileInOuputFolderFromJsonObject } from './utils.mjs' /********************** * initialize configs **********************/ // const sourceFileBaseName = 'sms-20180423162531' const sourceFileBaseName = 'sms' 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 let headers = [] let tasksObjectsForJsonExport = [] let headersByKind = {} let writeJsonAfterParse = false writeJsonAfterParse = true /************************************************************** * fetch the source file to read its contents *************************************************************/ // console.log('parse some org file', sourceFilePath) if (!sourceFilePath) { console.error('pas de fichier à ouvrir') } 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) }) } /** * convert a json object to a tsv file and save it * @param jsonFilePath * @param outputFileTSVName */ function convertJsonToCsvAndPersistTSVFile (jsonFilePath, outputFileTSVName) { console.log('convert', folder_base+'output/' + jsonFilePath) fs.readFile(folder_base+'output/' + jsonFilePath, 'utf8', function (err, data) { console.log('data', data.length) 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)) let smslist = data['elements'][1]['elements'] if (smslist) { smslist.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.log('no sms in data') } } else { console.error('no data in xml file ' + jsonFilePath) } }) } function openSourceFile () { fs.stat(sourceFilePath, function (err, data) { if (err == null) { // console.log(`File ${sourceFilePath} exists`) /********************** * loop to parse all *********************/ fs.readFile(sourceFilePath, 'utf8', function (err, data) { // // console.log('data', data) var jsonConversion = convert.xml2json(data) console.log('jsonConversion keys', Object.keys(jsonConversion)) // console.log('jsonConversion.length', parseFloat(jsonConversion.length / 1024 / 1024, 2), 'Mo') writeFileInOuputFolderFromJsonObject(outputFileJson, JSON.parse( jsonConversion)) convertJsonToCsvAndPersistTSVFile(outputFileJson, outputFileCsv) }) } else if (err.code === 'ENOENT') { // file does not exist console.error(`le fichier ${sourceFilePath} est introuvable. Impossible d en extraire des infos.`, err) } else { console.log('Some other error: ', err.code) } }) } openSourceFile() // convertJsonToCsvAndPersistTSVFile(outputFileJson, outputFileCsv)