change path export sms
This commit is contained in:
parent
bbcb33e531
commit
3d1401662e
179
sms_to_csv.mjs
179
sms_to_csv.mjs
|
@ -1,122 +1,127 @@
|
||||||
import fs from "node-fs";
|
import fs from 'node-fs'
|
||||||
import convert from "xml-js";
|
import convert from 'xml-js'
|
||||||
import {writeFileInOuputFolderFromJsonObject} from "./utils.mjs";
|
import { writeFileInOuputFolderFromJsonObject } from './utils.mjs'
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* initialize configs
|
* initialize configs
|
||||||
**********************/
|
**********************/
|
||||||
|
|
||||||
const sourceFileName = 'sms.xml'
|
const sourceFileBaseName = 'sms-20180423162531'
|
||||||
const outputFileJson = 'sms-20180423162531.json'
|
const sourceFileName = `${sourceFileBaseName}.xml`
|
||||||
const outputFileCsv = 'sms-20180423162531.tsv'
|
const outputFileJson = `${sourceFileBaseName}.json`
|
||||||
const outputFileJsonPathFull = '/home/cipherbliss/Nextcloud/ressources/social sorting/output/' + outputFileJson
|
const outputFileCsv = `${sourceFileBaseName}.tsv`
|
||||||
const sourceFileJson = '/home/cipherbliss/Nextcloud/ressources/social sorting/output/' + outputFileJson
|
const folder_base = '/home/tykayn/Nextcloud/ressources/social sorting/'
|
||||||
const sourceFilePath = '/home/cipherbliss/Nextcloud/ressources/social sorting/' + sourceFileName;
|
|
||||||
|
const outputFileJsonPathFull = `${folder_base}output/${outputFileJson}`
|
||||||
|
const sourceFileJson = `${folder_base}output/${outputFileJson}`
|
||||||
|
const sourceFilePath = folder_base + sourceFileName
|
||||||
|
|
||||||
let headers = []
|
let headers = []
|
||||||
let tasksObjectsForJsonExport = []
|
let tasksObjectsForJsonExport = []
|
||||||
let headersByKind = {}
|
let headersByKind = {}
|
||||||
let writeJsonAfterParse = false;
|
let writeJsonAfterParse = false
|
||||||
writeJsonAfterParse = true;
|
writeJsonAfterParse = true
|
||||||
|
|
||||||
/**************************************************************
|
/**************************************************************
|
||||||
* fetch the source file to read its contents
|
* fetch the source file to read its contents
|
||||||
*************************************************************/
|
*************************************************************/
|
||||||
|
|
||||||
console.log('parse some org file', sourceFilePath)
|
// console.log('parse some org file', sourceFilePath)
|
||||||
if (!sourceFilePath) {
|
if (!sourceFilePath) {
|
||||||
console.error('pas de fichier à ouvrir')
|
console.error('pas de fichier à ouvrir')
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertToCsv(elementsArray) {
|
function convertToCsv (elementsArray) {
|
||||||
elementsArray.forEach(item => {
|
elementsArray.forEach(item => {
|
||||||
console.log('item._attributes.date_sent', item._attributes.date_sent)
|
// console.log('item._attributes.date_sent', item._attributes.date_sent)
|
||||||
console.log('item._attributes.contact_name', item._attributes.date_sent)
|
// console.log('item._attributes.contact_name', item._attributes.date_sent)
|
||||||
console.log('item._attributes.body', item._attributes.date_sent)
|
// console.log('item._attributes.body', item._attributes.date_sent)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertJsonToCsv(sourceFilePath, outputFileName) {
|
function convertJsonToCsvAndPersistTSVFile (jsonFilePath, outputFileTSVName) {
|
||||||
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
// console.log('convertJsonToCsvAndWriteItAs', jsonFilePath, outputFileTSVName)
|
||||||
|
|
||||||
console.log('data', data)
|
fs.readFile( folder_base+ jsonFilePath, 'utf8', function (err, data) {
|
||||||
if (data) {
|
|
||||||
|
|
||||||
const events = [
|
// console.log('data', data)
|
||||||
|
if (data) {
|
||||||
|
|
||||||
'amount\t' +
|
const events = [
|
||||||
'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
|
'amount\t' +
|
||||||
events.push(
|
'content\t' +
|
||||||
'\t' +
|
'description\t' +
|
||||||
item._attributes.body.replace('\n', ' ') + '\t' +
|
'destination\t' +
|
||||||
'sms ' + item._attributes.address + ' le ' + item._attributes.readable_date + '\t' +
|
'end\t' +
|
||||||
item._attributes.address + '\t' +
|
'kind of activity\t' +
|
||||||
'' + '\t' +
|
'person\t' +
|
||||||
'' + '\t' +
|
'place\t' +
|
||||||
item._attributes.contact_name + '\t' +
|
'source\t' +
|
||||||
'' + '\t' +
|
'start\t' +
|
||||||
'' + '\t' +
|
'unique id\t' +
|
||||||
'' + '\t' +
|
'url\t'
|
||||||
'' + '\t' +
|
]
|
||||||
'' + '\t'
|
data = JSON.parse(data)
|
||||||
)
|
// console.log('data.length', data.length)
|
||||||
})
|
// // console.log('data', Object.keys(data[0]))
|
||||||
console.log('events', events)
|
data['smses']['sms'].forEach(item => {
|
||||||
writeFileInOuputFolderFromJsonObject(outputFileName, events.join("\n"))
|
|
||||||
|
|
||||||
} else {
|
// convert all fields to common event description
|
||||||
console.error('no data in xml file ' + sourceFilePath)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function openSourceFile() {
|
function openSourceFile () {
|
||||||
|
|
||||||
fs.stat(sourceFilePath, function (err, data) {
|
fs.stat(sourceFilePath, function (err, data) {
|
||||||
if (err == null) {
|
if (err == null) {
|
||||||
console.log(`File ${sourceFilePath} exists`);
|
// console.log(`File ${sourceFilePath} exists`)
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* loop to parse all
|
* loop to parse all
|
||||||
*********************/
|
*********************/
|
||||||
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
fs.readFile(sourceFilePath, 'utf8', function (err, data) {
|
||||||
// console.log('data', data)
|
// // console.log('data', data)
|
||||||
var jsonConversion = convert.xml2json(data, {compact: true, spaces: 2});
|
var jsonConversion = convert.xml2json(data, { compact: true, spaces: 2 })
|
||||||
// console.log('smses', Object.keys(jsonConversion))
|
// // console.log('jsonConversion keys', Object.keys(jsonConversion))
|
||||||
console.log('jsonConversion[0]', jsonConversion['10'])
|
// console.log('jsonConversion.length', parseFloat(jsonConversion.length / 1024 / 1024, 2), 'Mo')
|
||||||
|
|
||||||
writeFileInOuputFolderFromJsonObject(outputFileJsonPathFull, jsonConversion)
|
writeFileInOuputFolderFromJsonObject(outputFileJson, jsonConversion)
|
||||||
convertJsonToCsv(sourceFileJson, outputFileCsv)
|
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 if (err.code === 'ENOENT') {
|
} else {
|
||||||
// file does not exist
|
console.log('Some other error: ', err.code)
|
||||||
console.error(`le fichier ${sourceFilePath} est introuvable. Impossible d en extraire des infos.`, err);
|
}
|
||||||
|
})
|
||||||
} else {
|
|
||||||
console.log('Some other error: ', err.code);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
openSourceFile()
|
openSourceFile()
|
||||||
|
|
17
utils.mjs
17
utils.mjs
|
@ -1,4 +1,4 @@
|
||||||
import fs from "node-fs";
|
import fs from 'node-fs'
|
||||||
|
|
||||||
export const headersTsv = 'amount\t' +
|
export const headersTsv = 'amount\t' +
|
||||||
'content\t' +
|
'content\t' +
|
||||||
|
@ -13,22 +13,23 @@ export const headersTsv = 'amount\t' +
|
||||||
'unique id\t' +
|
'unique id\t' +
|
||||||
'url\t'
|
'url\t'
|
||||||
|
|
||||||
const outputAbsolutePath = '/home/tykayn/Nextcloud/ressources/social sorting/output/';
|
const outputAbsolutePath = '/home/tykayn/Nextcloud/ressources/social sorting/output/'
|
||||||
|
|
||||||
export async function writeFileInOuputFolderFromJsonObject(fileName, jsonObjectThing) {
|
export async function writeFileInOuputFolderFromJsonObject (fileName, jsonObjectThing) {
|
||||||
|
|
||||||
// console.log('statistics.dates', statistics.dates)
|
// console.log('statistics.dates', statistics.dates)
|
||||||
|
let fullpath = `${outputAbsolutePath}${fileName}`
|
||||||
|
|
||||||
return await fs.writeFile(
|
return await fs.writeFile(
|
||||||
`${outputAbsolutePath}${fileName}`,
|
fullpath,
|
||||||
JSON.stringify(jsonObjectThing, null, 2),
|
JSON.stringify(jsonObjectThing, null, 2),
|
||||||
"utf8",
|
'utf8',
|
||||||
(err) => {
|
(err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(`Error writing file: ${err}`);
|
console.log(`Error writing file: ${err}`)
|
||||||
} else {
|
} else {
|
||||||
console.log(`File ${fileName} is written successfully!`);
|
console.log(`File ${fileName} is written successfully! \n\n ${fullpath}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue