slack: Add the channels.history() function and a Message class

For finding unread messages.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-05-26 16:00:11 -04:00
parent 4eb177cd8f
commit 48baf65574
2 changed files with 69 additions and 49 deletions

View File

@ -35,8 +35,41 @@ class Channel:
self.fetch_info() self.fetch_info()
return self.__unread return self.__unread
def read(self):
if self.__last_ts == None:
self.fetch_info()
return history(self.__id, self.__last_ts);
class Message:
def __init__(self, json):
self.__time = json["ts"]
self.__user = json["user"]
self.__text = json["text"].encode("utf-8").decode("latin-1")
def __lt__(self, other):
return self.__time < other.__time
def __str__(self):
return "<Message time:%s user:%s text:%s>" % (self.__time, self.__user, self.__text)
def __repr__(self):
return self.__str__()
def history(channel, timestamp):
ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp)
if ret["ok"] == False:
return None
m_list = []
for message in ret["messages"]:
m_list += [ Message(message) ]
m_list.sort()
return m_list
def info(channel): def info(channel):
ret = api.call("channels.info", token = auth.token(), channel = channel) ret = api.call("channels.info", token = auth.token(), channel = channel)
if ret["ok"] == False: if ret["ok"] == False:

View File

@ -3,7 +3,6 @@
import os import os
import slack import slack
import sys
from datetime import datetime from datetime import datetime
@ -14,52 +13,40 @@ for channel in slack.channels.list():
continue continue
if channel.unread_count() == 0 or channel.unread_count() == None: if channel.unread_count() == 0 or channel.unread_count() == None:
continue continue
print(channel, channel.unread_count()) print(channel.read())
sys.exit(0) #def _write_message(fout, user, message):
# dt = datetime.fromtimestamp(float(message["ts"]))
# text = message["text"].encode("utf-8").decode("latin-1")
def read_channel(id, ts): # fout.write("%s %s: %s\n" % (str(dt), user, text))
args = { "channel" : id, "oldest" : ts } #
call = call_method_auth("channels.history", args) #def write_message(fout, message):
if call["ok"] == True: # call = call_method_auth("users.info", {"user" : message["user"]})
messages = call["messages"] # if call["ok"] == False:
messages.reverse() # name = message["user"]
for message in messages: # else:
yield message # first = call["user"]["profile"].get("first_name", "")
# last = call["user"]["profile"].get("last_name", "")
def _write_message(fout, user, message): # name = "%s %s" % (first, last)
dt = datetime.fromtimestamp(float(message["ts"])) # if first == "" and last == "":
text = message["text"].encode("utf-8").decode("latin-1") # name = call["user"]["name"]
fout.write("%s %s: %s\n" % (str(dt), user, text)) # _write_message(fout, name, message)
#
def write_message(fout, message): #
call = call_method_auth("users.info", {"user" : message["user"]}) #tmp_dir = "/tmp/%s" % team
if call["ok"] == False: #if not os.path.exists(tmp_dir):
name = message["user"] # os.mkdir(tmp_dir)
else: #
first = call["user"]["profile"].get("first_name", "") #for id in list_channel_ids():
last = call["user"]["profile"].get("last_name", "") # channel = find_channel_info(id)
name = "%s %s" % (first, last) # if not channel["is_member"]:
if first == "" and last == "": # continue
name = call["user"]["name"] # if channel["unread_count"] == 0:
_write_message(fout, name, message) # continue
#
# fout = open("%s/%s" % (tmp_dir, channel["name"]), 'w')
tmp_dir = "/tmp/%s" % team # fout.write(channel["purpose"]["value"].encode("utf-8").decode("latin-1"))
if not os.path.exists(tmp_dir): # fout.write("\n")
os.mkdir(tmp_dir) # for message in read_channel(id, channel["last_read"]):
# write_message(fout, message)
for id in list_channel_ids(): # fout.close()
channel = find_channel_info(id)
if not channel["is_member"]:
continue
if channel["unread_count"] == 0:
continue
fout = open("%s/%s" % (tmp_dir, 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()