Fixed updating of user/channel lists to not repeat forever and rate limit itself
This commit is contained in:
parent
a79581413e
commit
5058ae0085
2
main.py
2
main.py
|
|
@ -45,7 +45,7 @@ def main() -> None:
|
|||
# wrap.add_passive(periodicals.ItsTenPM())
|
||||
|
||||
# Add automatic updating of users
|
||||
wrap.add_passive(periodicals.Updatinator(wrap, 60))
|
||||
wrap.add_passive(periodicals.Updatinator(wrap, 120))
|
||||
|
||||
# Add nagloop
|
||||
wrap.add_passive(periodicals.NotifyJobs())
|
||||
|
|
|
|||
|
|
@ -121,12 +121,16 @@ class RemindJobs(slack_util.Passive, JobNotifier):
|
|||
|
||||
|
||||
class Updatinator(slack_util.Passive):
|
||||
"""
|
||||
Periodically updates the channels and users in the slack
|
||||
"""
|
||||
def __init__(self, wrapper_to_update: slack_util.ClientWrapper, interval_seconds: int):
|
||||
self.wrapper_target = wrapper_to_update
|
||||
self.interval = interval_seconds
|
||||
|
||||
async def run(self):
|
||||
# Give time to warmup
|
||||
while True:
|
||||
await asyncio.sleep(self.interval)
|
||||
self.wrapper_target.update_channels()
|
||||
self.wrapper_target.update_users()
|
||||
await asyncio.sleep(self.interval)
|
||||
|
|
|
|||
|
|
@ -169,8 +169,6 @@ class ClientWrapper(object):
|
|||
# Cache users and channels
|
||||
self.users: dict = {}
|
||||
self.channels: dict = {}
|
||||
self.update_users()
|
||||
self.update_channels()
|
||||
|
||||
# Scheduled events handling
|
||||
def add_passive(self, per: Passive) -> None:
|
||||
|
|
@ -298,7 +296,7 @@ class ClientWrapper(object):
|
|||
if cursor:
|
||||
args["cursor"] = cursor
|
||||
|
||||
channel_dicts = self.api_call("https://slack.com/api/conversations.list", **args)
|
||||
channel_dicts = self.api_call("conversations.list", **args)
|
||||
|
||||
# If the response is good, put its results to the dict
|
||||
if channel_dicts["ok"]:
|
||||
|
|
@ -307,15 +305,16 @@ class ClientWrapper(object):
|
|||
name=channel_dict["name"])
|
||||
new_dict[new_channel.id] = new_channel
|
||||
|
||||
# If no new channels, just give it up
|
||||
if len(channel_dicts["channels"]) == 0:
|
||||
break
|
||||
|
||||
# Otherwise, fetch the cursor
|
||||
# Fetch the cursor
|
||||
cursor = channel_dicts.get("response_metadata").get("next_cursor")
|
||||
|
||||
# If cursor is blank, we're done new channels, just give it up
|
||||
if cursor == "":
|
||||
break
|
||||
|
||||
|
||||
else:
|
||||
print("Warning: failed to retrieve channels")
|
||||
print("Warning: failed to retrieve channels. Message: {}".format(channel_dicts))
|
||||
break
|
||||
self.channels = new_dict
|
||||
|
||||
|
|
@ -332,7 +331,7 @@ class ClientWrapper(object):
|
|||
if cursor:
|
||||
args["cursor"] = cursor
|
||||
|
||||
user_dicts = self.api_call("https://slack.com/api/users.list", **args)
|
||||
user_dicts = self.api_call("users.list", **args)
|
||||
|
||||
# Make a new dict to use
|
||||
new_dict = {}
|
||||
|
|
@ -346,15 +345,15 @@ class ClientWrapper(object):
|
|||
email=user_dict.get("profile").get("email"))
|
||||
new_dict[new_user.id] = new_user
|
||||
|
||||
# If no new channels, just give it up
|
||||
if len(user_dicts["channels"]) == 0:
|
||||
break
|
||||
|
||||
# Otherwise, fetch the cursor
|
||||
# Fetch the cursor
|
||||
cursor = user_dicts.get("response_metadata").get("next_cursor")
|
||||
|
||||
# If cursor is blank, we're done new channels, just give it up
|
||||
if cursor == "":
|
||||
break
|
||||
|
||||
else:
|
||||
print("Warning: failed to retrieve channels")
|
||||
print("Warning: failed to retrieve users")
|
||||
break
|
||||
self.users = new_dict
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue