slack: Add channels module and Channel class

This patch begins a new main loop using the new Channel class.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-05-26 15:20:12 -04:00
parent 02b610e671
commit 23ac359ad7
4 changed files with 45 additions and 26 deletions

View File

@ -4,6 +4,8 @@
from . import api from . import api
from . import auth from . import auth
from . import channels
import sys import sys

View File

@ -14,3 +14,6 @@ def test():
print("Authentication error: %s." % ret["error"]) print("Authentication error: %s." % ret["error"])
return None return None
return ret return ret
def token():
return __TOKEN

34
slack/channels.py Normal file
View File

@ -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 "<Channel id:%s name:%s is_member:%s>" % (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

View File

@ -1,40 +1,20 @@
#!/usr/bin/python #!/usr/bin/python
# Copyright 2015 (c) Anna Schumaker. # Copyright 2015 (c) Anna Schumaker.
import slack
import json
import os import os
import slack import slack
import sys import sys
from datetime import datetime 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)
# #