Skip to content

Instantly share code, notes, and snippets.

@HaiderZaidiDev
Created February 4, 2022 01:52
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 HaiderZaidiDev/4aff97b6c97815fbd3eba114778309c4 to your computer and use it in GitHub Desktop.
Save HaiderZaidiDev/4aff97b6c97815fbd3eba114778309c4 to your computer and use it in GitHub Desktop.
Instagram Group Chat Frequency Calculator
import matplotlib.pyplot as plt
import json
def message_count(*args):
"""
Counts the frequency of messages sent by users in an Instagram group chat,
Parameters
----------
args: str
Filenames for instagram json dumps.
"""
senders = {}
for filename in args:
# Parsing data dumps.
message_content = open(filename)
message_data = json.load(message_content)
# Calculating frequency of messages sent by each user.
for message in message_data['messages']:
sender = message['sender_name']
if sender in senders:
# If the user is already in the dictionary.
senders[sender] += 1
else:
# If they're not in the dictionary.
senders[sender] = 1
# Compiling data into lists, for matplotlib.
names = [key for key, value in senders.items()]
values = [value for key, value in senders.items()]
# Creating Graph
plt.bar(names, values)
plt.xlabel('Names')
plt.ylabel('# of Messages Sent')
plt.xticks(rotation=90) # Rotating names by 90 degrees.
plt.show()
plt.savefig('academics.png')
message_count('message_1.json', 'message_2.json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment