From 4eb177cd8f957c9ddeca7e1ff87a73f71798d3ec Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 26 May 2015 15:38:47 -0400 Subject: [PATCH] slack: Find extra channel information if needed This is mostly to find unread count and timestamp Signed-off-by: Anna Schumaker --- slack/channels.py | 21 +++++++++++++++++++++ slackmail.py | 19 +++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/slack/channels.py b/slack/channels.py index d358e76..8e3372f 100644 --- a/slack/channels.py +++ b/slack/channels.py @@ -12,6 +12,8 @@ class Channel: self.__member = json.get("is_member", False) self.__topic = json["topic"].get("value", "").encode("utf-8").decode("latin-1") self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode("latin-1") + self.__unread = json.get("unread_count", None) + self.__last_ts = json.get("last_read", None) def __str__(self): return "" % (self.__id, self.__name, self.__member) @@ -19,9 +21,28 @@ class Channel: def __repr__(self): return self.__str__() + def fetch_info(self): + json = info(self.__id) + if json != None: + self.__unread = json.get("unread_count", None) + self.__last_ts = json.get("last_read", None) + def is_member(self): return self.__member + def unread_count(self): + if self.__unread == None: + self.fetch_info() + return self.__unread + + + +def info(channel): + ret = api.call("channels.info", token = auth.token(), channel = channel) + if ret["ok"] == False: + return None + return ret["channel"] + def list(): ret = api.call("channels.list", token = auth.token()) diff --git a/slackmail.py b/slackmail.py index 5082a46..569f826 100644 --- a/slackmail.py +++ b/slackmail.py @@ -12,26 +12,13 @@ from datetime import datetime for channel in slack.channels.list(): if not channel.is_member(): continue - print(channel) + if channel.unread_count() == 0 or channel.unread_count() == None: + continue + print(channel, channel.unread_count()) sys.exit(0) -# -# Find list of channels, we'll get info later -# - -def list_channel_ids(): - call = call_method_auth("channels.list") - if call["ok"] == True: - for channel in call["channels"]: - yield channel["id"] - -def find_channel_info(id): - call = call_method_auth("channels.info", { "channel" : id }) - if call["ok"] == True: - return call["channel"] - def read_channel(id, ts): args = { "channel" : id, "oldest" : ts } call = call_method_auth("channels.history", args)