portail/scripts/generate_chat_info.py

112 lines
3.4 KiB
Python
Raw Normal View History

from rocketchat_API.rocketchat import RocketChat
import os
import json
from datetime import datetime
from monthdelta import monthdelta
2019-09-01 10:50:21 +00:00
from common.rocketchathelper import getAllChannels, Connection
2019-09-01 11:12:30 +00:00
from common.savehelper import save
def main():
print("Start chat info generation")
crapauds_total = 0
crapauds_recent = 0
channels_total = 0
channels_recent = 0
channels_list = []
messages_total = 0
messages_recent = 0
2019-08-20 15:38:55 +00:00
cohortes = []
recent_date = datetime.now() - monthdelta()
2019-09-01 10:50:21 +00:00
rocket = Connection()
2019-08-18 20:44:19 +00:00
2019-08-17 16:31:31 +00:00
print("Check users")
2019-08-26 17:16:26 +00:00
users = getAllActiveUsers(rocket)
crapauds_total = len(users)
2019-08-18 19:49:58 +00:00
2019-08-17 16:31:31 +00:00
print("Check channels")
channels = getAllChannels(rocket)
channels_total = len(channels)
2019-08-18 21:25:18 +00:00
users = []
for channel in channels:
2019-08-28 16:14:03 +00:00
if "lm" not in channel:
continue
messages_total += int(channel["msgs"])
2019-08-28 16:14:03 +00:00
2019-08-17 16:17:51 +00:00
date = channel["lm"]
channel_date = datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.%fZ")
2019-08-20 15:38:55 +00:00
if channel["name"].startswith("cohorte-"):
cohortes.append(channel["name"][8:])
if channel_date > recent_date:
channels_recent += 1
2019-08-18 21:25:18 +00:00
2019-08-17 16:31:31 +00:00
print("Check messages for channels {}".format(channel['name']))
messages = rocket.channels_history(channel["_id"], oldest= recent_date, count= 10000).json()
2019-08-26 17:08:22 +00:00
if messages["success"]:
messages = list(filter(lambda message: "t" not in message, messages["messages"]))
nbMessages = len(messages)
2019-08-18 21:25:18 +00:00
messages_recent += nbMessages
2019-08-26 16:50:02 +00:00
if (channel["name"] != "general") and (channel["name"] != "accueil"):
channels_list.append((channel["name"], nbMessages))
2019-08-26 17:08:22 +00:00
users.extend(map(lambda message: message["u"]["_id"], messages))
2019-08-18 19:49:58 +00:00
else:
print("Error : {}".format(messages["error"]))
2019-08-18 21:25:18 +00:00
# Get the channels with the most number of message
channels_list.sort(key=lambda channel: channel[1], reverse= True)
channels_recentlist = list(map(lambda channel: channel[0], channels_list))
crapauds_recent = len(set(users))
info = {
"crapauds": {
"total": crapauds_total,
"recent": crapauds_recent
},
"canaux": {
"total": channels_total,
"recent": channels_recent,
2019-08-18 21:25:18 +00:00
# Get only the 10 first channels
"liste": channels_recentlist[0:10]
},
"messages": {
"total": messages_total,
"recent": messages_recent
2019-08-20 15:38:55 +00:00
},
"cohortes": cohortes
}
2019-09-01 11:12:30 +00:00
save(info, "chat")
print("End chat info generation")
2019-08-26 17:16:26 +00:00
def getAllActiveUsers(rocket):
index = 0
allUsers = []
while True:
users = rocket.users_list(offset = index).json()
allUsers.extend(users["users"])
if users['count'] + users['offset'] >= users['total']:
break
index += users['count']
2019-08-26 17:16:26 +00:00
return list(filter( lambda user: user["active"] ,allUsers))
def getAllMessages(rocket, roomid, begindate):
index = 0
allMessages = []
while True:
messages = rocket.channels_history(roomid, offset = index, oldest= begindate).json()
allMessages.extend(messages["m"])
if messages['count'] + messages['offset'] >= messages['total']:
break
index += messages['count']
return allMessages
if __name__ == "__main__":
main()