Skip to content

Instantly share code, notes, and snippets.

@abagali1
Created August 19, 2020 00:00
Show Gist options
  • Save abagali1/30214fca06c6feb018e704423325efed to your computer and use it in GitHub Desktop.
Save abagali1/30214fca06c6feb018e704423325efed to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os, glob, json, pickle, sys, datetime
from shutil import rmtree
def overview():
groups, dms = {}, {}
for name in os.listdir('.'):
if not os.path.isdir(name):
continue
files = glob.glob(f"{name}/message_*.json")
if not files:
rmtree(name)
else:
for f in files:
content = json.load(open(f))
key = name[:name.find("_")]
length = len(content["messages"])
if length < 10:
continue
if len(content["participants"]) > 2:
if key in groups:
groups[key] += length
else:
groups[key] = length
else:
if key in dms:
dms[key] += length
else:
dms[key] = length
pickle.dump(groups, open('groups.pkl','wb'))
pickle.dump(dms,open('dms.pkl','wb'))
for pos, group in enumerate(sorted(groups, key=lambda x: -groups.get(x))):
print(f"{pos}. {group} => {groups.get(group)}")
print()
for pos, dm in enumerate(sorted(dms,key=lambda x: -dms.get(x))):
print(f"{dm} {dms.get(dm)}")
by_year, by_month, by_week, by_day = {}, {}, {}, {}
def overtime(person, base_yr):
global by_year, by_month, by_week, by_day
messages_dir = [x for x in glob.glob(f"{person}*") if os.path.isdir(x)]
if not messages_dir:
print("Cant find person")
return
files = glob.glob(f"{messages_dir[0]}/message_*.json")
if not files:
print(messages_dir)
print("No messages")
return
for f in files:
content = json.load(open(f))
for message in content["messages"]:
date = datetime.datetime.fromtimestamp(message["timestamp_ms"]/1000.0)
year, month, week, day = date.year, date.month, date.isocalendar()[1], date.timetuple().tm_yday
if year > base_yr:
month += 12*(year-base_yr)
week += 52*(year-base_yr)
day += 365*(year-base_yr)
if day == 259:
print(message.get("content", None), message["timestamp_ms"], end='\n')
if year in by_year:
by_year[year] += 1
else:
by_year[year] = 1
if month in by_month:
by_month[month] += 1
else:
by_month[month] = 1
if week in by_week:
by_week[week] += 1
else:
by_week[week] = 1
if day in by_day:
by_day[day] += 1
else:
by_day[day] = 1
for name, d in {'by_year': by_year, 'by_month': by_month, 'by_week': by_week, 'by_day': by_day}.items():
with open(f"../{person}_{name}.txt", "w") as w:
for k in sorted(d):
w.write(f"{k},{d.get(k)}\n")
def main():
if len(sys.argv) == 3:
overtime(sys.argv[1], int(sys.argv[2]))
else:
overview()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment