Skip to content

Instantly share code, notes, and snippets.

@dgeske
Forked from wsorenson/hipchat.py
Last active Aug 29, 2015
Embed
What would you like to do?
Display stats for all users on your HipChat account. Requires requests (http://python-requests.org)
#!/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.
# domain is shown when you are signed into hipchat
# 1. sign in: https://www.hipchat.com/sign_in
# 2. copy domain name https://YOUR_DOMAIN_HERE.hipchat.com/
DOMAIN = 'YOUR_DOMAIN_HERE'
# once you are signed in, copy the value of the cookie named "s"
SESSION='VALUE_OF_S_COOKIE'
def main():
url = 'https://%s.hipchat.com/people' % DOMAIN
template = url + "/%s"
regexp = re.compile(r'/people/([^"]+)"')
sent_regexp = re.compile(r'Messages sent:([^<]+)<')
signed_up_regexp = re.compile(r'Signed up:([^<]+)<')
headers = {'Cookie': 'rm=1;s=' + SESSION + ';sd=' + DOMAIN }
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('/')[2]
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment