From 504e93a78d59d51243acbcab6831309805d01fd1 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sun, 3 Apr 2016 13:20:05 +0100 Subject: [PATCH] tinkered with some modules and fixed a few bugs, also added the skeleton for the auto_mode.py module. --- IRCBot.py | 9 ++++--- IRCChannel.py | 4 +++ IRCLineHandler.py | 6 +++++ IRCServer.py | 2 ++ Timer.py | 2 +- modules/auto_mode.py | 18 +++++++++++++ modules/{isbn.py => books.py} | 50 ++++++++++++++++++++++------------- modules/commands.py | 4 ++- modules/set.py | 8 +++--- modules/trakt.py | 10 +++---- modules/youtube.py | 6 ++--- 11 files changed, 84 insertions(+), 35 deletions(-) create mode 100644 modules/auto_mode.py rename modules/{isbn.py => books.py} (52%) diff --git a/IRCBot.py b/IRCBot.py index ac3d202f..a76175cf 100644 --- a/IRCBot.py +++ b/IRCBot.py @@ -68,11 +68,14 @@ class Bot(object): self.poll.unregister(server.fileno()) del self.servers[server.fileno()] - def reconnect(self, server): - IRCServer.Server.__init__(server) + def reconnect(self, timer, server): + IRCServer.Server.__init__(server, server.id, server.hostname, + server.port, server.password, server.ipv4, server.tls, + server.nickname, server.username, server.realname, self) if self.connect(server): self.servers[server.fileno()] = server - + else: + timer.redo() def run(self): while self.running: self.lock.acquire() diff --git a/IRCChannel.py b/IRCChannel.py index 04413282..666ebfcd 100644 --- a/IRCChannel.py +++ b/IRCChannel.py @@ -31,6 +31,8 @@ class Channel(object): self.modes[mode] = set([]) if args: self.modes[mode].add(args.lower()) + self.bot.events.on("mode").on("channel").call( + channel=self, mode=mode, args=args, remove=False) def remove_mode(self, mode, args=None): if not args: del self.modes[mode] @@ -38,6 +40,8 @@ class Channel(object): self.modes[mode].remove(args.lower()) if not len(self.modes[mode]): del self.modes[mode] + self.bot.events.on("mode").on("channel").call( + channel=self, mode=mode, args=args, remove=True) def set_setting(self, setting, value): self.bot.database.set_channel_setting(self.server.id, self.name, setting, value) diff --git a/IRCLineHandler.py b/IRCLineHandler.py index c43846c1..406dcfd8 100644 --- a/IRCLineHandler.py +++ b/IRCLineHandler.py @@ -215,6 +215,9 @@ def handle_MODE(): channel.add_mode(char, nickname) else: args.pop(0) + bot.events.on("received").on("mode").call( + line=line, line_split=line_split, server=server, bot=bot, + modes=modes, args=args, channel=channel) elif server.is_own_nickname(target): modes = Utils.remove_colon(line_split[3]) remove = False @@ -228,6 +231,9 @@ def handle_MODE(): server.remove_own_mode(char) else: server.add_own_mode(char) + bot.events.on("self").on("mode").call( + line=line, line_split=line_split, server=server, bot=bot, + modes=modes) @handler(description="I've been invited somewhere") def handle_INVITE(): nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) diff --git a/IRCServer.py b/IRCServer.py index 054eb86c..04d0695a 100644 --- a/IRCServer.py +++ b/IRCServer.py @@ -193,6 +193,8 @@ class Server(object): if action: message = message.split("\01ACTION ", 1)[1][:-1] if self.has_channel(target): + print("logging line:") + print("channel: %s" % target) self.get_channel(target).log.add_line(None, message, action, True) else: self.get_user(target).log.add_line(None, message, action, True) diff --git a/Timer.py b/Timer.py index 24cdaaed..b5017c85 100644 --- a/Timer.py +++ b/Timer.py @@ -24,7 +24,7 @@ class Timer(object): self.function(self, *self.args, **self.kwargs) def redo(self): - self.done = False + self._done = False self.set_started_time() def done(self): diff --git a/modules/auto_mode.py b/modules/auto_mode.py new file mode 100644 index 00000000..3fbdc96c --- /dev/null +++ b/modules/auto_mode.py @@ -0,0 +1,18 @@ + + +class Module(object): + def __init__(self, bot): + self.bot = bot + bot.events.on("channel").on("mode").hook(self.on_mode) + bot.events.on("received").on("join").hook(self.on_join) + + def on_mode(self, event): + if event["channel"].get_setting("auto-mode", False): + remove = event["remove"] + channel = event["channel"] + mode = event["mode"] + args = event["args"] + + def on_join(self, event): + if event["channel"].get_setting("auto-mode", False): + pass diff --git a/modules/isbn.py b/modules/books.py similarity index 52% rename from modules/isbn.py rename to modules/books.py index 67870520..c78e381f 100644 --- a/modules/isbn.py +++ b/modules/books.py @@ -1,7 +1,9 @@ -import json +import json, re import Utils URL_GOOGLEBOOKS = "https://www.googleapis.com/books/v1/volumes" +URL_BOOKINFO = "https://books.google.co.uk/books?id=%s" +REGEX_BOOKID = re.compile("id=([\w\-]+)") class Module(object): _name = "ISBN" @@ -10,26 +12,38 @@ class Module(object): bot.events.on("received").on("command").on("isbn").hook( self.isbn, help="Get book information from a provided ISBN", min_args=1) + bot.events.on("received").on("command").on("book").hook( + self.book, help="Get book information from a provided title", + min_args=1) + + def get_book(self, query, event): + page = Utils.get_url(URL_GOOGLEBOOKS, get_params={ + "q": query, "country": "us"}, json=True) + if page: + if page["totalItems"] > 0: + book = page["items"][0]["volumeInfo"] + print(book) + title = book["title"] + sub_title = (", %s" % book.get("subtitle") + ) if book.get("subtitle") else "" + authors = ", ".join(book["authors"]) + date = book["publishedDate"] + rating = book["averageRating"] + id = re.search(REGEX_BOOKID, book["infoLink"]).group(1) + info_link = URL_BOOKINFO % id + event["stdout"].write("%s - %s (%s)%s %s (%s/5.0)" % ( + title, authors, date, sub_title, info_link, rating)) + else: + event["stderr"].write("Unable to find book") + else: + event["stderr"].write("Failed to load results") def isbn(self, event): isbn = event["args_split"][0] if len(isbn) == 10: isbn = "978%s" % isbn isbn = isbn.replace("-", "") - page = Utils.get_url(URL_GOOGLEBOOKS, get_params={ - "q": "isbn:%s" % isbn, "country": "us"}, json=True) - if page: - if page["totalItems"] > 0: - book = page["items"][0]["volumeInfo"] - title = book["title"] - sub_title = book["subtitle"] - authors = ", ".join(book["authors"]) - date = book["publishedDate"] - rating = book["averageRating"] - #language = book["language"] - event["stdout"].write("%s - %s (%s), %s (%s/5.0)" % ( - title, authors, date, sub_title, rating)) - else: - event["stderr"].write("Unable to find book") - else: - event["stderr"].write("Failed to load results") + self.get_book("isbn:%s" % isbn, event) + + def book(self, event): + self.get_book(event["args"], event) diff --git a/modules/commands.py b/modules/commands.py index 3ea4a4e0..cfdfc5a6 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -88,7 +88,6 @@ class Module(object): return log = target.log - log.skip_next() module_name = hook.function.__self__._name stdout, stderr = StdOut(module_name, target), StdErr(module_name, @@ -99,6 +98,7 @@ class Module(object): for returned in returns: if returned: stderr.write(returned).send() + log.skip_next() return args_split = list(filter(None, event["message_split"][args_index:])) min_args = hook.kwargs.get("min_args") @@ -117,6 +117,8 @@ class Module(object): target.last_stdout = stdout stderr.send() target.last_stderr = stderr + log.skip_next() + def channel_message(self, event): command_prefix = event["channel"].get_setting("command_prefix", diff --git a/modules/set.py b/modules/set.py index 3628ab8d..456e4b5a 100644 --- a/modules/set.py +++ b/modules/set.py @@ -26,14 +26,14 @@ class Module(object): def postboot_channelset(self, event): self._postboot_set(self.channel_settings, event) - def _set(self, settings, event): + def _set(self, settings, event, target): if len(event["args_split"]) > 1: setting = event["args_split"][0].lower() if setting in settings: value = " ".join(event["args_split"][1:]) value = settings[setting]["validate"](value) if not value == None: - event["target"].set_setting(setting, value) + target.set_setting(setting, value) event["stdout"].write("Saved setting") else: event["stderr"].write("Invalid value") @@ -45,11 +45,11 @@ class Module(object): event["stdout"].write("Available settings: %s" % ( ", ".join(settings.keys()))) def set(self, event): - self._set(self.settings, event["user"], event) + self._set(self.settings, event, event["user"]) def channel_set(self, event): if event["target"].mode_or_above(event["user"].nickname, "o"): - self._set(self.channel_settings, event) + self._set(self.channel_settings, event, event["target"]) else: event["stderr"].write("You do not have the modes required") diff --git a/modules/trakt.py b/modules/trakt.py index 94538b8f..b488ad40 100644 --- a/modules/trakt.py +++ b/modules/trakt.py @@ -29,12 +29,9 @@ class Module(object): "trakt-api-version": "2", "trakt-api-key": self.bot.config["trakt-api-key"]}, json=True, code=True) - if page: + if page[0]: code, page = page - if code == 204: - event["stderr"].write( - "%s is not watching anything" % username) - else: + if code == 200: type = page["type"] if type == "movie": title = page["movie"]["title"] @@ -58,5 +55,8 @@ class Module(object): URL_TRAKTSLUG % ("shows", slug))) else: print("ack! unknown trakt media type!") + else: + event["stderr"].write( + "%s is not watching anything" % username) else: event["stderr"].write("Failed to load results") diff --git a/modules/youtube.py b/modules/youtube.py index 3c766db8..8cdd2b36 100644 --- a/modules/youtube.py +++ b/modules/youtube.py @@ -58,9 +58,9 @@ class Module(object): video_duration += "%s:" % match.group(1)[:-1].zfill(2 ) if match.group(1) else "" video_duration += "%s:" % match.group(2)[:-1].zfill(2 - ) if match.group(2) else "" + ) if match.group(2) else "00:" video_duration += "%s" % match.group(3)[:-1].zfill(2 - ) if match.group(3) else "" + ) if match.group(3) else "00" return "%s (%s) uploaded by %s, %s views (%s%s%s%s) %s" % ( video_title, video_duration, video_uploader, "{:,}".format( int(video_views)), video_likes, ARROW_UP, ARROW_DOWN, video_dislikes, @@ -72,7 +72,7 @@ class Module(object): if event["args"]: search = event["args"] else: - last_youtube = event["channel"].log.find(REGEX_YOUTUBE) + last_youtube = event["log"].find(REGEX_YOUTUBE) if last_youtube: video_id = re.search(REGEX_YOUTUBE, last_youtube.message).group(1) if search or video_id: