up convert sms

This commit is contained in:
Tykayn 2023-09-21 18:15:45 +02:00 committed by tykayn
parent a491c12d15
commit bbcb33e531
2 changed files with 52 additions and 89 deletions

View File

@ -1,16 +1,17 @@
import fs from "node-fs"; import fs from "node-fs";
import convert from "xml-js"; import convert from "xml-js";
import {writeFileInOuputFolderFromJsonObject} from "./parse_orgmode_to_json.mjs"; import {writeFileInOuputFolderFromJsonObject} from "./utils.mjs";
/********************** /**********************
* initialize configs * initialize configs
**********************/ **********************/
const sourceFileName = 'sms-20180423162531.xml' const sourceFileName = 'sms.xml'
const outputFileJson = 'sms-20180423162531.json' const outputFileJson = 'sms-20180423162531.json'
const sourceFileJson = 'output/' + outputFileJson
const outputFileCsv = 'sms-20180423162531.tsv' 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 headers = []
let tasksObjectsForJsonExport = [] let tasksObjectsForJsonExport = []
@ -38,44 +39,51 @@ function convertToCsv(elementsArray) {
function convertJsonToCsv(sourceFilePath, outputFileName) { function convertJsonToCsv(sourceFilePath, outputFileName) {
fs.readFile(sourceFilePath, 'utf8', function (err, data) { 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', data)
// console.log('data', Object.keys(data[0])) if (data) {
data['smses']['sms'].forEach(item => {
// convert all fields to common event description const events = [
events.push(
'\t' + 'amount\t' +
item._attributes.body.replace('\n', ' ') + '\t' + 'content\t' +
'sms ' + item._attributes.address + ' le ' + item._attributes.readable_date + '\t' + 'description\t' +
item._attributes.address + '\t' + 'destination\t' +
'' + '\t' + 'end\t' +
'' + '\t' + 'kind of activity\t' +
item._attributes.contact_name + '\t' + 'person\t' +
'' + '\t' + 'place\t' +
'' + '\t' + 'source\t' +
'' + '\t' + 'start\t' +
'' + '\t' + 'unique id\t' +
'' + '\t' 'url\t'
) ];
}) data = JSON.parse(data)
console.log('events', events) console.log('data', data)
writeFileInOuputFolderFromJsonObject(outputFileName, events.join("\n")) // 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('smses', Object.keys(jsonConversion))
console.log('jsonConversion[0]', jsonConversion['10']) console.log('jsonConversion[0]', jsonConversion['10'])
writeFileInOuputFolderFromJsonObject(outputFileJson, jsonConversion) writeFileInOuputFolderFromJsonObject(outputFileJsonPathFull, jsonConversion)
convertJsonToCsv(sourceFileJson, outputFileCsv) convertJsonToCsv(sourceFileJson, outputFileCsv)
}) })
@ -111,49 +119,4 @@ function openSourceFile() {
} }
// 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"))
})
}

View File

@ -20,8 +20,8 @@ export async function writeFileInOuputFolderFromJsonObject(fileName, jsonObjectT
// console.log('statistics.dates', statistics.dates) // console.log('statistics.dates', statistics.dates)
return await fs.writeFile( return await fs.writeFile(
`${fileName}`, `${outputAbsolutePath}${fileName}`,
JSON.stringify(jsonObjectThing), JSON.stringify(jsonObjectThing, null, 2),
"utf8", "utf8",
(err) => { (err) => {
if (err) { if (err) {