Skip to content

Instantly share code, notes, and snippets.

@sachk
Last active March 15, 2022 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sachk/414e1765b13563f9b9cfb525367f026a to your computer and use it in GitHub Desktop.
Save sachk/414e1765b13563f9b9cfb525367f026a to your computer and use it in GitHub Desktop.
bar chart race for discord messages sent

Instructions

1: Enable devtools in Discord

2: Download DiscordChatExporter and use it to download every channel as JSON

3: Setup a virtualenv if you want

4: Install dependancies with pip install simdjson pandas git+https://github.com/programiz/bar_chart_race.git@master

5: Run load.py modifying it to to use the path of your json files, it'll save a sorted and concatenated version in the current directory

6: Run main.py modifying it to use the path of that json file

import simdjson
import json
from datetime import datetime
import glob
messages = []
for message_file in glob.glob('C:\\Users\\SachK\\Documents\\csesoc\\*.json'):
cur_messages = simdjson.load(open(message_file, encoding="utf-8"), strict=False)
messages.extend(cur_messages['messages'])
def parse_time(date_string):
try:
timestamp = datetime.strptime(date_string[:len(date_string) - 6], '%Y-%m-%dT%H:%M:%S.%f')
except ValueError:
timestamp = datetime.strptime(date_string[:len(date_string) - 6], '%Y-%m-%dT%H:%M:%S')
return timestamp
sorted_messages = sorted(
messages,
key=lambda x: parse_time(x['timestamp'])
)
print(sorted_messages[0])
print(sorted_messages[-1])
with open('cse.json', 'w') as f:
json.dump(sorted_messages, f)
import bar_chart_race as bcr
import pandas as pd
import simdjson as json
from datetime import datetime
import glob
def parse_time(date_string):
try:
timestamp = datetime.strptime(date_string[:len(date_string) - 6], '%Y-%m-%dT%H:%M:%S.%f')
except ValueError:
timestamp = datetime.strptime(date_string[:len(date_string) - 6], '%Y-%m-%dT%H:%M:%S')
return timestamp
message_file = "C:\\Users\\SachK\\Documents\\cse.json"
messages = json.load(open(message_file, encoding="utf-8"), strict=False)
print(len(messages))
cur_day = parse_time(messages[0]['timestamp']).date()
user_names = []
for message in messages:
if not message['author']['name'] in user_names:
user_names.append(message['author']['name'])
print(user_names)
user_temp = [int(0)] * len(user_names)
df = pd.DataFrame(columns=['date'].append(user_names))
for message in messages:
if message['type'] == 'Default':
index = user_names.index(message['author']['name'])
user_temp[index] += 1
if parse_time(message['timestamp']).date() != cur_day:
s = {'date': cur_day.strftime('%d/%m/%Y')}
for user in user_names:
index = user_names.index(user)
s[user] = int(user_temp[index])
df = df.append(s, ignore_index=True)
print(df)
cur_day = parse_time(message['timestamp']).date()
df = df.set_index('date')
df = df.astype(int)
df.fillna(0, inplace=True)
print(df)
bcr.bar_chart_race(
df=df,
filename='csesoc1.mp4',
orientation='h',
sort='desc',
n_bars=16,
period_length=200,
steps_per_period=6,
end_period_pause=0,
interpolate_period=False,
filter_column_colors=False,
title='Messages sent on CSESoc by user')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment