Skip to content

Instantly share code, notes, and snippets.

@neongreen
Created May 19, 2024 16:07
Show Gist options
  • Save neongreen/4548582769122e4ce2adc18e1ff588dc to your computer and use it in GitHub Desktop.
Save neongreen/4548582769122e4ce2adc18e1ff588dc to your computer and use it in GitHub Desktop.
// deno run -A go.ts <file.json> | json2csv > @result.csv
function readJson(filePath: string) {
return JSON.parse(Deno.readTextFileSync(filePath))
}
async function processMessages(filePath: string) {
const data = await readJson(filePath)
const allEmojis = new Set<string>()
// Collect all possible emojis
data.messages.forEach((message: any) => {
;(message.reactions?.results || []).forEach((reaction: any) => {
allEmojis.add(reaction.reaction.emoticon)
})
})
const processed = data.messages.map((message: any) => {
const reactions = message.reactions?.results || []
const reactionsObj = Array.from(allEmojis).reduce(
(acc: any, emoji: string) => {
acc[emoji] = 0
return acc
},
{}
)
reactions.forEach((reaction: any) => {
reactionsObj[reaction.reaction.emoticon] = reaction.count
})
const reactionsTotal = reactions.reduce(
(sum: number, cur: any) => sum + cur.count,
0
)
return {
message: message.message,
date: message.date,
...reactionsObj,
reactions_total: reactionsTotal,
}
})
console.log(JSON.stringify(processed, null, 2))
}
const filePath = Deno.args[0]
processMessages(filePath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment