Fix generate info for chat

This commit is contained in:
Mickael 2020-02-16 16:12:08 +01:00
parent 0e62246c63
commit 24810ff5af

View file

@ -1,5 +1,5 @@
from rocketchat_API.rocketchat import RocketChat from rocketchat_API.rocketchat import RocketChat
import os import os, time
import json import json
from datetime import datetime from datetime import datetime
from monthdelta import monthdelta from monthdelta import monthdelta
@ -23,8 +23,7 @@ def main():
rocket = Connection() rocket = Connection()
print("Check users") print("Check users")
users = getAllActiveUsers(rocket) crapauds_total = getNbUsers(rocket)
crapauds_total = len(users)
print("Check channels") print("Check channels")
channels = getAllChannels(rocket) channels = getAllChannels(rocket)
@ -43,15 +42,17 @@ def main():
cohortes.append(channel["name"][8:]) cohortes.append(channel["name"][8:])
if channel_date > recent_date: if channel_date > recent_date:
channels_recent += 1 channels_recent += 1
messages = getAllMessages(rocket, channel["_id"], begindate= recent_date) messages = getAllMessages(rocket, channel["_id"], begindate= recent_date)
if messages is not None:
nbMessages = len(messages) nbMessages = len(messages)
print("Check messages for channels {} ({})".format(channel['name'], nbMessages)) print("Check messages for channels {} ({})".format(channel['name'], nbMessages))
messages_recent += nbMessages messages_recent += nbMessages
if (channel["name"] != "general") and (channel["name"] != "accueil"): if (channel["name"] != "general") and (channel["name"] != "accueil"):
channels_list.append((channel["name"], nbMessages)) channels_list.append((channel["name"], nbMessages))
users.extend(map(lambda message: message["u"]["_id"], messages)) users.extend(map(lambda message: message["u"]["_id"], messages))
else:
print("ERROR : couldn't get message")
time.sleep(20)
# Get the channels with the most number of message # Get the channels with the most number of message
channels_list.sort(key=lambda channel: channel[1], reverse= True) channels_list.sort(key=lambda channel: channel[1], reverse= True)
@ -80,17 +81,9 @@ def main():
save(info, "chat") save(info, "chat")
print("End chat info generation") print("End chat info generation")
def getAllActiveUsers(rocket): def getNbUsers(rocket):
index = 0 users = rocket.users_list().json()
allUsers = [] return users["total"]
while True:
users = rocket.users_list(offset = index).json()
allUsers.extend(users["users"])
if users['count'] + users['offset'] >= users['total']:
break
index += users['count']
return list(filter( lambda user: user["active"] ,allUsers))
if __name__ == "__main__": if __name__ == "__main__":
main() main()