diff options
Diffstat (limited to 'main.py')
-rw-r--r-- | main.py | 57 |
1 files changed, 45 insertions, 12 deletions
@@ -1,8 +1,20 @@ #!/usr/bin/env python3 ''' -AFTER TESTING BIND MONGODB TO LOCALHOST/ARGO HOST -CLEAR CONFIG/SETTINGS.CFG BEFORE COMMITTING +Copyright Daniel Jones 2017 + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. ''' import urllib.request; @@ -10,6 +22,7 @@ import time; import json; from pymongo import MongoClient; import configparser; +import pickle; ''' slack logging run via a cronjob and stored in MongoDB @@ -64,7 +77,7 @@ def getstaticapidata(): usersurl = usersurl.format(token); global usersjson; usersjson = getjson(usersurl); - + def getchannelid(channel): ''' returns channel id gathered from the name provided via the slack api @@ -76,7 +89,7 @@ def getchannelid(channel): totalchannels = len(j['channels']); for x in range(totalchannels): if (j['channels'][x]['name'] == channel): - channelid = j['channels'][x]['id']; + channelid = j['channels'][x]['id']; try: channelid; except NameError: @@ -106,7 +119,7 @@ def getuserid(username): totalusers = len(j['members']); for x in range(totalusers): if (j['members'][x]['name'] == username): - userid = j['members'][x]['id']; + userid = j['members'][x]['id']; try: userid; except NameError: @@ -124,7 +137,7 @@ def getusername(userid): totalusers = len(j['members']); for x in range(totalusers): if (j['members'][x]['id'] == userid): - username = j['members'][x]['name']; + username = j['members'][x]['name']; try: username; except NameError: @@ -170,7 +183,7 @@ def collectbants(): data = getchannelmsghistory(token, getchannelid(j['channels'][x]['name']), cfg.get("messagelog", "count")); totalmessages = len(data['messages']); for i in range(totalmessages): - # print("<" + getusername(data['messages'][x]['user']) + "> " + str(data['messages'][x]['text'].encode('utf8'))); + # print("<" + getusername(data['messages'][x]['user']) + "> " + str(data['messages'][x]['text'].encode('utf8'))); try: author = getusername(data['messages'][i]['user']); except KeyError: @@ -179,9 +192,9 @@ def collectbants(): if (checkifentryexists(j['channels'][x]['name'], data['messages'][i]['ts']) != True): print("message not logged, logging"); query = {"author": author, - "channel": j['channels'][x]['name'], - "message": str(data['messages'][i]['text']), - "timestamp": data['messages'][i]['ts']}; + "channel": j['channels'][x]['name'], + "message": str(data['messages'][i]['text']), + "timestamp": data['messages'][i]['ts']}; message_id = messagedb.insert_one(query).inserted_id; else: print("message exists:", data['messages'][i]['ts']); @@ -193,18 +206,38 @@ def collectbants(): else: print("message matches, nothing to do."); +def updateuserdict(userdict): + ''' + updates users in the user dictionary + returns nothing + args: + userdict = dictionary to add to/save + ''' + usersurl = "https://slack.com/api/users.list?token={}"; + usersurl = usersurl.format(token); + usersj = getjson(usersurl); + if __name__ == "__main__": start_time = time.time(); + # cfg cfg = configparser.ConfigParser(); cfg.read("config/settings.cfg"); token = cfg.get("slack", "token"); + # users + try: + with open("config/users.dict", "rb") as f: + userdict = pickle.load(f); + except FileNotFoundError: + print("user dictionary not found"); + userdict = {}; + updateuserdict(userdict); # static api data = channel info and user info getstaticapidata(); dbconnect(); - collectbants(); + #collectbants(); print("===========finished in %s seconds===========" % (time.time() - start_time)); ''' Total API calls should be 10 (8 channels) + (channels list, members list) ''' - + |