2015-05-26 15:20:12 -04:00
|
|
|
#
|
|
|
|
# Copyright 2015 (c) Anna Schumaker
|
|
|
|
#
|
|
|
|
from . import api
|
|
|
|
from . import auth
|
2015-05-26 16:22:05 -04:00
|
|
|
from . import users
|
|
|
|
from datetime import datetime
|
2015-05-26 15:20:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Channel:
|
|
|
|
def __init__(self, json):
|
|
|
|
self.__id = json.get("id", "0")
|
2015-05-26 16:48:22 -04:00
|
|
|
self.__name = json.get("name", "").title()
|
2015-05-26 15:20:12 -04:00
|
|
|
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")
|
2015-05-26 15:38:47 -04:00
|
|
|
self.__unread = json.get("unread_count", None)
|
|
|
|
self.__last_ts = json.get("last_read", None)
|
2015-05-26 15:20:12 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-05-26 16:22:05 -04:00
|
|
|
topic=""
|
|
|
|
if self.__topic != "":
|
|
|
|
topic = "\nTopic: %s" % self.__topic
|
|
|
|
return "%s: %s%s" % (self.__name, self.__purpose, topic)
|
2015-05-26 15:20:12 -04:00
|
|
|
|
2015-05-26 15:38:47 -04:00
|
|
|
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)
|
|
|
|
|
2015-05-26 15:20:12 -04:00
|
|
|
def is_member(self):
|
|
|
|
return self.__member
|
|
|
|
|
2015-05-26 16:48:22 -04:00
|
|
|
def name(self):
|
|
|
|
return self.__name
|
|
|
|
|
2015-05-26 15:38:47 -04:00
|
|
|
def unread_count(self):
|
|
|
|
if self.__unread == None:
|
|
|
|
self.fetch_info()
|
|
|
|
return self.__unread
|
|
|
|
|
2015-05-26 16:00:11 -04:00
|
|
|
def read(self):
|
|
|
|
if self.__last_ts == None:
|
|
|
|
self.fetch_info()
|
|
|
|
return history(self.__id, self.__last_ts);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Message:
|
|
|
|
def __init__(self, json):
|
2015-05-26 17:00:20 -04:00
|
|
|
self.__ts = json["ts"]
|
2015-05-26 17:00:20 -04:00
|
|
|
self.__time = datetime.fromtimestamp(float(self.__ts))
|
2015-05-26 16:22:05 -04:00
|
|
|
self.__user = users.info(json["user"])
|
2015-05-26 16:00:11 -04:00
|
|
|
self.__text = json["text"].encode("utf-8").decode("latin-1")
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.__time < other.__time
|
|
|
|
|
|
|
|
def __str__(self):
|
2015-05-26 16:22:05 -04:00
|
|
|
return "%s %s: %s" % (self.__time.time(), self.__user.name(), self.__text)
|
2015-05-26 16:00:11 -04:00
|
|
|
|
2015-05-26 17:00:20 -04:00
|
|
|
def ts(self):
|
2015-05-26 17:00:20 -04:00
|
|
|
return self.__ts
|
2015-05-26 17:00:20 -04:00
|
|
|
|
2015-05-26 16:00:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
def history(channel, timestamp):
|
|
|
|
ret = api.call("channels.history", token = auth.token(), channel = channel, oldest = timestamp)
|
|
|
|
if ret["ok"] == False:
|
|
|
|
return None
|
2015-05-26 15:38:47 -04:00
|
|
|
|
2015-05-26 16:00:11 -04:00
|
|
|
m_list = []
|
|
|
|
for message in ret["messages"]:
|
|
|
|
m_list += [ Message(message) ]
|
|
|
|
m_list.sort()
|
2015-05-26 17:00:20 -04:00
|
|
|
|
|
|
|
if len(m_list) > 0:
|
|
|
|
mark(channel, m_list[-1].ts())
|
2015-05-26 16:00:11 -04:00
|
|
|
return m_list
|
2015-05-26 15:38:47 -04:00
|
|
|
|
|
|
|
def info(channel):
|
|
|
|
ret = api.call("channels.info", token = auth.token(), channel = channel)
|
|
|
|
if ret["ok"] == False:
|
|
|
|
return None
|
|
|
|
return ret["channel"]
|
|
|
|
|
2015-05-26 15:20:12 -04:00
|
|
|
def list():
|
|
|
|
ret = api.call("channels.list", token = auth.token())
|
|
|
|
if ret["ok"] == False:
|
|
|
|
return None
|
|
|
|
|
|
|
|
ch_list = []
|
|
|
|
for channel in ret["channels"]:
|
|
|
|
ch_list += [ Channel(channel) ]
|
|
|
|
return ch_list
|
2015-05-26 17:00:20 -04:00
|
|
|
|
|
|
|
def mark(channel, timestamp):
|
|
|
|
api.call("channels.mark", token = auth.token(), channel = channel, ts = timestamp)
|