Add Group class

And pull more things out of the Channel class and into the Thread class.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-06-09 09:56:55 -04:00
parent fcd2789c29
commit 6859f8c3ec
4 changed files with 60 additions and 15 deletions

View File

@ -11,6 +11,7 @@ os.chdir(os.path.dirname(path))
from . import api
from . import auth
from . import channels
from . import groups
from . import users

View File

@ -12,17 +12,9 @@ class Channel(threads.Thread):
def __init__(self, json):
threads.Thread.__init__(self, json)
self.__member = json.get("is_member", False)
self.__unread = json.get("unread_count", None)
self.__last_ts = json.get("last_read", None)
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)
def id(self):
return self.__id
def do_fetch_info(self):
return info(self.id())
def is_member(self):
return self.__member
@ -30,11 +22,6 @@ class Channel(threads.Thread):
def post(self, text):
chat.postMessage(self.__id, text)
def unread_count(self):
if self.__unread == None:
self.fetch_info()
return self.__unread
def read(self):
if self.__last_ts == None:
self.fetch_info()

32
slack/groups.py Normal file
View File

@ -0,0 +1,32 @@
#
# Copyright 2015 (c) Anna Schumaker.
#
from . import api
from . import auth
from . import threads
class Group(threads.Thread):
def __init__(self, json):
threads.Thread.__init__(self, json)
def do_fetch_info(self):
return info(self.id())
def info(group):
ret = api.call("groups.info", token = auth.token(), channel = group)
if ret["ok"] == False:
return None
return ret["group"]
def list():
ret = api.call("groups.list", token = auth.token())
if ret["ok"] == False:
return None
gr_list = []
for group in ret["groups"]:
gr_list += [ Group(group) ]
return gr_list

View File

@ -3,12 +3,20 @@
#
#
# This class represents a slack "thread", which could be a channel, group,
# or chat. Note that child classes need to implement the following methods:
#
# - do_fetch_info(): Call <whatever>.info to find unread count and timestamp.
#
class Thread:
def __init__(self, json):
self.__id = json.get("id", 0)
self.__name = json.get("name", "").title()
self.__topic = json["topic"].get("value", "").encode("utf-8").decode("latin-1")
self.__purpose = json["purpose"].get("value", "").encode("utf-8").decode("latin-1")
self.__unread = json.get("unread_count", None)
self.__last_ts = json.get("last_read", None)
def __str__(self):
topic=""
@ -16,5 +24,22 @@ class Thread:
topic = "\nTopic: %s" % self.__topic
return "%s: %s%s" % (self.__name, self.__purpose, topic)
def fetch_info(self):
json = self.do_fetch_info()
if json != None:
self.__unread = json.get("unread_count", None)
self.__last_ts = json.get("last_read", None)
def id(self):
return self.__id
def is_member(self):
return True
def name(self):
return self.__name
def unread_count(self):
if self.__unread == None:
self.fetch_info()
return self.__unread