Skip to content

Instantly share code, notes, and snippets.

@wsorenson
Created October 18, 2012 15:42
Show Gist options
  • Save wsorenson/3912655 to your computer and use it in GitHub Desktop.
Save wsorenson/3912655 to your computer and use it in GitHub Desktop.
A quick and dirty script to calculate HipChat stats.
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import requests
import re
# log in on the browser and supply the full cookie sent to HipChat as well as the company domain.
COOKIE = ''
DOMAIN = ''
def main():
url = 'https://%s.hipchat.com/members' % DOMAIN
template = url + "/%s"
regexp = re.compile(r'members/([^"]+)"')
sent_regexp = re.compile(r'Messages sent:([^<]+)<')
signed_up_regexp = re.compile(r'Signed up:([^<]+)<')
headers = {'Cookie': COOKIE }
print "fetching member list..."
body = requests.get(url, headers=headers)
all_stats = []
for member in regexp.findall(body.content):
member_url = template % member
print "fetching", member_url
body2 = requests.get(member_url, headers=headers)
stats = sent_regexp.findall(body2.content)[0]
signed_up = signed_up_regexp.findall(body2.content)[0].strip().lower()
stats = stats.strip().replace(',', '')
member = member.split('/')[1]
name_split = member.split('-')
name = " ".join([n.strip().capitalize() for n in name_split])
signed_up = signed_up.strip().split(' ')
if signed_up[0] == 'today' or signed_up[0] == 'yesterday':
days = 1
else:
number = int(signed_up[0])
date_type = signed_up[1]
if date_type == 'year' or date_type == 'years':
days = number * 365
elif date_type == 'months' or date_type == 'month':
days = number * 30
elif date_type == 'weeks' or date_type == 'week':
days = number * 7
elif date_type == 'days' or date_type == 'day':
days = number
else:
days = 0
print "unknown date_type: %s" % date_type
stats = int(stats)
rate = 0
if days > 0:
rate = (float(stats) / days)
stats_triple = (stats, name, rate)
all_stats.append(stats_triple)
counter = 1
for score, name, rate in sorted(all_stats, reverse=True):
print "%s. %s - %s (%.02f/day)" % (counter, name, score, rate)
counter += 1
if __name__ == '__main__':
main()
@eibrahim
Copy link

eibrahim commented Sep 3, 2015

How do you get the cookie? How do you run the script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment