From 11a777cb76f7655aa6cd338a4d8b8bf252f2fe4b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 26 May 2015 12:26:31 -0400 Subject: [PATCH] Add timestamps to message output Signed-off-by: Anna Schumaker --- slackmail.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/slackmail.py b/slackmail.py index 05d03ad..ca9d7fb 100644 --- a/slackmail.py +++ b/slackmail.py @@ -3,6 +3,7 @@ import json import sys +from datetime import datetime from urllib import request @@ -46,36 +47,40 @@ if call_method_auth("auth.test")["ok"] == False: def list_channel_ids(): call = call_method_auth("channels.list") - if call["ok"] == False: - sys.exit(1) - for channel in call["channels"]: - yield channel["id"] + 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"] == False: - sys.exit(1) - return call["channel"] + if call["ok"] == True: + return call["channel"] def read_channel(id, ts): args = { "channel" : id, "oldest" : ts } call = call_method_auth("channels.history", args) - if call["ok"] == False: - sys.exit(1) - messages = call["messages"] - messages.reverse() - for message in messages: - yield message + if call["ok"] == True: + messages = call["messages"] + messages.reverse() + for message in messages: + yield message + +def _print_message(user, message): + dt = datetime.fromtimestamp(float(message["ts"])) + text = message["text"].encode("utf-8").decode("latin-1") + print("%s %s: %s" % (str(dt), user, text)) def print_message(message): call = call_method_auth("users.info", {"user" : message["user"]}) if call["ok"] == False: - sys.exit(1) - - name = "%s %s" % (call["user"]["profile"].get("first_name", ""), call["user"]["profile"].get("last_name", "")) - text = message["text"].encode("utf-8").decode("latin-1") - print("%s: %s" % (name, message["text"].encode("utf-8").decode("latin-1"))) - + name = message["user"] + else: + first = call["user"]["profile"].get("first_name", "") + last = call["user"]["profile"].get("last_name", "") + name = "%s %s" % (first, last) + if first == "" and last == "": + name = call["user"]["name"] + _print_message(name, message)