From d1496b5ed90b67aaf3d4b812debe021c510f777f Mon Sep 17 00:00:00 2001 From: Androz2091 Date: Sat, 19 Jun 2021 13:16:38 +0200 Subject: [PATCH] :recycle: Rewrite data storage --- src/App.svelte | 3 +- src/app/extractor.js | 58 +++++++++++++++++++---------- src/app/store.js | 81 +++++++++++++++++++++-------------------- src/views/Help.svelte | 7 ++++ src/views/Loader.svelte | 6 +-- src/views/Stats.svelte | 19 +++++----- 6 files changed, 102 insertions(+), 72 deletions(-) create mode 100644 src/views/Help.svelte diff --git a/src/App.svelte b/src/App.svelte index 368266f..0c71198 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -6,6 +6,7 @@ import Stats from './views/Stats.svelte'; import Loader from './views/Loader.svelte'; + import Help from "./views/Help.svelte"; import Modal from 'svelte-simple-modal'; @@ -30,7 +31,7 @@
- + 404 diff --git a/src/app/extractor.js b/src/app/extractor.js index cc7f864..d593453 100644 --- a/src/app/extractor.js +++ b/src/app/extractor.js @@ -112,11 +112,14 @@ export const extractData = async (files) => { const extractedData = { user: null, - channels: [], - guilds: [], topDMs: [], + topChannels: [], + guildCount: 0, + dmChannelCount: 0, + channelCount: 0, messageCount: 0, + characterCount: 0, totalSpent: 0, hoursValues: [], favoriteWords: null, @@ -148,13 +151,13 @@ export const extractData = async (files) => { // Parse and load current user informations console.log('[debug] Loading user info...'); loadTask.set('Loading user information...'); + extractedData.user = JSON.parse(await readFile('account/user.json')); - await fetchUser(extractedData.user.id).then((fetchedUser) => { - extractedData.user.username = fetchedUser.username; - extractedData.user.discriminator = fetchedUser.discriminator; - extractedData.user.avatar_hash = fetchedUser.avatar; + const fetchedUser = await fetchUser(extractedData.user.id); + extractedData.user.username = fetchedUser.username; + extractedData.user.discriminator = fetchedUser.discriminator; + extractedData.user.avatar_hash = fetchedUser.avatar; - }).catch(() => {}); const confirmedPayments = extractedData.user.payments.filter((p) => p.status === 1); if (confirmedPayments.length) { extractedData.payments.total += confirmedPayments.map((p) => p.amount / 100).reduce((p, c) => p + c); @@ -175,6 +178,7 @@ export const extractData = async (files) => { const isOldPackage = channelsIDsFile[0].name.match(/messages\/(c)?([0-9]{16,32})\/$/)[1] === undefined; const channelsIDs = channelsIDsFile.map((file) => file.name.match(messagesPathRegex)[1]); + const channels = []; let messagesRead = 0; await Promise.all(channelsIDs.map((channelID) => { @@ -198,7 +202,7 @@ export const extractData = async (files) => { const name = messagesIndex[data.id]; const isDM = data.recipients && data.recipients.length === 2; const dmUserID = isDM ? data.recipients.find((userID) => userID !== extractedData.user.id) : undefined; - extractedData.channels.push({ + channels.push({ data, messages, name, @@ -214,18 +218,30 @@ export const extractData = async (files) => { if (messagesRead === 0) throw new Error('invalid_package_missing_messages'); - console.log(`[debug] ${extractedData.channels.length} channels loaded.`); + extractedData.channelCount = channels.filter(c => !c.isDM).length; + extractedData.dmChannelCount = channels.length - extractedData.channelCount; + extractedData.topChannels = channels.filter(c => c.data && c.data.guild).sort((a, b) => b.messages.length - a.messages.length).slice(0, 10).map((channel) => ({ + name: channel.name, + messageCount: channel.messages.length, + guildName: channel.data.guild.name + })); + extractedData.characterCount = channels.map((channel) => channel.messages).flat().map((message) => message.length).reduce((p, c) => p + c); + + for (let i = 0; i < 24; i++) { + extractedData.hoursValues.push(channels.map((c) => c.messages).flat().filter((m) => new Date(m.timestamp).getHours() === i).length); + } + + console.log(`[debug] ${channels.length} channels loaded.`); console.log('[debug] Loading guilds...'); loadTask.set('Loading joined servers...'); const guildIndex = JSON.parse(await readFile('servers/index.json')); - const guilds = Object.entries(guildIndex).map(g => ({ id: g[0], name: g[1] })); - extractedData.guilds = guilds; + extractedData.guildCount = Object.keys(guildIndex).length; - console.log(`[debug] ${guilds.length} guilds loaded`); + console.log(`[debug] ${extractedData.guildCount} guilds loaded`); - const words = extractedData.channels.map((channel) => channel.messages).flat().map((message) => message.words).flat().filter((w) => w.length > 5); + const words = channels.map((channel) => channel.messages).flat().map((message) => message.words).flat().filter((w) => w.length > 5); extractedData.favoriteWords = getFavoriteWords(words); for (let wordData of extractedData.favoriteWords) { const userID = parseMention(wordData.word); @@ -241,14 +257,20 @@ export const extractData = async (files) => { console.log('[debug] Fetching top DMs...'); loadTask.set('Loading user activity...'); - extractedData.topDMs = extractedData.channels + extractedData.topDMs = channels .filter((channel) => channel.isDM) .sort((a, b) => b.messages.length - a.messages.length) - .slice(0, 10); + .slice(0, 10) + .map((channel) => ({ + id: channel.data.id, + dmUserID: channel.dmUserID, + messageCount: channel.messages.length, + userData: null + })); await Promise.all(extractedData.topDMs.map((channel) => { return new Promise((resolve) => { fetchUser(channel.dmUserID).then((userData) => { - const channelIndex = extractedData.topDMs.findIndex((c) => c.data.id === channel.data.id); + const channelIndex = extractedData.topDMs.findIndex((c) => c.id === channel.id); extractedData.topDMs[channelIndex].userData = userData; resolve(); }); @@ -276,9 +298,7 @@ export const extractData = async (files) => { loadTask.set('Calculating statistics...'); - for (let i = 0; i < 24; i++) { - extractedData.hoursValues.push(extractedData.channels.map((c) => c.messages).flat().filter((m) => new Date(m.timestamp).getHours() === i).length); - } + console.log(extractedData); return extractedData; }; diff --git a/src/app/store.js b/src/app/store.js index 6ee461a..0fdcea8 100644 --- a/src/app/store.js +++ b/src/app/store.js @@ -1,54 +1,41 @@ import { writable } from 'svelte/store'; -let loadedValue = false; -let dataValue = null; +const storedData = localStorage.getItem('data') || null; + +let dataValue = storedData && JSON.parse(storedData); + const isDemo = window.location.href.includes('demo'); const removeAnalytics = window.location.href.includes('noanalytics'); if (isDemo) { const randomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); - loadedValue = true; + const demoUserObject = { id: 422820341791064085, username: 'Wumpus', discriminator: '0000', avatar: null }; - const demoMessageObject = { - id: 422820341791064085, - timestamp: 1613810737577, - length: randomNumber(200, 600) - }; - const analytics = removeAnalytics ? {} : { - openCount: randomNumber(200, 300), - averageOpenCountPerDay: randomNumber(3, 5), - notificationCount: randomNumber(200, 400), - joinVoiceChannelCount: randomNumber(40, 100), - joinCallCount: randomNumber(20, 30), - addReactionCount: randomNumber(100, 200), - messageEditedCount: randomNumber(50, 70), - sentMessageCount: randomNumber(200, 600), - averageMessageCountPerDay: randomNumber(20, 30), - slashCommandUsedCount: randomNumber(10, 20) - }; dataValue = { + isDemo: true, + user: demoUserObject, - channels: new Array(randomNumber(200, 600)).fill({}).map(() => ({ - isDM: true, - name: 'just-chatting', - data: { - guild: { - name: 'Cool Discord' - } - }, - messages: new Array(randomNumber(200, 600)).fill(demoMessageObject) - })), - guilds: new Array(randomNumber(10, 100)), - applications: [], + topDMs: new Array(10).fill({}).map(() => ({ - userData: demoUserObject, - messages: new Array(randomNumber(200, 10000)).fill(demoMessageObject) - })).sort((a, b) => b.messages.length - a.messages.length), + messageCount: randomNumber(200, 600), + userData: demoUserObject + })), + topChannels: new Array(10).fill({}).map(() => ({ + messageCount: randomNumber(200, 600), + name: 'awesome', + guildName: 'AndrozDev' + })), + guildCount: randomNumber(10, 200), + dmChannelCount: randomNumber(30, 50), + channelCount: randomNumber(50, 100), + messageCount: randomNumber(300, 600), + characterCount: randomNumber(4000, 10000), + totalSpent: randomNumber(100, 200), hoursValues: new Array(24).fill(0).map(() => Math.floor(Math.random() * 300) + 1), favoriteWords: [ { @@ -61,14 +48,30 @@ if (isDemo) { } ], payments: { - total: 500, - list: 'Super Mega Nitro ($500)' + total: 0, + list: '' }, - ...analytics + + ...(!removeAnalytics && { + openCount: randomNumber(200, 300), + averageOpenCountPerDay: randomNumber(3, 5), + notificationCount: randomNumber(200, 400), + joinVoiceChannelCount: randomNumber(40, 100), + joinCallCount: randomNumber(20, 30), + addReactionCount: randomNumber(100, 200), + messageEditedCount: randomNumber(50, 70), + sentMessageCount: randomNumber(200, 600), + averageMessageCountPerDay: randomNumber(20, 30), + slashCommandUsedCount: randomNumber(10, 20) + }) }; } -export const loaded = writable(loadedValue); export const loadTask = writable(null); export const loadEstimatedTime = writable(null); export const data = writable(dataValue); + +data.subscribe((value) => { + if (!value) localStorage.removeItem('data'); + else if (!value.isDemo) localStorage.setItem('data', JSON.stringify(value)); +}); diff --git a/src/views/Help.svelte b/src/views/Help.svelte new file mode 100644 index 0000000..f544454 --- /dev/null +++ b/src/views/Help.svelte @@ -0,0 +1,7 @@ + + +
+ To get +
diff --git a/src/views/Loader.svelte b/src/views/Loader.svelte index a73f212..9883a65 100644 --- a/src/views/Loader.svelte +++ b/src/views/Loader.svelte @@ -1,7 +1,8 @@
@@ -50,7 +49,7 @@

Your favorite words are @@ -145,8 +144,8 @@ strokeLinecap="square" strokeLinejoin="square" content="You have spoken in % different text channels" - count="{ channels.length }" - explanation="That's ~{ Math.round(channels.length / $data.guilds.length) } per guild!" + count="{ $data.channelCount }" + explanation="That's ~{ Math.round($data.channelCount / $data.guildCount) } per guild!" /> @@ -157,19 +156,19 @@ avatarURL={generateAvatarURL(channel.userData.avatar, channel.userData.id, channel.userData.discriminator)} name={channel.userData.username} discriminator={channel.userData.discriminator} - count={channel.messages.length.toLocaleString('en-US')} + count={channel.messageCount.toLocaleString('en-US')} /> {/each} - {#each $data.channels.filter(c => c.data && c.data.guild).sort((a, b) => b.messages.length - a.messages.length).slice(0, 10) as channel, i} + {#each $data.topChannels as channel, i} {/each}