Skip to content

Commit

Permalink
Fixed issues with dates, compiling correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
russoedu committed Jul 28, 2021
1 parent 453139f commit 036e1aa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ _chat.txt
/chart-month.*
/chart-year.*
build
WhatsappChatConverter-*
WhatsappChatConverter*
WhatsappConversion
12 changes: 9 additions & 3 deletions src/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Terminal } from './Terminal.js'
import csvStringify from 'csv-stringify'
import chalk from 'chalk'

const folder = './WhatsappConversion/'
/**
* File manipulation class
*/
Expand All @@ -12,6 +13,11 @@ class File {
* @param {string} filePath The chat export path
*/
constructor (filePath) {
try {
fs.mkdirSync(folder)
} catch (error) {
// Folder already exists
}
try {
/**
* @type string
Expand Down Expand Up @@ -51,11 +57,11 @@ class File {
* @param {string} name
*/
static saveJson (data, name) {
fs.writeFileSync(name, JSON.stringify(data, null, 2))
fs.writeFileSync(folder + name, JSON.stringify(data, null, 2))
}

static saveHtml (html, name) {
fs.writeFileSync(name, html)
fs.writeFileSync(folder + name, html)
}

/**
Expand All @@ -82,7 +88,7 @@ class File {
console.log(err)
reject(err)
} else {
fs.writeFileSync('result.csv', output)
fs.writeFileSync(folder + 'result.csv', output)
resolve(true)
}
})
Expand Down
27 changes: 26 additions & 1 deletion src/Whatsapp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Contact } from './Contact.js'
import { Message } from './Message.js'
import { Progress } from './Progress.js'

const dateFormat = {
DAY_MONTH_YEAR: 0,
MONTH_DAY_YEAR: 1,
}
/**
* Whatsapp messages manipulation class
*/
Expand All @@ -9,6 +14,7 @@ class Whatsapp {
#messageRegEx = /(\[\d{2}\/\d{2}\/\d{4},\s\d{2}:\d{2}:\d{2}\])\s/gm
#contentSplitRegex = /\[(\d{2})\/(\d{2})\/(\d{4}),\s(\d{2}):(\d{2}):(\d{2})\]\s(.+?):\s([\s\S]+)/
#contacts = {}
#dateFormat = dateFormat.DAY_MONTH_YEAR

/**
* @param {import('./File.js').File} file file The exported file
Expand Down Expand Up @@ -60,6 +66,21 @@ class Whatsapp {
progress.update()
}

/**
* Read all messages to try and identify the date format
* Changes this.#dateFormat to 'm' if any date has the second element bigger than 12,
* meaning the day is the second element, not the first
*/
#setDateFormat () {
for (const message of this.messages) {
const split = this.#contentSplitRegex.exec(message)
if (!!split && split[2] > 12) {
this.#dateFormat = dateFormat.MONTH_DAY_YEAR
break
}
}
}

/**
* Set each message as a string as a Message entry
*/
Expand All @@ -81,6 +102,8 @@ class Whatsapp {
const contact = new Contact()
const replacementsFileContent = []

this.#setDateFormat()

// Replace each entry by the Message instance and remove the null entries
const contentCleaningProgress = new Progress('Splitting date, contact and message', this.messages.length)
this.messages = this.messages
Expand All @@ -91,7 +114,9 @@ class Whatsapp {
return null
}

const date = new Date(Date.UTC(split[3], split[2], split[1], split[4], split[5], split[6])).toLocaleString().replace(',', '')
const date = this.#dateFormat === dateFormat.DAY_MONTH_YEAR
? `${split[1]}/${split[2]}/${split[3]} ${split[4]}:${split[5]}:${split[6]}`
: `${split[2]}/${split[1]}/${split[3]} ${split[4]}:${split[5]}:${split[6]}`

let cont = Contact.clean(split[7])
const content = split[8]
Expand Down

0 comments on commit 036e1aa

Please sign in to comment.