From e83b9d0b0c646932c71f721754fc6019caae8637 Mon Sep 17 00:00:00 2001 From: palasso Date: Fri, 25 Dec 2015 09:52:00 +0200 Subject: [PATCH 01/14] Replace import . with from import Coding style improvement: Replace import urllib.request with from urllib import request and do appropriate changes in code --- gbot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gbot.py b/gbot.py index 1c8b305..ef5eff9 100755 --- a/gbot.py +++ b/gbot.py @@ -5,7 +5,7 @@ from lxml import html import requests import json -import urllib.request +from urllib import request from html import unescape import re @@ -116,7 +116,7 @@ def norris(info,usrs): url += "?firstName=" + msg[0] + "&lastName=" if(len(msg) > 1): url += msg[1] - req = urllib.request.urlopen(url) + req = request.urlopen(url) resp = req.read() joke = json.loads(resp.decode('utf8')) say(unescape(joke['value']['joke']).replace(" ", " ")) @@ -134,7 +134,7 @@ def btc(info,usrs): cur = 'USD' msg = info['msg'].split() url = "https://api.bitcoinaverage.com/ticker/global/all" - req = urllib.request.urlopen(url) + req = request.urlopen(url) resp = req.read() data = json.loads(resp.decode('utf8')) if(len(msg) > 0): @@ -152,7 +152,7 @@ def lenny(info,usrs): def eightball(info,usrs): msg = info['msg'][len(info['botcmd']):] url = "http://8ball.delegator.com/magic/JSON/" - req = urllib.request.urlopen(url + msg) + req = request.urlopen(url + msg) resp = req.read() data = json.loads(resp.decode('utf8')) say(data['magic']['answer']) From 575e4f955cb02656f8d8c30837494e1f1d1dc69b Mon Sep 17 00:00:00 2001 From: palasso Date: Fri, 25 Dec 2015 09:58:44 +0200 Subject: [PATCH 02/14] Remove spaces in parenthesis Coding Style Improvement: Replace ( ) with () and function () with function() to make code style consistent --- gbot.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/gbot.py b/gbot.py index ef5eff9..065f594 100755 --- a/gbot.py +++ b/gbot.py @@ -32,7 +32,7 @@ readbuffer = "" -s=socket.socket( ) +s=socket.socket() s.connect((HOST, PORT)) s.send(bytes("NICK %s\r\n" % NICK, "UTF-8")) @@ -47,10 +47,10 @@ def joinch(line): def getcmd(line): botcmd = "" - if (len(line) > 3): - if (line[3][:2] == ":!"): + if(len(line) > 3): + if(line[3][:2] == ":!"): botcmd = line[3][1:] - return (botcmd) + return(botcmd) def getusr(line): sender = "" @@ -59,7 +59,7 @@ def getusr(line): break if(char != ":"): sender += char - return (sender) + return(sender) def getmsg(line): size = len(line) @@ -101,7 +101,7 @@ class commands: def smug(info,usrs): msg = info['msg'].replace(" ","") s = "Fuck you, " - if ((msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace()))): + if((msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace()))): s += info['user'] else: s += msg @@ -183,7 +183,7 @@ def parse(self,line): 'botcmd' : getcmd(line) } #handle userlist here... WIP. - if (out['cmd'] == "353"): + if(out['cmd'] == "353"): #this is terrible... find a better way later newusrs = line[5:] newusrs = ' '.join(newusrs).replace('@','').split() @@ -196,11 +196,11 @@ def parse(self,line): if(out['cmd'] == "NICK"): self.usrlist[out['channel'][1:]] = self.usrlist[out['user']] del self.usrlist[out['user']] - if (out['cmd'] == "PART" or out['cmd'] == "QUIT"): + if(out['cmd'] == "PART" or out['cmd'] == "QUIT"): del self.usrlist[out['user']] - if (out['cmd'] == "JOIN" and out['user'] != NICK): + if(out['cmd'] == "JOIN" and out['user'] != NICK): self.usrlist[out['user']] = "" - if (out['cmd'] == "KICK"): + if(out['cmd'] == "KICK"): del self.usrlist[line[3]] #run commands try: @@ -221,7 +221,7 @@ def parse(self,line): while 1: readbuffer = readbuffer+s.recv(1024).decode("UTF-8",'ignore') temp = str.split(readbuffer, "\n") - readbuffer=temp.pop( ) + readbuffer=temp.pop() for line in temp: line = str.rstrip(line) #print(line) @@ -234,15 +234,15 @@ def parse(self,line): #housekeeping done, be a bot else: x = bot.parse(line) - print (x) + print(x) #print(bot.usrlist) # check if the message in a channel contains a protocol or or www. - if (x['cmd'] == 'PRIVMSG' and x['channel'] == CHANNEL): + if(x['cmd'] == 'PRIVMSG' and x['channel'] == CHANNEL): if( x['msg'].find("http") != -1 or x['msg'].find("www.") != -1): msgArray = x['msg'].split(" ") for l in msgArray: - if (isURL(l)): + if(isURL(l)): # check if the link has a protocol if not add http if not l.lower().startswith("http"): l = 'http://' + l From 37931d1052d71c1fcb50c1f58f7306f8590b12b7 Mon Sep 17 00:00:00 2001 From: palasso Date: Fri, 25 Dec 2015 11:23:00 +0200 Subject: [PATCH 03/14] Remove one uneccessary indentation --- gbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gbot.py b/gbot.py index 065f594..983067a 100755 --- a/gbot.py +++ b/gbot.py @@ -177,7 +177,7 @@ def parse(self,line): #info returned to main loop for further processing out = { 'user' : getusr(line[0]), - 'cmd' : line[1], + 'cmd' : line[1], 'channel' :line[2], 'msg' : getmsg(line)[len(getcmd(line)):], 'botcmd' : getcmd(line) From 6cfb81b74c9267b11722a021a5cc40a9cdd523e2 Mon Sep 17 00:00:00 2001 From: palasso Date: Fri, 25 Dec 2015 11:44:18 +0200 Subject: [PATCH 04/14] Remove uneccessary indentations --- gbot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gbot.py b/gbot.py index 983067a..7d69a9f 100755 --- a/gbot.py +++ b/gbot.py @@ -174,7 +174,7 @@ def wisdom(info,usrs): } def parse(self,line): - #info returned to main loop for further processing + #info returned to main loop for further processing out = { 'user' : getusr(line[0]), 'cmd' : line[1], @@ -183,8 +183,8 @@ def parse(self,line): 'botcmd' : getcmd(line) } #handle userlist here... WIP. - if(out['cmd'] == "353"): - #this is terrible... find a better way later + if(out['cmd'] == "353") + #this is terrible... find a better way later newusrs = line[5:] newusrs = ' '.join(newusrs).replace('@','').split() newusrs = ' '.join(newusrs).replace('%','').split() From 65ff653e23dad2b99722363822634c27946175cb Mon Sep 17 00:00:00 2001 From: palasso Date: Fri, 25 Dec 2015 12:11:33 +0200 Subject: [PATCH 05/14] add one space to match indentation --- gbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gbot.py b/gbot.py index 7d69a9f..5be63d1 100755 --- a/gbot.py +++ b/gbot.py @@ -108,7 +108,7 @@ def smug(info,usrs): s += "! :]" say(s) def swag(info,usrs): - say("out of ten!") + say("out of ten!") def norris(info,usrs): msg = info['msg'].split() url = "http://api.icndb.com/jokes/random" From e152919626490499fca81daaaf041cd7055ba161 Mon Sep 17 00:00:00 2001 From: palasso Date: Sat, 26 Dec 2015 09:36:15 +0200 Subject: [PATCH 06/14] fix missing colon and replace tab with spaces --- gbot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gbot.py b/gbot.py index 5be63d1..be0fc2e 100755 --- a/gbot.py +++ b/gbot.py @@ -174,7 +174,7 @@ def wisdom(info,usrs): } def parse(self,line): - #info returned to main loop for further processing + #info returned to main loop for further processing out = { 'user' : getusr(line[0]), 'cmd' : line[1], @@ -183,7 +183,7 @@ def parse(self,line): 'botcmd' : getcmd(line) } #handle userlist here... WIP. - if(out['cmd'] == "353") + if(out['cmd'] == "353"): #this is terrible... find a better way later newusrs = line[5:] newusrs = ' '.join(newusrs).replace('@','').split() From 471a05f66e7e993745689b2a56285973fe25c57a Mon Sep 17 00:00:00 2001 From: palasso Date: Sat, 26 Dec 2015 10:41:41 +0200 Subject: [PATCH 07/14] Replace requests with request Remove requests dependency and replace requests calls with request --- gbot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gbot.py b/gbot.py index bedc813..ff2145a 100755 --- a/gbot.py +++ b/gbot.py @@ -3,7 +3,6 @@ import socket import string from lxml import html -import requests import json from urllib import request from html import unescape @@ -77,9 +76,9 @@ def say(msg): # get the title from a link and send it to the channel def getTitle(link): try: - page = requests.get(link, headers=headers, timeout=5) - page.encoding = 'UTF-8' - tree = html.fromstring(page.text) + req = request.urlopen(link, timeout=5) + resp = req.read() + tree = html.fromstring(resp) title = tree.xpath('//title/text()') say("^ " + title[0].strip()) except Exception: @@ -157,8 +156,9 @@ def eightball(info,usrs): data = json.loads(resp.decode('utf8')) say(data['magic']['answer']) def wisdom(info,usrs): - page = requests.get('http://wisdomofchopra.com/iframe.php') - tree = html.fromstring(page.content) + req = request.urlopen('http://wisdomofchopra.com/iframe.php') + resp = req.read() + tree = html.fromstring(resp) quote = tree.xpath('//table//td[@id="quote"]//header//h2/text()') say(quote[0][1:-3]) cmdlist ={ From 09e85f35e9b051c0dcc86f0a726e002f4a21d6c3 Mon Sep 17 00:00:00 2001 From: palasso Date: Fri, 1 Jan 2016 10:50:33 +0200 Subject: [PATCH 08/14] Remove importing of string and re-order imports in alphabetical order --- gbot.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gbot.py b/gbot.py index ff2145a..5921bc9 100755 --- a/gbot.py +++ b/gbot.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 -import socket -import string -from lxml import html import json -from urllib import request -from html import unescape import re +import socket +from html import unescape +from urllib import request + +from lxml import html import cfg From b1b35b91247ce89b56b4b5fb3d9f59b3792b813d Mon Sep 17 00:00:00 2001 From: Vassilis Palassopoulos Date: Fri, 1 Jan 2016 11:12:31 +0200 Subject: [PATCH 09/14] Remove redundant parentheses --- gbot.py | 60 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/gbot.py b/gbot.py index 5921bc9..b140249 100755 --- a/gbot.py +++ b/gbot.py @@ -39,32 +39,32 @@ def joinch(line): global CONNECTED - if(line[1] == "005"): + if line[1] == "005": print("Connected! Joining channel") s.send(bytes("JOIN %s %s \r\n" % (CHANNEL,KEY), "UTF-8")); CONNECTED = 1 def getcmd(line): botcmd = "" - if(len(line) > 3): - if(line[3][:2] == ":!"): + if len(line) > 3: + if line[3][:2] == ":!": botcmd = line[3][1:] - return(botcmd) + return botcmd def getusr(line): sender = "" for char in line: - if(char == "!"): + if char == "!": break - if(char != ":"): + if char != ":": sender += char - return(sender) + return sender def getmsg(line): size = len(line) i = 3 message = "" - while(i < size): + while i < size: message += line[i] + " " i = i + 1 message.lstrip(":") @@ -100,7 +100,7 @@ class commands: def smug(info,usrs): msg = info['msg'].replace(" ","") s = "Fuck you, " - if((msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace()))): + if (msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace())): s += info['user'] else: s += msg @@ -111,9 +111,9 @@ def swag(info,usrs): def norris(info,usrs): msg = info['msg'].split() url = "http://api.icndb.com/jokes/random" - if(len(msg) > 0): + if len(msg) > 0: url += "?firstName=" + msg[0] + "&lastName=" - if(len(msg) > 1): + if len(msg) > 1: url += msg[1] req = request.urlopen(url) resp = req.read() @@ -121,7 +121,7 @@ def norris(info,usrs): say(unescape(joke['value']['joke']).replace(" ", " ")) def bacon(info,usrs): msg = info['msg'].replace(" ","") - if(msg in usrs): + if msg in usrs: say("\001ACTION gives " + msg + " a delicious strip of bacon as a gift from " + info['user'] + "! \001") else: say("\001ACTION gives " + info['user'] + " a delicious strip of bacon. \001") @@ -136,14 +136,14 @@ def btc(info,usrs): req = request.urlopen(url) resp = req.read() data = json.loads(resp.decode('utf8')) - if(len(msg) > 0): - if(msg[0] in data): + if len(msg) > 0: + if msg[0] in data: cur = msg[0] say(info['user'] + ": 1 BTC = " + str(data[cur]['ask']) + " " + cur) def lenny(info,usrs): usr = "" msg = info['msg'].split() - if(len(msg) > 0 and msg[0] in usrs): + if len(msg) > 0 and msg[0] in usrs: usr = msg[0] else: usr = info['user'] @@ -183,7 +183,7 @@ def parse(self,line): 'botcmd' : getcmd(line) } #handle userlist here... WIP. - if(out['cmd'] == "353"): + if out['cmd'] == "353": #this is terrible... find a better way later newusrs = line[5:] newusrs = ' '.join(newusrs).replace('@','').split() @@ -193,29 +193,29 @@ def parse(self,line): newusrs = ' '.join(newusrs).replace('~','').split() for usr in newusrs: self.usrlist[usr] = "" - if(out['cmd'] == "NICK"): + if out['cmd'] == "NICK": self.usrlist[out['channel'][1:]] = self.usrlist[out['user']] del self.usrlist[out['user']] - if(out['cmd'] == "PART" or out['cmd'] == "QUIT"): + if out['cmd'] == "PART" or out['cmd'] == "QUIT": del self.usrlist[out['user']] - if(out['cmd'] == "JOIN" and out['user'] != NICK): + if out['cmd'] == "JOIN" and out['user'] != NICK: self.usrlist[out['user']] = "" - if(out['cmd'] == "KICK"): + if out['cmd'] == "KICK": del self.usrlist[line[3]] #run commands try: - if(out['channel'] == CHANNEL): - if(out['botcmd'][1:] in self.usrlist.keys()): - if(out['botcmd'][1:] == out['user']): + if out['channel'] == CHANNEL: + if out['botcmd'][1:] in self.usrlist.keys(): + if out['botcmd'][1:] == out['user']: self.usrlist[out['user']] = out['msg'] else: - if(not self.usrlist[out['botcmd'][1:]].isspace()): + if not self.usrlist[out['botcmd'][1:]].isspace(): say(self.usrlist[out['botcmd'][1:]]) else: self.cmdlist[out['botcmd']](out,self.usrlist) except Exception as FUCK: print(FUCK) - return(out) + return out bot = commands() while 1: @@ -227,9 +227,9 @@ def parse(self,line): #print(line) line = str.split(line) #must respond to pings to receive new messages - if(line[0] == "PING"): + if line[0] == "PING": s.send(bytes("PONG %s\r\n" % line[1], "UTF-8")) - elif(CONNECTED == 0): + elif CONNECTED == 0: joinch(line) #housekeeping done, be a bot else: @@ -238,11 +238,11 @@ def parse(self,line): #print(bot.usrlist) # check if the message in a channel contains a protocol or or www. - if(x['cmd'] == 'PRIVMSG' and x['channel'] == CHANNEL): - if( x['msg'].find("http") != -1 or x['msg'].find("www.") != -1): + if x['cmd'] == 'PRIVMSG' and x['channel'] == CHANNEL: + if x['msg'].find("http") != -1 or x['msg'].find("www.") != -1: msgArray = x['msg'].split(" ") for l in msgArray: - if(isURL(l)): + if isURL(l): # check if the link has a protocol if not add http if not l.lower().startswith("http"): l = 'http://' + l From 03e5b7994c51e54fbdd2c42d43841f509fea75ec Mon Sep 17 00:00:00 2001 From: Vassilis Palassopoulos Date: Fri, 1 Jan 2016 11:43:57 +0200 Subject: [PATCH 10/14] Add and remove whitespaces --- gbot.py | 88 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/gbot.py b/gbot.py index b140249..e404175 100755 --- a/gbot.py +++ b/gbot.py @@ -31,7 +31,7 @@ readbuffer = "" -s=socket.socket() +s = socket.socket() s.connect((HOST, PORT)) s.send(bytes("NICK %s\r\n" % NICK, "UTF-8")) @@ -41,7 +41,7 @@ def joinch(line): global CONNECTED if line[1] == "005": print("Connected! Joining channel") - s.send(bytes("JOIN %s %s \r\n" % (CHANNEL,KEY), "UTF-8")); + s.send(bytes("JOIN %s %s \r\n" % (CHANNEL, KEY), "UTF-8")) CONNECTED = 1 def getcmd(line): @@ -97,7 +97,7 @@ def isURL(string): class commands: usrlist = {} - def smug(info,usrs): + def smug(info, usrs): msg = info['msg'].replace(" ","") s = "Fuck you, " if (msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace())): @@ -106,9 +106,9 @@ def smug(info,usrs): s += msg s += "! :]" say(s) - def swag(info,usrs): + def swag(info, usrs): say("out of ten!") - def norris(info,usrs): + def norris(info, usrs): msg = info['msg'].split() url = "http://api.icndb.com/jokes/random" if len(msg) > 0: @@ -119,16 +119,16 @@ def norris(info,usrs): resp = req.read() joke = json.loads(resp.decode('utf8')) say(unescape(joke['value']['joke']).replace(" ", " ")) - def bacon(info,usrs): + def bacon(info, usrs): msg = info['msg'].replace(" ","") if msg in usrs: say("\001ACTION gives " + msg + " a delicious strip of bacon as a gift from " + info['user'] + "! \001") else: say("\001ACTION gives " + info['user'] + " a delicious strip of bacon. \001") - def listusr(info,users): + def listusr(info, users): say("I reckon there are " + str(len(users)) + " users!") print(users) - def btc(info,usrs): + def btc(info, usrs): money = 0 cur = 'USD' msg = info['msg'].split() @@ -140,57 +140,57 @@ def btc(info,usrs): if msg[0] in data: cur = msg[0] say(info['user'] + ": 1 BTC = " + str(data[cur]['ask']) + " " + cur) - def lenny(info,usrs): + def lenny(info, usrs): usr = "" msg = info['msg'].split() if len(msg) > 0 and msg[0] in usrs: usr = msg[0] else: usr = info['user'] - say( usr + ": ( ͡° ͜ʖ ͡°)") - def eightball(info,usrs): + say(usr + ": ( ͡° ͜ʖ ͡°)") + def eightball(info, usrs): msg = info['msg'][len(info['botcmd']):] url = "http://8ball.delegator.com/magic/JSON/" req = request.urlopen(url + msg) resp = req.read() data = json.loads(resp.decode('utf8')) say(data['magic']['answer']) - def wisdom(info,usrs): + def wisdom(info, usrs): req = request.urlopen('http://wisdomofchopra.com/iframe.php') resp = req.read() tree = html.fromstring(resp) quote = tree.xpath('//table//td[@id="quote"]//header//h2/text()') say(quote[0][1:-3]) - cmdlist ={ - "!smug" : smug, - "!swag" : swag, - "!cn" : norris, - "!bacon" : bacon, - "!users" : listusr, - "!btc" : btc, - "!lenny" : lenny, - "!8ball" : eightball, - "!wisdom" : wisdom + cmdlist = { + "!smug": smug, + "!swag": swag, + "!cn": norris, + "!bacon": bacon, + "!users": listusr, + "!btc": btc, + "!lenny": lenny, + "!8ball": eightball, + "!wisdom": wisdom } - def parse(self,line): - #info returned to main loop for further processing + def parse(self, line): + # info returned to main loop for further processing out = { - 'user' : getusr(line[0]), - 'cmd' : line[1], - 'channel' :line[2], - 'msg' : getmsg(line)[len(getcmd(line)):], - 'botcmd' : getcmd(line) + 'user': getusr(line[0]), + 'cmd': line[1], + 'channel': line[2], + 'msg': getmsg(line)[len(getcmd(line)):], + 'botcmd': getcmd(line) } - #handle userlist here... WIP. + # handle userlist here... WIP. if out['cmd'] == "353": - #this is terrible... find a better way later + # this is terrible... find a better way later newusrs = line[5:] - newusrs = ' '.join(newusrs).replace('@','').split() - newusrs = ' '.join(newusrs).replace('%','').split() - newusrs = ' '.join(newusrs).replace('+','').split() - newusrs = ' '.join(newusrs).replace(':','').split() - newusrs = ' '.join(newusrs).replace('~','').split() + newusrs = ' '.join(newusrs).replace('@', '').split() + newusrs = ' '.join(newusrs).replace('%', '').split() + newusrs = ' '.join(newusrs).replace('+', '').split() + newusrs = ' '.join(newusrs).replace(':', '').split() + newusrs = ' '.join(newusrs).replace('~', '').split() for usr in newusrs: self.usrlist[usr] = "" if out['cmd'] == "NICK": @@ -202,7 +202,7 @@ def parse(self,line): self.usrlist[out['user']] = "" if out['cmd'] == "KICK": del self.usrlist[line[3]] - #run commands + # run commands try: if out['channel'] == CHANNEL: if out['botcmd'][1:] in self.usrlist.keys(): @@ -212,30 +212,30 @@ def parse(self,line): if not self.usrlist[out['botcmd'][1:]].isspace(): say(self.usrlist[out['botcmd'][1:]]) else: - self.cmdlist[out['botcmd']](out,self.usrlist) + self.cmdlist[out['botcmd']](out, self.usrlist) except Exception as FUCK: print(FUCK) return out bot = commands() while 1: - readbuffer = readbuffer+s.recv(1024).decode("UTF-8",'ignore') + readbuffer = readbuffer+s.recv(1024).decode("UTF-8", 'ignore') temp = str.split(readbuffer, "\n") - readbuffer=temp.pop() + readbuffer = temp.pop() for line in temp: line = str.rstrip(line) - #print(line) + # print(line) line = str.split(line) - #must respond to pings to receive new messages + # must respond to pings to receive new messages if line[0] == "PING": s.send(bytes("PONG %s\r\n" % line[1], "UTF-8")) elif CONNECTED == 0: joinch(line) - #housekeeping done, be a bot + # housekeeping done, be a bot else: x = bot.parse(line) print(x) - #print(bot.usrlist) + # print(bot.usrlist) # check if the message in a channel contains a protocol or or www. if x['cmd'] == 'PRIVMSG' and x['channel'] == CHANNEL: From ca468e76cea60c58365e2df6516529aff1bad02b Mon Sep 17 00:00:00 2001 From: Vassilis Palassopoulos Date: Fri, 1 Jan 2016 11:45:04 +0200 Subject: [PATCH 11/14] Remove unused variable --- gbot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gbot.py b/gbot.py index e404175..1a8eff7 100755 --- a/gbot.py +++ b/gbot.py @@ -129,7 +129,6 @@ def listusr(info, users): say("I reckon there are " + str(len(users)) + " users!") print(users) def btc(info, usrs): - money = 0 cur = 'USD' msg = info['msg'].split() url = "https://api.bitcoinaverage.com/ticker/global/all" From 776004852f9721d8ac9b32e773efc38b4e283575 Mon Sep 17 00:00:00 2001 From: Vassilis Palassopoulos Date: Fri, 1 Jan 2016 12:01:40 +0200 Subject: [PATCH 12/14] Separate functions with more spaces, add more whitespaces --- gbot.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/gbot.py b/gbot.py index 1a8eff7..06ac938 100755 --- a/gbot.py +++ b/gbot.py @@ -37,6 +37,7 @@ s.send(bytes("NICK %s\r\n" % NICK, "UTF-8")) s.send(bytes("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME), "UTF-8")) + def joinch(line): global CONNECTED if line[1] == "005": @@ -44,6 +45,7 @@ def joinch(line): s.send(bytes("JOIN %s %s \r\n" % (CHANNEL, KEY), "UTF-8")) CONNECTED = 1 + def getcmd(line): botcmd = "" if len(line) > 3: @@ -51,6 +53,7 @@ def getcmd(line): botcmd = line[3][1:] return botcmd + def getusr(line): sender = "" for char in line: @@ -60,6 +63,7 @@ def getusr(line): sender += char return sender + def getmsg(line): size = len(line) i = 3 @@ -69,10 +73,13 @@ def getmsg(line): i = i + 1 message.lstrip(":") return message[1:] + + def say(msg): s.send(bytes("PRIVMSG %s :%s\r\n" % (CHANNEL, msg), "UTF-8")) return True + # get the title from a link and send it to the channel def getTitle(link): try: @@ -84,6 +91,7 @@ def getTitle(link): except Exception: print("Bad url in message: ", link) + # checks if given string is a url # it must start with either http(s):// and/or www. and contain only # characters that are acceptable in URLs @@ -97,8 +105,9 @@ def isURL(string): class commands: usrlist = {} + def smug(info, usrs): - msg = info['msg'].replace(" ","") + msg = info['msg'].replace(" ", "") s = "Fuck you, " if (msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace())): s += info['user'] @@ -106,8 +115,10 @@ def smug(info, usrs): s += msg s += "! :]" say(s) + def swag(info, usrs): say("out of ten!") + def norris(info, usrs): msg = info['msg'].split() url = "http://api.icndb.com/jokes/random" @@ -119,15 +130,18 @@ def norris(info, usrs): resp = req.read() joke = json.loads(resp.decode('utf8')) say(unescape(joke['value']['joke']).replace(" ", " ")) + def bacon(info, usrs): - msg = info['msg'].replace(" ","") + msg = info['msg'].replace(" ", "") if msg in usrs: say("\001ACTION gives " + msg + " a delicious strip of bacon as a gift from " + info['user'] + "! \001") else: say("\001ACTION gives " + info['user'] + " a delicious strip of bacon. \001") + def listusr(info, users): say("I reckon there are " + str(len(users)) + " users!") print(users) + def btc(info, usrs): cur = 'USD' msg = info['msg'].split() @@ -139,6 +153,7 @@ def btc(info, usrs): if msg[0] in data: cur = msg[0] say(info['user'] + ": 1 BTC = " + str(data[cur]['ask']) + " " + cur) + def lenny(info, usrs): usr = "" msg = info['msg'].split() @@ -147,6 +162,7 @@ def lenny(info, usrs): else: usr = info['user'] say(usr + ": ( ͡° ͜ʖ ͡°)") + def eightball(info, usrs): msg = info['msg'][len(info['botcmd']):] url = "http://8ball.delegator.com/magic/JSON/" @@ -154,6 +170,7 @@ def eightball(info, usrs): resp = req.read() data = json.loads(resp.decode('utf8')) say(data['magic']['answer']) + def wisdom(info, usrs): req = request.urlopen('http://wisdomofchopra.com/iframe.php') resp = req.read() From b4678698ed3b51636c6a52beadc693008ef85578 Mon Sep 17 00:00:00 2001 From: Vassilis Palassopoulos Date: Fri, 1 Jan 2016 12:03:01 +0200 Subject: [PATCH 13/14] Replace assignment with augmented assignment --- gbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gbot.py b/gbot.py index 06ac938..19fb580 100755 --- a/gbot.py +++ b/gbot.py @@ -70,7 +70,7 @@ def getmsg(line): message = "" while i < size: message += line[i] + " " - i = i + 1 + i += 1 message.lstrip(":") return message[1:] From ca550c83971d75f6c01312a5b59a07c979de3b1c Mon Sep 17 00:00:00 2001 From: Vassilis Palassopoulos Date: Fri, 1 Jan 2016 12:27:46 +0200 Subject: [PATCH 14/14] Rename usrs parameter to users for better consistency and readability --- gbot.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/gbot.py b/gbot.py index 19fb580..25850be 100755 --- a/gbot.py +++ b/gbot.py @@ -106,20 +106,20 @@ def isURL(string): class commands: usrlist = {} - def smug(info, usrs): + def smug(info, users): msg = info['msg'].replace(" ", "") s = "Fuck you, " - if (msg not in usrs) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace())): + if (msg not in users) or (("gamah" in str.lower(info['msg'])) or (str.lower(NICK) in str.lower(info['msg'])) or(info['msg'].isspace())): s += info['user'] else: s += msg s += "! :]" say(s) - def swag(info, usrs): + def swag(info, users): say("out of ten!") - def norris(info, usrs): + def norris(info, users): msg = info['msg'].split() url = "http://api.icndb.com/jokes/random" if len(msg) > 0: @@ -131,9 +131,9 @@ def norris(info, usrs): joke = json.loads(resp.decode('utf8')) say(unescape(joke['value']['joke']).replace(" ", " ")) - def bacon(info, usrs): + def bacon(info, users): msg = info['msg'].replace(" ", "") - if msg in usrs: + if msg in users: say("\001ACTION gives " + msg + " a delicious strip of bacon as a gift from " + info['user'] + "! \001") else: say("\001ACTION gives " + info['user'] + " a delicious strip of bacon. \001") @@ -142,7 +142,7 @@ def listusr(info, users): say("I reckon there are " + str(len(users)) + " users!") print(users) - def btc(info, usrs): + def btc(info, users): cur = 'USD' msg = info['msg'].split() url = "https://api.bitcoinaverage.com/ticker/global/all" @@ -154,16 +154,16 @@ def btc(info, usrs): cur = msg[0] say(info['user'] + ": 1 BTC = " + str(data[cur]['ask']) + " " + cur) - def lenny(info, usrs): + def lenny(info, users): usr = "" msg = info['msg'].split() - if len(msg) > 0 and msg[0] in usrs: + if len(msg) > 0 and msg[0] in users: usr = msg[0] else: usr = info['user'] say(usr + ": ( ͡° ͜ʖ ͡°)") - def eightball(info, usrs): + def eightball(info, users): msg = info['msg'][len(info['botcmd']):] url = "http://8ball.delegator.com/magic/JSON/" req = request.urlopen(url + msg) @@ -171,7 +171,7 @@ def eightball(info, usrs): data = json.loads(resp.decode('utf8')) say(data['magic']['answer']) - def wisdom(info, usrs): + def wisdom(info, users): req = request.urlopen('http://wisdomofchopra.com/iframe.php') resp = req.read() tree = html.fromstring(resp)