From 23ac359ad75ab7b773dfa64db16a09b129fa6461 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 26 May 2015 15:20:12 -0400 Subject: [PATCH] slack: Add channels module and Channel class This patch begins a new main loop using the new Channel class. Signed-off-by: Anna Schumaker --- slack/__init__.py | 2 ++ slack/auth.py | 3 +++ slack/channels.py | 34 ++++++++++++++++++++++++++++++++++ slackmail.py | 32 ++++++-------------------------- 4 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 slack/channels.py diff --git a/slack/__init__.py b/slack/__init__.py index 7bbb0a0..1c272d1 100644 --- a/slack/__init__.py +++ b/slack/__init__.py @@ -4,6 +4,8 @@ from . import api from . import auth +from . import channels + import sys diff --git a/slack/auth.py b/slack/auth.py index 611c13d..28d5e9c 100644 --- a/slack/auth.py +++ b/slack/auth.py @@ -14,3 +14,6 @@ def test(): print("Authentication error: %s." % ret["error"]) return None return ret + +def token(): + return __TOKEN diff --git a/slack/channels.py b/slack/channels.py new file mode 100644 index 0000000..d358e76 --- /dev/null +++ b/slack/channels.py @@ -0,0 +1,34 @@ +# +# Copyright 2015 (c) Anna Schumaker +# +from . import api +from . import auth + + +class Channel: + def __init__(self, json): + self.__id = json.get("id", "0") + self.__name = json.get("name", "") + 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") + + def __str__(self): + return "" % (self.__id, self.__name, self.__member) + + def __repr__(self): + return self.__str__() + + def is_member(self): + return self.__member + + +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 diff --git a/slackmail.py b/slackmail.py index 0584fbf..5082a46 100644 --- a/slackmail.py +++ b/slackmail.py @@ -1,40 +1,20 @@ #!/usr/bin/python # Copyright 2015 (c) Anna Schumaker. -import slack -import json import os import slack import sys from datetime import datetime -from urllib import request - - -# TODO: Move the token file outside of code directory -TOKEN = "" -with open("token") as f: - TOKEN = f.read().strip() - - -def call_method(method, args = dict()): - arglist = [] - for (key, value) in args.items(): - arglist += ["%s=%s" % (key, value)] - argstr = "&&".join(arglist) - - server = "https://slack.com/api/" - with request.urlopen("%s/%s?%s" % (server, method, argstr)) as f: - return json.loads(f.read().decode()) - return {"ok" : False} - - -def call_method_auth(method, args = dict()): - args["token"] = TOKEN - return call_method(method, args) +for channel in slack.channels.list(): + if not channel.is_member(): + continue + print(channel) + +sys.exit(0) #