2023-09-21 22:24:04 +02:00
|
|
|
/**
|
|
|
|
* conversion de sms en XML exportés par Sms backup and restore
|
|
|
|
* au format des évènements csv
|
|
|
|
*/
|
|
|
|
|
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:24:04 +02:00
|
|
|
// const sourceFileBaseName = 'sms-20180423162531'
|
2023-09-22 10:16:46 +02:00
|
|
|
const sourceFileBaseName = 'sms-20230921224605'
|
2023-09-21 22:00:15 +02:00
|
|
|
const sourceFileName = `${sourceFileBaseName}.xml`
|
|
|
|
const outputFileJson = `${sourceFileBaseName}.json`
|
2023-09-22 10:16:46 +02:00
|
|
|
const outputFileCsv = `${sourceFileBaseName}.csv`
|
2023-09-21 22:00:15 +02:00
|
|
|
const folder_base = '/home/tykayn/Nextcloud/ressources/social sorting/'
|
2023-09-22 10:16:46 +02:00
|
|
|
const csv_separator = ';'
|
2023-09-21 22:00:15 +02:00
|
|
|
|
|
|
|
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:24:04 +02:00
|
|
|
/**
|
|
|
|
* convert a json object to a tsv file and save it
|
|
|
|
* @param jsonFilePath
|
|
|
|
* @param outputFileTSVName
|
|
|
|
*/
|
2023-09-21 22:00:15 +02:00
|
|
|
function convertJsonToCsvAndPersistTSVFile (jsonFilePath, outputFileTSVName) {
|
|
|
|
|
2023-09-21 22:24:04 +02:00
|
|
|
console.log('convert', folder_base+'output/' + jsonFilePath)
|
|
|
|
fs.readFile(folder_base+'output/' + jsonFilePath, 'utf8', function (err, data) {
|
2023-09-22 10:16:46 +02:00
|
|
|
data = JSON.parse(data)
|
2023-09-21 22:24:04 +02:00
|
|
|
console.log('data', data.length)
|
2023-09-21 22:00:15 +02:00
|
|
|
if (data) {
|
|
|
|
|
|
|
|
const events = [
|
|
|
|
|
2023-09-22 10:16:46 +02:00
|
|
|
'amount' + csv_separator + ''+
|
|
|
|
'content' + csv_separator +''+
|
|
|
|
'description' + csv_separator +
|
|
|
|
'destination' + csv_separator +
|
|
|
|
'end' + csv_separator +
|
|
|
|
'kind of activity' + csv_separator +
|
|
|
|
'person' + csv_separator +
|
|
|
|
'place' + csv_separator +
|
|
|
|
'source' + csv_separator +
|
|
|
|
'start' + csv_separator +
|
|
|
|
'unique id' + csv_separator +
|
|
|
|
'url' + csv_separator
|
2023-09-21 22:00:15 +02:00
|
|
|
]
|
2023-09-22 10:16:46 +02:00
|
|
|
|
|
|
|
// console.log('data', data)
|
|
|
|
// console.log('data other ', data)
|
|
|
|
console.log('data.length', data['elements'].length)
|
|
|
|
console.log('data name', data['elements'][2]['elements'].length)
|
|
|
|
let smslist = data['elements'][2]['elements']
|
|
|
|
// let smslist = data['smses']
|
2023-09-21 22:24:04 +02:00
|
|
|
if (smslist) {
|
|
|
|
smslist.forEach(item => {
|
|
|
|
|
|
|
|
// convert all fields to common event description
|
|
|
|
events.push(
|
2023-09-22 10:16:46 +02:00
|
|
|
''+csv_separator +
|
|
|
|
item.attributes.body + csv_separator +
|
|
|
|
'sms ' + item.attributes.address + ' le ' + item.attributes.readable_date + csv_separator +
|
|
|
|
item.attributes.address + csv_separator +
|
|
|
|
'' + csv_separator +
|
|
|
|
'' + csv_separator +
|
|
|
|
item.attributes.contact_name + csv_separator +
|
|
|
|
'' + csv_separator +
|
|
|
|
'' + csv_separator +
|
|
|
|
'' + csv_separator +
|
|
|
|
'' + csv_separator +
|
|
|
|
'' + csv_separator
|
2023-09-21 22:24:04 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
// // console.log('events', events)
|
|
|
|
writeFileInOuputFolderFromJsonObject(outputFileTSVName, events.join('\n')).then(res => {
|
|
|
|
// console.log('res', typeof res)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log('no sms in data')
|
|
|
|
}
|
2023-09-21 22:00:15 +02:00
|
|
|
|
|
|
|
} 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)
|
2023-09-21 22:24:04 +02:00
|
|
|
var jsonConversion = convert.xml2json(data)
|
2023-09-22 10:16:46 +02:00
|
|
|
// console.log('jsonConversion 0', Object.keys(jsonConversion['elements']))
|
2023-09-21 22:00:15 +02:00
|
|
|
// console.log('jsonConversion.length', parseFloat(jsonConversion.length / 1024 / 1024, 2), 'Mo')
|
2023-03-14 18:04:54 +01:00
|
|
|
|
2023-09-22 10:16:46 +02:00
|
|
|
// writeFileInOuputFolderFromJsonObject(outputFileJson, JSON.parse( jsonConversion))
|
|
|
|
writeFileInOuputFolderFromJsonObject(outputFileJson, JSON.parse(jsonConversion))
|
2023-09-21 22:00:15 +02:00
|
|
|
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()
|
2023-09-21 22:24:04 +02:00
|
|
|
// convertJsonToCsvAndPersistTSVFile(outputFileJson, outputFileCsv)
|