Skip to content

Instantly share code, notes, and snippets.

@kskrueger
Last active August 1, 2021 12:20
Show Gist options
  • Save kskrueger/e7b1f68a71b43df8de3aa8146309587f to your computer and use it in GitHub Desktop.
Save kskrueger/e7b1f68a71b43df8de3aa8146309587f to your computer and use it in GitHub Desktop.
Analyze discord usage
import json
import csv
from datetime import date
import numpy as np
from glob import glob
from matplotlib import pyplot as plt
join_date = date(2017, 8, 3)
path_to_package_directory = "/Users/kskrueger/PycharmProjects/DiscordLearning/Personal/package-2/"
class Conversation:
def __init__(self, folderpath):
self.messages = []
with open(folderpath + '/messages.csv', encoding="utf8") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
# Format: ID,Timestamp,Contents,Attachments
self.messages.append(row)
if len(self.messages) < 1:
raise Exception("Empty file provided!")
self.messages.pop(0)
self.length = len(self.messages)
self.words = 0
for message in self.messages:
self.words = self.words + len(str(message).split())
def get_date_distribution(self):
messages_dates = np.zeros((date.today() - join_date).days, np.int)
for _, time, message, _ in self.messages:
message_date = date(int(time[:4]), int(time[5:7]), int(time[8:10]))
days = (message_date - join_date).days
messages_dates[days] = messages_dates[days] + 1
return messages_dates
with open(path_to_package_directory + 'messages/index.json', encoding="utf8") as json_data:
name_map = json.load(json_data)
json_data.close()
conversations = {}
all_running_total = np.zeros((date.today() - join_date).days, np.int)
total_words = 0
for x in glob(path_to_package_directory + 'messages/*/'):
a = conversations[str(x[-19:-1])] = Conversation(x).get_date_distribution()
total_words += Conversation(x).words
all_running_total = all_running_total + a
# Plot function is used by others to plot all conversations in a plot_list variable
# total=False will plot the rate per day istead of running_total of messages
def plot(plot_list, total=True):
if len(plot_list) < 1:
print("Invalid list to plot")
return
plt.figure()
legend_names = []
for id, value in plot_list.items():
if id in name_map.keys():
name = str(name_map[id])
else:
name = id
if 'with' in name:
name = name[name.index('with') + 5:]
legend_names.append(name)
messages_day = [0]
for x in value:
messages_day.append(x + messages_day[-1] if total else x)
plt.plot(range(0, len(messages_day) - 1), messages_day[:-1])
plt.title("Activity by total")
plt.xlabel("Days since joining discord")
plt.ylabel("Total Messages")
plt.legend(list(legend_names))
plt.show()
# Plot only conversations that are over a certain threshold of messages (ex: plot_thresh(1000))
def plot_thresh(thresh, total=True):
plot_list = {}
for k, v in conversations.items():
if sum(v) > thresh:
plot_list[k] = v
plot(plot_list, total)
# Plot only conversations of target usernames/channels
# (this is a rough matching for username/channel name and could break for duplicates?)
# ex: plot_names(['Karter', 'general-robotics', 'Ilan 9421#0431'])
def plot_names(names_in, total=True):
plot_list = {}
id_list = None
for name_in in names_in:
for id, name in name_map.items():
if str(name_in) in str(name):
id_list = id
for k, v in conversations.items():
if str(id_list) == str(k):
plot_list[k] = v
plot(plot_list, total)
# Plot a running total of all messages sent
plot({'ALL HISTORY': all_running_total})
# Plot daily rate graph (by adding total=False)
plot({'ALL HISTORY': all_running_total}, total=False)
# Examples
plot_thresh(3000) # plot all conversations over 3000 message threshold
plot_names(['general-robotics', 'Karter']) # plot these conversations
# Scary statistics of days without discord and words sent
days = 0
for day_total in all_running_total:
if day_total < 1:
days = days + 1
print("Days without going on discord: " + str(days))
print("Total words sent on discord: " + str(total_words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment