Write unread messages to a temporary file

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-05-26 12:41:34 -04:00
parent 11a777cb76
commit fdc2c9970e
1 changed files with 16 additions and 11 deletions

View File

@ -2,6 +2,7 @@
# Copyright 2015 (c) Anna Schumaker. # Copyright 2015 (c) Anna Schumaker.
import json import json
import os
import sys import sys
from datetime import datetime from datetime import datetime
from urllib import request from urllib import request
@ -65,12 +66,12 @@ def read_channel(id, ts):
for message in messages: for message in messages:
yield message yield message
def _print_message(user, message): def _write_message(fout, user, message):
dt = datetime.fromtimestamp(float(message["ts"])) dt = datetime.fromtimestamp(float(message["ts"]))
text = message["text"].encode("utf-8").decode("latin-1") 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"]}) call = call_method_auth("users.info", {"user" : message["user"]})
if call["ok"] == False: if call["ok"] == False:
name = message["user"] name = message["user"]
@ -80,16 +81,20 @@ def print_message(message):
name = "%s %s" % (first, last) name = "%s %s" % (first, last)
if first == "" and last == "": if first == "" and last == "":
name = call["user"]["name"] name = call["user"]["name"]
_print_message(name, message) _write_message(fout, name, message)
os.mkdir("/tmp/slackmail/")
for id in list_channel_ids(): for id in list_channel_ids():
channel = find_channel_info(id) channel = find_channel_info(id)
print(channel["name"]) if not channel["is_member"]:
print("\t%s" % channel["purpose"]["value"].encode("utf-8").decode("latin-1")) continue
if channel["is_member"]: if channel["unread_count"] == 0:
print("\tUnread count: %s, last read: %s" % (channel["unread_count"], channel["last_read"])) continue
for message in read_channel(id, channel["last_read"]): fout = open("/tmp/slackmail/%s" % channel["name"], 'w')
print_message(message) fout.write(channel["purpose"]["value"].encode("utf-8").decode("latin-1"))
print() fout.write("\n")
for message in read_channel(id, channel["last_read"]):
write_message(fout, message)
fout.close()