Skip to content

Commit

Permalink
Merge pull request #9 from CactusBot/v0.2.1
Browse files Browse the repository at this point in the history
v0.2.1
  • Loading branch information
2Cubed committed Mar 28, 2016
2 parents 80433e0 + 1c957fa commit b0e20a4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 65 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ language: python
python:
- "3.4"
- "3.5"

install:
- pip install flake8
before_script:
- "flake8 ."
script: nosetests
12 changes: 7 additions & 5 deletions beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ class Beam:
message_id = 0

def __init__(self, debug="WARNING", **kwargs):
self._init_logger(debug, kwargs.get("log_to_file", False))
self._init_logger(debug, kwargs.get("log_to_file", True))
self.http_session = Session()

def _init_logger(self, level, log_to_file=False):
def _init_logger(self, level, log_to_file=True):
"""Initialize logger."""

self.logger = get_logger("CactusBot")
Expand All @@ -35,7 +35,8 @@ def _init_logger(self, level, log_to_file=False):
if level in levels:
level_num = __import__("logging").__getattribute__(level)
self.logger.setLevel(level_num)
get_logger("urllib3").setLevel(WARNING)
get_logger("asyncio").setLevel(WARNING)
get_logger("requests").setLevel(WARNING)
get_logger("websockets").setLevel(WARNING)
self.logger.info("Logger level set to: {}".format(level))

Expand Down Expand Up @@ -128,7 +129,7 @@ def send_message(self, arguments, method="msg"):
}

if method == "msg":
self.logger.info("$[CactusBot] {message}".format(
self.logger.info("$ [CactusBot] {message}".format(
message=arguments[0]))

yield from self.websocket.send(dumps(message_packet))
Expand All @@ -144,9 +145,10 @@ def remove_message(self, channel_id, message_id):
id=channel_id, message=message_id))

def read_chat(self, handle=None):
"""Receive chat messages from Beam."""
while True:
response = loads((yield from self.websocket.recv()))
self.logger.debug(response)

if handle:
if callable(handle):
handle(response)
64 changes: 32 additions & 32 deletions cactus.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class Cactus(MessageHandler, Beam):
def __init__(self, autorestart=True, **kwargs):
super(Cactus, self).__init__(**kwargs)
self.debug = kwargs.get("DEBUG", False)
self.autorestart = autorestart
self.config_file = kwargs.get("config_file", "data/config.json")
self.stats_file = kwargs.get("stats_file", "data/stats.json")
self.database = kwargs.get("database", "data/data.db")
Expand Down Expand Up @@ -84,6 +83,9 @@ def load_config(self, filename):
exit()

def load_stats(self, filename):
self.logger.warning("Statistics are not yet implemented.")
return dict()

if exists(filename):
self.stats_file = filename
self.logger.info("Statistics file was found. Loading...")
Expand All @@ -101,27 +103,43 @@ def update_config(self, keys, value):
reduce(lambda d, k: d[k], keys.split('.')[:-1], config_data)[
keys.split('.')[-1]] = value
with open(self.config_file, 'w+') as config:
dump(config_data, config, indent=4, sort_keys=True)
dump(config_data, config, indent=2, sort_keys=True)
self.config = config_data
return self.config

def update_stats(self, keys, value):
self.logger.warning("Statistics are not yet implemented.")
return

with open(self.stats_file, 'r') as stats:
stats_data = load(stats)
reduce(lambda d, k: d[k], keys.split('.')[:-1], stats_data)[
keys.split('.')[-1]] = value
with open(self.config_file, 'w+') as config:
dump(stats_data, config, indent=4, sort_keys=True)
self.config = stats_data
with open(self.stats_file, 'w+') as stats:
dump(stats_data, stats, indent=2, sort_keys=True)
self.stats = stats_data
return self.stats

def run(self, *args, **kwargs):
"""Run bot."""

self.logger.info(cactus_art)
self._init_database(self.database)
self.load_config(filename=self.config_file)
self.load_stats(filename=self.stats_file)

while self.autorestart or not self.started:
while self.config.get("autorestart") or not self.started:
try:
self._run(args, kwargs)
self.bot_data = self.login(**self.config["auth"])
self.logger.info("Authenticated as: {}.".format(
self.bot_data["username"]))

self.started = True

self.channel = self.config["channel"]
self.channel_data = self.get_channel(self.channel)

self._init_commands()

loop = get_event_loop()

Expand Down Expand Up @@ -150,14 +168,18 @@ def run(self, *args, **kwargs):
loop.run_until_complete(
self.send_message("CactusBot deactivated! :cactus")
)
pass
self.logger.info("CactusBot deactivated.")
exit()
except Exception:
self.logger.critical("Oh no, I crashed!")
try:
loop.run_until_complete(gather(
self.send_message("Oh no, I crashed! :127")))
except Exception:
pass
self.logger.error("\n\n" + format_exc())

if self.autorestart:
if self.config.get("autorestart"):
self.logger.info("Restarting in 10 seconds...")
try:
sleep(10)
Expand All @@ -168,29 +190,7 @@ def run(self, *args, **kwargs):
self.logger.info("CactusBot deactivated.")
exit()

def _run(self, *args, **kwargs):
"""Bot execution code."""

if self.load_config(filename=self.config_file):
self.load_stats(filename=self.stats_file)
self.bot_data = self.login(**self.config["auth"])
self.username = self.bot_data["username"]
self.bot_id = self.bot_data["id"]
self.logger.info("Authenticated as: {}.".format(self.username))

self.started = True

self.channel = self.config["channel"]
self.channel_data = self.get_channel(self.channel)

self.logger.info("Channel {ch} (id {id}) is {status}.".format(
ch=self.channel_data["token"], id=self.channel_data["id"],
status=["offline", "online"][self.channel_data["online"]]
))

self._init_commands()


if __name__ == "__main__":
cactus = Cactus(debug="debug", autorestart=False, log_to_file=True)
cactus = Cactus(debug="info")
cactus.run()
1 change: 1 addition & 0 deletions data/config-template.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"channel": "CHANNEL",
"autorestart": true,
"auth": {
"username": "USERNAME",
"password": "PASSWORD"
Expand Down
9 changes: 1 addition & 8 deletions data/stats-template.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{
"total-messages": 0,
"unique-views": 0,
"total-views": 0,
"total-followers": 0,
"total-subs": 0,
"total-resubs": 0,
"total-deleted-messages": 0,
"total-banned-users": 0
"status": "Not yet implemented."
}
54 changes: 35 additions & 19 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def __call__(self, args, data):
elif args[1] == "remove":
if len(args) > 2:
command = session.query(Command).filter_by(
command=args[2]).first()
if command:
command=args[2])
if command.first():
command.delete()
session.commit()
return "Removed command !{}.".format(args[2])
Expand Down Expand Up @@ -177,7 +177,7 @@ def __call__(self, args, data):
session.commit()
return "Removed quote with ID {}.".format(args[2])
return "Quote {} does not exist!".format(args[2])
return "Invalid argument: '{}'".format(args[1])
return "Invalid argument: '{}'.".format(args[1])
return "Not enough arguments."
else:
if not session.query(Quote).count():
Expand Down Expand Up @@ -289,24 +289,40 @@ def __init__(self, update_config):
def __call__(self, args, data=None):
if len(args) >= 3:
if args[1] == "length":
self.update_config("spam_protection.maximum_message_length",
int(args[2]))
return "Maximum message length set to {}.".format(args[2])
if args[2].isdigit():
self.update_config(
"spam_protection.maximum_message_length",
int(args[2]))
return "Maximum message length set to {}.".format(
args[2])
return "Invalid number: '{}'.".format(args[2])
elif args[1] == "caps":
self.update_config("spam_protection.maximum_message_capitals",
int(args[2]))
return "Maximum capitals per message set to {}.".format(
args[2])
if args[2].isdigit():
self.update_config(
"spam_protection.maximum_message_capitals",
int(args[2]))
return "Maximum capitals per message set to {}.".format(
args[2])
return "Invalid number: '{}'.".format(args[2])
elif args[1] == "emotes":
self.update_config("spam_protection.maximum_message_emotes",
int(args[2]))
return "Maximum emotes per message set to {}.".format(args[2])
elif args[1] == "link":
self.update_config("spam_protection.allow_links",
bool(args[2]))
return "Links are now {}allowed.".format("dis"*bool(args[2]))
else:
return "Not enough arguments"
if args[2].isdigit():
self.update_config(
"spam_protection.maximum_message_emotes",
int(args[2]))
return "Maximum emotes per message set to {}.".format(
args[2])
return "Invalid number: '{}'.".format(args[2])
elif args[1] == "links":
if args[2].lower() in ("true", "false"):
links_allowed = args[2].lower() == "true"
self.update_config(
"spam_protection.allow_links",
links_allowed)
return "Links are now {dis}allowed.".format(
dis="dis"*(not links_allowed))
return "Invalid true/false: '{}'.".format(args[2])
return "Invalid argument: '{}'.".format(args[1])
return "Not enough arguments."


class ProCommand(Command):
Expand Down

0 comments on commit b0e20a4

Please sign in to comment.