From fac109e4a202dd9bfc11d004d5c2be1ce672e727 Mon Sep 17 00:00:00 2001 From: yurippe Date: Mon, 4 May 2015 19:08:39 +0200 Subject: [PATCH 1/7] Update AdvanceMessage.py --- DataTypes/AdvanceMessage.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DataTypes/AdvanceMessage.py b/DataTypes/AdvanceMessage.py index ca1e242..9d27792 100644 --- a/DataTypes/AdvanceMessage.py +++ b/DataTypes/AdvanceMessage.py @@ -15,6 +15,6 @@ def __init__(self, data): try: self.historyId = data["p"]["h"] except: self.historyId = None - - try: self.length = data["p"]["m"]["duration"] - except: self.length = None + + try: self.duration = data["p"]["m"]["duration"] + except: self.duration = None From 06d307a93fba3328301bd301c9f38719063258bc Mon Sep 17 00:00:00 2001 From: yurippe Date: Sun, 31 May 2015 16:08:10 +0200 Subject: [PATCH 2/7] Fixed the REST API Now the rest API works 100% --- Bot.py | 5 ++++- plugin.py | 5 +++++ plugins/ookamimeh.py | 5 ++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Bot.py b/Bot.py index bd2916c..f62f31e 100644 --- a/Bot.py +++ b/Bot.py @@ -205,9 +205,12 @@ def generateAuthkey(this): #print "Authkey: " + authkey this.authkey = authkey - + def join(this): + this.REST("POST", "rooms/join", {"slug": this.roomslug }) + def start(this): this.generateAuthkey() + this.join() #this.urllibGenerateAuthKey() this.ws.connect() this.ws.run_forever() diff --git a/plugin.py b/plugin.py index 387914a..67ec226 100644 --- a/plugin.py +++ b/plugin.py @@ -46,6 +46,10 @@ def onRecv(self, message): def onEnable(self): pass + #Called when the bot joins a room + def onJoin(self): + pass + def sendChat(self, message): self.bot.sendChat(message) @@ -55,6 +59,7 @@ def getUser(self, uid): ############################## ## Event Handlers ## ############################## + def UNIMPLEMENTED(self, raw_data): pass diff --git a/plugins/ookamimeh.py b/plugins/ookamimeh.py index e561dbf..c8e496b 100644 --- a/plugins/ookamimeh.py +++ b/plugins/ookamimeh.py @@ -14,9 +14,8 @@ def onChat(self, data): r = self.getUser(data.uid) print r self.sendChat(r) + self.sendChat(self.meh()) def meh(self): - print self.bot.REST("POST", "votes", {"direction":-1, "historyID": self.historyID}) - - print "--------------------------------------" + return self.bot.REST("POST", "votes", {"direction":-1, "historyID": self.historyID}) From e22390ee6fbec7eced8d9f77f2df9d59c98db995 Mon Sep 17 00:00:00 2001 From: yurippe Date: Sun, 31 May 2015 16:32:51 +0200 Subject: [PATCH 3/7] Added a core "plugin" To keep track of some data (for now only "historyID" for woots and mehs) --- Bot.py | 12 ++++++++++++ plugin.py | 9 ++++++++- plugins/ookamimeh.py | 8 +------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Bot.py b/Bot.py index f62f31e..b6c3bb6 100644 --- a/Bot.py +++ b/Bot.py @@ -11,6 +11,15 @@ from ws4py.client.threadedclient import WebSocketClient import requests + +import plugin + +#Use a plugin to do some core stuff +class BotPlugin(plugin.plugin): + def onAdvance(self, data): + self.bot.data["historyID"] = data.historyId + + class WebSock(WebSocketClient): def init(this, bot): @@ -33,6 +42,8 @@ def __init__(this, username, password, roomslug): this.ws = WebSock("wss://godj.plug.dj:443/socket") this.ws.init(this) this.plugins = {} + this.data = {"historyID":""} + this.core = BotPlugin(this) def loadPlugins(this, folder): @@ -63,6 +74,7 @@ def onRecv(this, websocket, message): #print message for plugin in this.plugins.keys(): try: + this.core.onRecv(message) #Update core first this.plugins[plugin]["instance"].onRecv(message) except Exception as e: tempel1 = "|------------Error in plugin: %s-------------"%(this.plugins[plugin]["name"]) diff --git a/plugin.py b/plugin.py index 67ec226..5e75239 100644 --- a/plugin.py +++ b/plugin.py @@ -15,7 +15,7 @@ def onRecv(self, message): elif msg["a"] == "vote": self.onVote(VoteMessage(msg)) return - elif msg["a"] == "advance": + elif msg["a"] == "advance": self.onAdvance(AdvanceMessage(msg)) return elif msg["a"] == "userLeave": @@ -52,9 +52,16 @@ def onJoin(self): def sendChat(self, message): self.bot.sendChat(message) + + def sendMeh(self): + return self.bot.REST("POST", "votes", {"direction":-1, "historyID": self.bot.data["historyID"]}) + + def sendWoot(self): + return self.bot.REST("POST", "votes", {"direction":1, "historyID": self.bot.data["historyID"]}) def getUser(self, uid): return self.bot.getUser(uid) + ############################## ## Event Handlers ## diff --git a/plugins/ookamimeh.py b/plugins/ookamimeh.py index c8e496b..7933704 100644 --- a/plugins/ookamimeh.py +++ b/plugins/ookamimeh.py @@ -11,11 +11,5 @@ def onAdvance(self, data): def onChat(self, data): if data.message == ".i": - r = self.getUser(data.uid) - print r - self.sendChat(r) - self.sendChat(self.meh()) + self.sendMeh() - - def meh(self): - return self.bot.REST("POST", "votes", {"direction":-1, "historyID": self.historyID}) From 7fa44639eee8f6f896496631ba324ba2905dc7ec Mon Sep 17 00:00:00 2001 From: yurippe Date: Sun, 31 May 2015 16:45:05 +0200 Subject: [PATCH 4/7] update to example plugins --- plugins/meh.py | 8 ++++++++ plugins/ookamimeh.py | 15 --------------- 2 files changed, 8 insertions(+), 15 deletions(-) create mode 100644 plugins/meh.py delete mode 100644 plugins/ookamimeh.py diff --git a/plugins/meh.py b/plugins/meh.py new file mode 100644 index 0000000..214f11e --- /dev/null +++ b/plugins/meh.py @@ -0,0 +1,8 @@ +from plugin import * + +class meh(plugin): + + def onChat(self, data): + if data.message == ".meh": + self.sendMeh() + diff --git a/plugins/ookamimeh.py b/plugins/ookamimeh.py deleted file mode 100644 index 7933704..0000000 --- a/plugins/ookamimeh.py +++ /dev/null @@ -1,15 +0,0 @@ -from plugin import * - -class ookamimeh(plugin): - def onEnable(self): - self.historyID = "" - - def onAdvance(self, data): - self.historyID = data.historyId - - print self.historyID - - def onChat(self, data): - if data.message == ".i": - self.sendMeh() - From 1a65a2a568662f03a0028abe2a86cbe5309780fb Mon Sep 17 00:00:00 2001 From: yurippe Date: Sun, 31 May 2015 17:16:34 +0200 Subject: [PATCH 5/7] update --- Bot.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Bot.py b/Bot.py index b6c3bb6..7e2cf51 100644 --- a/Bot.py +++ b/Bot.py @@ -61,8 +61,9 @@ def loadPlugins(this, folder): friendly_name + "(this)") print "Successfully loaded '" + friendly_name + "'" this.plugins[friendly_name]["instance"].onEnable() - except: + except Exception as e: print "Failed loading plugin '" + friendly_name + "'(Class name must be the same as filename for plugins)" + print e del this.plugins[friendly_name] @@ -115,7 +116,6 @@ def REST(this, method, endpoint, data): resp = requests.get(url, data = json.dumps(data), headers=headers, cookies=Cookies) elif(method.upper() == "POST"): - print json.dumps(data) resp = requests.post(url, data = json.dumps(data), headers=headers, cookies=Cookies) else: return @@ -219,6 +219,16 @@ def generateAuthkey(this): this.authkey = authkey def join(this): this.REST("POST", "rooms/join", {"slug": this.roomslug }) + for plugin in this.plugins.keys(): + try: + this.core.onJoin() #Update core first + this.plugins[plugin]["instance"].onJoin() + except Exception as e: + tempel1 = "|------------Error in plugin: %s-------------"%(this.plugins[plugin]["name"]) + print tempel1 + "-|" + print "|" + " "*((len(tempel1)-len(str(e)))/2) + str(e) + " "*((len(tempel1)-len(str(e)))/2) + "|" + print "|" + "-"*len(tempel1) + "|" + pass def start(this): this.generateAuthkey() From 0ccc83383044bb11c227b1808f6243007a762f99 Mon Sep 17 00:00:00 2001 From: yurippe Date: Thu, 4 Jun 2015 15:28:34 +0200 Subject: [PATCH 6/7] Python 3 compatibility Updated to work with python 3 --- Bot.py | 34 +++++++++++++++++----------------- RestAPI.py | 29 ----------------------------- plugins/ChatPrinter.py | 4 ++-- plugins/debug.py | 2 +- 4 files changed, 20 insertions(+), 49 deletions(-) delete mode 100644 RestAPI.py diff --git a/Bot.py b/Bot.py index 7e2cf51..e3418b9 100644 --- a/Bot.py +++ b/Bot.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import time import json import glob @@ -19,7 +20,6 @@ class BotPlugin(plugin.plugin): def onAdvance(self, data): self.bot.data["historyID"] = data.historyId - class WebSock(WebSocketClient): def init(this, bot): @@ -49,21 +49,21 @@ def __init__(this, username, password, roomslug): def loadPlugins(this, folder): if folder.endswith("/"): slashornot = "" else: slashornot = "/" - print "Appending '" + folder + slashornot + "' to sys.path" + print ("Appending '" + folder + slashornot + "' to sys.path") sys.path.append(folder + slashornot) - print "Loading plugins from " + folder + slashornot + print ("Loading plugins from " + folder + slashornot) for fil in glob.glob(folder + slashornot + "*.py"): friendly_name = ".".join(os.path.basename(fil).split(".")[:-1]) - print "Loading '" + friendly_name + "' ..." + print ("Loading '" + friendly_name + "' ...") this.plugins[friendly_name] = {"import": __import__(friendly_name), "name":friendly_name} try: exec('this.plugins[friendly_name]["instance"] = this.plugins[friendly_name]["import"].' + friendly_name + "(this)") - print "Successfully loaded '" + friendly_name + "'" + print ("Successfully loaded '" + friendly_name + "'") this.plugins[friendly_name]["instance"].onEnable() except Exception as e: - print "Failed loading plugin '" + friendly_name + "'(Class name must be the same as filename for plugins)" - print e + print ("Failed loading plugin '" + friendly_name + "'(Class name must be the same as filename for plugins)") + print (e) del this.plugins[friendly_name] @@ -79,9 +79,9 @@ def onRecv(this, websocket, message): this.plugins[plugin]["instance"].onRecv(message) except Exception as e: tempel1 = "|------------Error in plugin: %s-------------"%(this.plugins[plugin]["name"]) - print tempel1 + "-|" - print "|" + " "*((len(tempel1)-len(str(e)))/2) + str(e) + " "*((len(tempel1)-len(str(e)))/2) + "|" - print "|" + "-"*len(tempel1) + "|" + print (tempel1 + "-|") + print ("|" + " "*((len(tempel1)-len(str(e)))/2) + str(e) + " "*((len(tempel1)-len(str(e)))/2) + "|") + print ("|" + "-"*len(tempel1) + "|") pass def sendChat(this, msg): @@ -168,8 +168,8 @@ def getCookies(urllibresponse): request.get_method = lambda: "POST" con = opener.open(request) - print con.headers - print con.read() + print (con.headers) + print (con.read()) return @@ -214,7 +214,7 @@ def generateAuthkey(this): f1_3 = '_jm="' f2_3 = '",_st' authkey = data_3[data_3.find(f1_3) + len(f1_3):data_3.find(f2_3)] - #print "Authkey: " + authkey + #print ("Authkey: " + authkey) this.authkey = authkey def join(this): @@ -225,15 +225,15 @@ def join(this): this.plugins[plugin]["instance"].onJoin() except Exception as e: tempel1 = "|------------Error in plugin: %s-------------"%(this.plugins[plugin]["name"]) - print tempel1 + "-|" - print "|" + " "*((len(tempel1)-len(str(e)))/2) + str(e) + " "*((len(tempel1)-len(str(e)))/2) + "|" - print "|" + "-"*len(tempel1) + "|" + print (tempel1 + "-|") + print ("|" + " "*((len(tempel1)-len(str(e)))/2) + str(e) + " "*((len(tempel1)-len(str(e)))/2) + "|") + print ("|" + "-"*len(tempel1) + "|") pass def start(this): this.generateAuthkey() - this.join() #this.urllibGenerateAuthKey() + this.join() this.ws.connect() this.ws.run_forever() diff --git a/RestAPI.py b/RestAPI.py deleted file mode 100644 index 6be1fff..0000000 --- a/RestAPI.py +++ /dev/null @@ -1,29 +0,0 @@ -import json -import requests - -def REST(bot, method, endpoint, data): - url = 'https://plug.dj/_/' + endpoint - Cookies = {'session': 'cb87722d-91ca-4008-be9a-9cf8175b634a|2592000|cecaf6f29ee4aedad668c29f7d612c9245505984863cd8fda2cadf427aa255b4', - } - x = { "doxygen_width" : "382", "__insp_slim":"1410877868274", "__insp_nv":"true", - "__insp_ref":"aHR0cHM6Ly9wbHVnLmRqL3RoZW5pZ2h0Y29ZWNsdWl%3D", - "wooTracker":"PapkJAwe1foy", "ajs_anonymous_id":"%220fc78d1e-0d1e-441e-8f96-435d4ffb0fca%22", "ajs_user_id":"4769627", - "ajs_group_id":"null"} - headers = {"Accept" : "application/json, text/javascript, */*; q=0.01", - "Content-Type": "application/json", - "User-Agent": "plugapi_1"} - - if(method.upper() == "GET"): - resp = requests.get(url, data = json.dumps(data), - headers=headers, cookies=Cookies) - elif(method.upper() == "POST"): - print json.dumps(data) - resp = requests.post(url, data = json.dumps(data), - headers=headers, cookies=Cookies) - else: return - - data = resp.text - print data - -REST(None, "POST", "votes", {"direction":-1, "historyID": "bf5f748d-3049-4f56-9998-d60b62969880"}) -REST(None, "GET", "users/" + "4769627", "") diff --git a/plugins/ChatPrinter.py b/plugins/ChatPrinter.py index 2f2ddb4..44b513e 100644 --- a/plugins/ChatPrinter.py +++ b/plugins/ChatPrinter.py @@ -3,7 +3,7 @@ class ChatPrinter(plugin): def onEnable(self): - print "Chat printer enabled" + print ("Chat printer enabled") def onChat(self, msg): - print msg.user + ": " + msg.message + print (msg.user + ": " + msg.message) diff --git a/plugins/debug.py b/plugins/debug.py index de8993a..8b78cc9 100644 --- a/plugins/debug.py +++ b/plugins/debug.py @@ -3,4 +3,4 @@ class debug(plugin): def onRecv(self, data): - print data + print (data) From 79c315919480b86a4446590516b07c6212f0bbc4 Mon Sep 17 00:00:00 2001 From: yurippe Date: Fri, 19 Jun 2015 21:58:44 +0200 Subject: [PATCH 7/7] CSRF token fix Fixed the way to find the csrf token --- Bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bot.py b/Bot.py index e3418b9..852b391 100644 --- a/Bot.py +++ b/Bot.py @@ -138,7 +138,7 @@ def getCookies(urllibresponse): resp1 = urllib2.urlopen(url) cookies = getCookies(resp1) data = resp1.read() - f1 = '_csrf = "' + f1 = '_csrf="' f2 = '", _fb' csrf = data[data.find(f1) + len(f1):data.find(f2)] @@ -178,7 +178,7 @@ def generateAuthkey(this): with requests.Session() as my_session: dat = my_session.get(url) data = dat.text - f1 = '_csrf = "' + f1 = '_csrf="' f2 = '", _fb' csrf = data[data.find(f1) + len(f1):data.find(f2)] #print "CSRF:" + csrf