Skip to content

Instantly share code, notes, and snippets.

@Gelob
Last active January 3, 2018 15:51
Show Gist options
  • Save Gelob/cd0230b3489374aa6cf84f8850e1aa88 to your computer and use it in GitHub Desktop.
Save Gelob/cd0230b3489374aa6cf84f8850e1aa88 to your computer and use it in GitHub Desktop.
Run with python hipchat2csv.py authtoken email write or post. Example: 'python hipchat2csv.py my.name@example.com print'. This will get the last 1000 messages from my conversations with my.name@example.com and print the csv output. If i use write instead of print it will write to a file with the email as the filename.
#You can get your own API token by going to https://yourcompany.hipchat.com/account/api and generating one.
#If you get an error it might be because something doesn't exist or you typo'd, there isn't much error checking
import json
import sys
import os
import os.path
import requests
def get_history(email, token):
hipchat_url = "https://api.hipchat.com/v2/user/{0}/history/latest?auth_token={1}&max-results=1000&reverse=true".format(email,token)
headers = {'Content-Type':'application/json'}
response = requests.get(hipchat_url)
return response.text
def parse_json_file(file,email):
res = []
data = json.loads(file, strict=False)
for d in data["items"]:
if d == []:
continue
name, date, message = d["from"]["name"], d["date"], d["message"]
message = message.replace('"', '\'')
s = "{0},{1},\"{2}\"".format(name, date, message.encode('utf-8').strip())
res.append(s)
if sys.argv[3] == "write":
f = email + ".csv"
with open(f, "w") as f:
f.write("\n".join(res))
else:
print "\n".join(res)
parse_json_file(get_history(sys.argv[2],sys.argv[1]), sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment