Chris Adams / May 24 2023
Where are CATS in the world?
We have a growing online community.
It would be cool to see the distribution of timezones though.
! pip install slackclient
import altair
import pandas
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
8.4s
import os
import slack
import json
from collections import Counter
import pprint
class TimeZoneCounter:
client = None
def __init__(self):
# connect to the slack API
self.client = slack.WebClient(token=os.environ['SLACK_API_TOKEN'])
def summary(self):
# make a counter thingo in python
cntr = Counter()
# get a list of our users
# TODO: we need to paginate through this
# https://api.slack.com/methods/users.list
res = self.client.users_list()
data = res.data
metadata = data.get("response_metadata")
if metadata:
cursor = metadata.get("next_cursor")
members = [*data["members"]]
while cursor:
print(f"Fetching next page for cursor {cursor}")
res = self.client.users_list(cursor=cursor)
metadata = res.data.get("response_metadata")
if metadata:
cursor = metadata.get("next_cursor")
members = [*members, *res.data["members"]]
# pull out the timezones
timezones = [mem.get('tz') for mem in members]
# make running totals of timezones
for tz in timezones:
cntr[tz] += 1
return cntr
0.2s
OK, let's use this object to make our queries
tzc = TimeZoneCounter()
res = tzc.summary()
len(res)
8.7s
And now present it in a more readable fashion
We should be able to make a cool chart, but for now, we just have this list.
data = list(res.items())
df = pandas.DataFrame.from_records(data, columns=['timezone', 'count'])
df.sort_values('count', ascending=False)
0.4s
altair.Chart(data=df, mark="bar").encode(x='timezone', y='count')
0.4s
df.to_csv("results/cat.timezones.csv")
0.0s
0.0s