From fdc2c9970ef09d92670720af3473f72c124a50a3 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 26 May 2015 12:41:34 -0400 Subject: [PATCH] Write unread messages to a temporary file Signed-off-by: Anna Schumaker --- slackmail.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/slackmail.py b/slackmail.py index ca9d7fb..1bc6909 100644 --- a/slackmail.py +++ b/slackmail.py @@ -2,6 +2,7 @@ # Copyright 2015 (c) Anna Schumaker. import json +import os import sys from datetime import datetime from urllib import request @@ -65,12 +66,12 @@ def read_channel(id, ts): for message in messages: yield message -def _print_message(user, message): +def _write_message(fout, 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)) + fout.write("%s %s: %s\n" % (str(dt), user, text)) -def print_message(message): +def write_message(fout, message): call = call_method_auth("users.info", {"user" : message["user"]}) if call["ok"] == False: name = message["user"] @@ -80,16 +81,20 @@ def print_message(message): name = "%s %s" % (first, last) if first == "" and last == "": name = call["user"]["name"] - _print_message(name, message) + _write_message(fout, name, message) +os.mkdir("/tmp/slackmail/") for id in list_channel_ids(): channel = find_channel_info(id) - print(channel["name"]) - print("\t%s" % channel["purpose"]["value"].encode("utf-8").decode("latin-1")) - if channel["is_member"]: - print("\tUnread count: %s, last read: %s" % (channel["unread_count"], channel["last_read"])) - for message in read_channel(id, channel["last_read"]): - print_message(message) - print() + if not channel["is_member"]: + continue + if channel["unread_count"] == 0: + continue + fout = open("/tmp/slackmail/%s" % channel["name"], 'w') + fout.write(channel["purpose"]["value"].encode("utf-8").decode("latin-1")) + fout.write("\n") + for message in read_channel(id, channel["last_read"]): + write_message(fout, message) + fout.close()