python 从hipchat房间获取详细聊天记录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 从hipchat房间获取详细聊天记录相关的知识,希望对你有一定的参考价值。


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()

以上是关于python 从hipchat房间获取详细聊天记录的主要内容,如果未能解决你的问题,请参考以下文章

HipChat Nodejs Bot - 无法接收房间消息

markdown 自动按字母顺序排列HipChat房间和人

sh HipChat API v2 - 使用cURL向房间发送消息

如何使用 xmpppy 向聊天室发送消息?

无法从服务器 XMPP 框架中获取 XMPPRoom 的详细信息

按需获取房间聊天记录