Skip to content

Commit

Permalink
Merge pull request #36 from CactusBot/release-v0.3.4
Browse files Browse the repository at this point in the history
Release v0.3.4
  • Loading branch information
2Cubed authored Aug 1, 2016
2 parents 0c5a26c + 025848c commit 140ae4c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ branches:
only:
- master
- develop
- /^rel-v(\d+.){0,2}\d+$/
- /^release-v(\d+.){0,2}\d+$/
install:
- pip install flake8
before_script:
Expand Down
28 changes: 20 additions & 8 deletions beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@


class Beam:
path = "https://beam.pro/api/v1/"

message_id = 0
PATH = "https://beam.pro/api/v1/"

def __init__(self, debug="INFO", **kwargs):
self._init_logger(debug, kwargs.get("log_to_file", True))

self.message_id = 0
self.csrf_token = None

self.http_session = Session()

def _init_logger(self, level="INFO", file_logging=True, **kwargs):
Expand Down Expand Up @@ -94,7 +96,18 @@ def _init_users(self):
def _request(self, url, method="GET", **kwargs):
"""Send HTTP request to Beam."""
response = self.http_session.request(
method, urljoin(self.path, url.lstrip('/')), **kwargs)
method,
urljoin(self.PATH, url.lstrip('/')),
headers={"X-CSRF-Token": self.csrf_token},
**kwargs
)

if self.csrf_token is None:
self.csrf_token = response.headers.get("X-CSRF-Token")
elif response.status_code == 461:
self.csrf_token = response.headers.get("X-CSRF-Token")
self._request(url, method, **kwargs)

try:
return response.json()
except Exception:
Expand Down Expand Up @@ -221,10 +234,9 @@ def send_message(self, *args, method="msg"):
user=args[0],
message=args[1]))

def remove_message(self, channel_id, message_id):
def remove_message(self, message_id):
"""Remove a message from chat."""
return self._request("/chats/{id}/message/{message}".format(
id=channel_id, message=message_id), method="DELETE")
return self.send_message(message_id, method="deleteMessage")

@coroutine
def read_chat(self, handler=None):
Expand Down Expand Up @@ -407,5 +419,5 @@ def watch_liveloading(self, handler=None):
self.logger.info("- {} hosted the channel.".format(
packet["data"][1]["hoster"]["token"]))
self.send_message(
"Thanks for the hosting the channel, @{}!".format(
"Thanks for hosting the channel, @{}!".format(
packet["data"][1]["hoster"]["token"]))
10 changes: 5 additions & 5 deletions messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def message_handler(self, data):
if not (data["user_roles"][0] in mod_roles or user.friend):
if (len(parsed) > self.config["spam_protection"].get(
"maximum_message_length", 256)):
self.remove_message(data["channel"], data["id"])
self.remove_message(data["id"])
user.offenses += 1
session.commit()
return self.send_message(
Expand All @@ -101,7 +101,7 @@ def message_handler(self, data):
elif (sum(char.isupper() for char in parsed) >
self.config["spam_protection"].get(
"maximum_message_capitals", 32)):
self.remove_message(data["channel"], data["id"])
self.remove_message(data["id"])
user.offenses += 1
session.commit()
return self.send_message(
Expand All @@ -111,7 +111,7 @@ def message_handler(self, data):
for chunk in data["message"]["message"]) >
self.config["spam_protection"].get(
"maximum_message_emotes", 8)):
self.remove_message(data["channel"], data["id"])
self.remove_message(data["id"])
user.offenses += 1
session.commit()
return self.send_message(
Expand All @@ -121,15 +121,15 @@ def message_handler(self, data):
r"[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"),
parsed) and not
self.config["spam_protection"].get("allow_links", False)):
self.remove_message(data["channel"], data["id"])
self.remove_message(data["id"])
user.offenses += 1
session.commit()
return self.send_message(
data["user_name"], "Please stop posting links.",
method="whisper")

if parsed == "/cry":
self.remove_message(data["channel"], data["id"])
self.remove_message(data["id"])
return self.send_message("/me cries with {} :'(".format(
data["user_name"]))

Expand Down
8 changes: 6 additions & 2 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def __call__(self, args, data=None):
class CubeCommand(Command):

def __call__(self, args, data=None, **kwargs):
if args >= 2:
if len(args) >= 2:
if args[1] == '2' and len(args) == 2:
return "8! Whoa, that's 2Cubed!"

Expand Down Expand Up @@ -448,7 +448,11 @@ def __call__(self, args, data):
def send(self, repeat):
try:
self.send_message(
repeat.command
repeat.command(
repeat.arguments.split(),
self.data,
channel_name=self.channel
)[0]
)
except TypeError:
command_name = repeat.command_name
Expand Down

0 comments on commit 140ae4c

Please sign in to comment.