Created
May 19, 2024 16:07
-
-
Save neongreen/4548582769122e4ce2adc18e1ff588dc to your computer and use it in GitHub Desktop.
https://github.com/estebanpdl/telegram-tracker data converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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