smanjil
3/28/2017 - 4:25 PM

get details chat history from hipchat room

get details chat history from hipchat room


import requests
import json
import csv

# open csv file
csv_file = open('hip_msg.csv', 'w')

writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)

# write header for csv
writer.writerow(['Date', 'Author', 'Message', 'Link'])

# get latest chat history of room with id '688910'
msg = requests.get("https://api.hipchat.com/v2/room/688910/history?auth_token=rboQfWxMA0QACNiWUtGZewcajrGWUpWK91sIa41y")

# separate message, date, and author of that message from whole json response
recs = [[item['date'], item['from']['name'], item['message']] \
            for item in json.loads(msg.text)['items'] \
                if isinstance(item['from'], dict)]

# separate a list of tuples containing (index, link) from all recs
final_list = [(i, msgs) for i, lists in enumerate(recs) \
                 for msgs in lists[2].split() \
                     if msgs.split(':')[0] in ['http', 'https']]

# append that link to the original list from recs
for items in final_list:
    recs[items[0]].append(items[1])

# write each row from recs in csv
[writer.writerow(rows) for rows in recs]

# close csv file
csv_file.close()