tinkered with some modules and fixed a few bugs, also added the skeleton for the auto_mode.py module.

This commit is contained in:
jesopo 2016-04-03 13:20:05 +01:00
parent 957947cf8d
commit 504e93a78d
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3
11 changed files with 84 additions and 35 deletions

View file

@ -68,11 +68,14 @@ class Bot(object):
self.poll.unregister(server.fileno()) self.poll.unregister(server.fileno())
del self.servers[server.fileno()] del self.servers[server.fileno()]
def reconnect(self, server): def reconnect(self, timer, server):
IRCServer.Server.__init__(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): if self.connect(server):
self.servers[server.fileno()] = server self.servers[server.fileno()] = server
else:
timer.redo()
def run(self): def run(self):
while self.running: while self.running:
self.lock.acquire() self.lock.acquire()

View file

@ -31,6 +31,8 @@ class Channel(object):
self.modes[mode] = set([]) self.modes[mode] = set([])
if args: if args:
self.modes[mode].add(args.lower()) 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): def remove_mode(self, mode, args=None):
if not args: if not args:
del self.modes[mode] del self.modes[mode]
@ -38,6 +40,8 @@ class Channel(object):
self.modes[mode].remove(args.lower()) self.modes[mode].remove(args.lower())
if not len(self.modes[mode]): if not len(self.modes[mode]):
del 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): def set_setting(self, setting, value):
self.bot.database.set_channel_setting(self.server.id, self.bot.database.set_channel_setting(self.server.id,
self.name, setting, value) self.name, setting, value)

View file

@ -215,6 +215,9 @@ def handle_MODE():
channel.add_mode(char, nickname) channel.add_mode(char, nickname)
else: else:
args.pop(0) 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): elif server.is_own_nickname(target):
modes = Utils.remove_colon(line_split[3]) modes = Utils.remove_colon(line_split[3])
remove = False remove = False
@ -228,6 +231,9 @@ def handle_MODE():
server.remove_own_mode(char) server.remove_own_mode(char)
else: else:
server.add_own_mode(char) 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") @handler(description="I've been invited somewhere")
def handle_INVITE(): def handle_INVITE():
nickname, username, hostname = Utils.seperate_hostmask(line_split[0]) nickname, username, hostname = Utils.seperate_hostmask(line_split[0])

View file

@ -193,6 +193,8 @@ class Server(object):
if action: if action:
message = message.split("\01ACTION ", 1)[1][:-1] message = message.split("\01ACTION ", 1)[1][:-1]
if self.has_channel(target): if self.has_channel(target):
print("logging line:")
print("channel: %s" % target)
self.get_channel(target).log.add_line(None, message, action, True) self.get_channel(target).log.add_line(None, message, action, True)
else: else:
self.get_user(target).log.add_line(None, message, action, True) self.get_user(target).log.add_line(None, message, action, True)

View file

@ -24,7 +24,7 @@ class Timer(object):
self.function(self, *self.args, **self.kwargs) self.function(self, *self.args, **self.kwargs)
def redo(self): def redo(self):
self.done = False self._done = False
self.set_started_time() self.set_started_time()
def done(self): def done(self):

18
modules/auto_mode.py Normal file
View file

@ -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

View file

@ -1,7 +1,9 @@
import json import json, re
import Utils import Utils
URL_GOOGLEBOOKS = "https://www.googleapis.com/books/v1/volumes" 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): class Module(object):
_name = "ISBN" _name = "ISBN"
@ -10,26 +12,38 @@ class Module(object):
bot.events.on("received").on("command").on("isbn").hook( bot.events.on("received").on("command").on("isbn").hook(
self.isbn, help="Get book information from a provided ISBN", self.isbn, help="Get book information from a provided ISBN",
min_args=1) 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): def isbn(self, event):
isbn = event["args_split"][0] isbn = event["args_split"][0]
if len(isbn) == 10: if len(isbn) == 10:
isbn = "978%s" % isbn isbn = "978%s" % isbn
isbn = isbn.replace("-", "") isbn = isbn.replace("-", "")
page = Utils.get_url(URL_GOOGLEBOOKS, get_params={ self.get_book("isbn:%s" % isbn, event)
"q": "isbn:%s" % isbn, "country": "us"}, json=True)
if page: def book(self, event):
if page["totalItems"] > 0: self.get_book(event["args"], event)
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")

View file

@ -88,7 +88,6 @@ class Module(object):
return return
log = target.log log = target.log
log.skip_next()
module_name = hook.function.__self__._name module_name = hook.function.__self__._name
stdout, stderr = StdOut(module_name, target), StdErr(module_name, stdout, stderr = StdOut(module_name, target), StdErr(module_name,
@ -99,6 +98,7 @@ class Module(object):
for returned in returns: for returned in returns:
if returned: if returned:
stderr.write(returned).send() stderr.write(returned).send()
log.skip_next()
return return
args_split = list(filter(None, event["message_split"][args_index:])) args_split = list(filter(None, event["message_split"][args_index:]))
min_args = hook.kwargs.get("min_args") min_args = hook.kwargs.get("min_args")
@ -117,6 +117,8 @@ class Module(object):
target.last_stdout = stdout target.last_stdout = stdout
stderr.send() stderr.send()
target.last_stderr = stderr target.last_stderr = stderr
log.skip_next()
def channel_message(self, event): def channel_message(self, event):
command_prefix = event["channel"].get_setting("command_prefix", command_prefix = event["channel"].get_setting("command_prefix",

View file

@ -26,14 +26,14 @@ class Module(object):
def postboot_channelset(self, event): def postboot_channelset(self, event):
self._postboot_set(self.channel_settings, 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: if len(event["args_split"]) > 1:
setting = event["args_split"][0].lower() setting = event["args_split"][0].lower()
if setting in settings: if setting in settings:
value = " ".join(event["args_split"][1:]) value = " ".join(event["args_split"][1:])
value = settings[setting]["validate"](value) value = settings[setting]["validate"](value)
if not value == None: if not value == None:
event["target"].set_setting(setting, value) target.set_setting(setting, value)
event["stdout"].write("Saved setting") event["stdout"].write("Saved setting")
else: else:
event["stderr"].write("Invalid value") event["stderr"].write("Invalid value")
@ -45,11 +45,11 @@ class Module(object):
event["stdout"].write("Available settings: %s" % ( event["stdout"].write("Available settings: %s" % (
", ".join(settings.keys()))) ", ".join(settings.keys())))
def set(self, event): def set(self, event):
self._set(self.settings, event["user"], event) self._set(self.settings, event, event["user"])
def channel_set(self, event): def channel_set(self, event):
if event["target"].mode_or_above(event["user"].nickname, if event["target"].mode_or_above(event["user"].nickname,
"o"): "o"):
self._set(self.channel_settings, event) self._set(self.channel_settings, event, event["target"])
else: else:
event["stderr"].write("You do not have the modes required") event["stderr"].write("You do not have the modes required")

View file

@ -29,12 +29,9 @@ class Module(object):
"trakt-api-version": "2", "trakt-api-key": "trakt-api-version": "2", "trakt-api-key":
self.bot.config["trakt-api-key"]}, json=True, self.bot.config["trakt-api-key"]}, json=True,
code=True) code=True)
if page: if page[0]:
code, page = page code, page = page
if code == 204: if code == 200:
event["stderr"].write(
"%s is not watching anything" % username)
else:
type = page["type"] type = page["type"]
if type == "movie": if type == "movie":
title = page["movie"]["title"] title = page["movie"]["title"]
@ -58,5 +55,8 @@ class Module(object):
URL_TRAKTSLUG % ("shows", slug))) URL_TRAKTSLUG % ("shows", slug)))
else: else:
print("ack! unknown trakt media type!") print("ack! unknown trakt media type!")
else:
event["stderr"].write(
"%s is not watching anything" % username)
else: else:
event["stderr"].write("Failed to load results") event["stderr"].write("Failed to load results")

View file

@ -58,9 +58,9 @@ class Module(object):
video_duration += "%s:" % match.group(1)[:-1].zfill(2 video_duration += "%s:" % match.group(1)[:-1].zfill(2
) if match.group(1) else "" ) if match.group(1) else ""
video_duration += "%s:" % match.group(2)[:-1].zfill(2 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 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" % ( return "%s (%s) uploaded by %s, %s views (%s%s%s%s) %s" % (
video_title, video_duration, video_uploader, "{:,}".format( video_title, video_duration, video_uploader, "{:,}".format(
int(video_views)), video_likes, ARROW_UP, ARROW_DOWN, video_dislikes, int(video_views)), video_likes, ARROW_UP, ARROW_DOWN, video_dislikes,
@ -72,7 +72,7 @@ class Module(object):
if event["args"]: if event["args"]:
search = event["args"] search = event["args"]
else: else:
last_youtube = event["channel"].log.find(REGEX_YOUTUBE) last_youtube = event["log"].find(REGEX_YOUTUBE)
if last_youtube: if last_youtube:
video_id = re.search(REGEX_YOUTUBE, last_youtube.message).group(1) video_id = re.search(REGEX_YOUTUBE, last_youtube.message).group(1)
if search or video_id: if search or video_id: