From 75ef33e1816b5c1f7293edd87092dbf70d4ff0e6 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Mon, 4 Dec 2017 13:34:01 -0500 Subject: [PATCH] Add script that notifies of joins over IFTTT --- .gitignore | 1 + notify/notify_joins.py | 52 ++++++++++++++++++++++++++++++++++++++++++ tox.ini | 2 ++ 3 files changed, 55 insertions(+) create mode 100755 notify/notify_joins.py create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore index 69916f6..844d210 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ server.log map +secrets.py diff --git a/notify/notify_joins.py b/notify/notify_joins.py new file mode 100755 index 0000000..18b9b18 --- /dev/null +++ b/notify/notify_joins.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# Read the Minecraft server log and diff it against the server log it saw last. If there any new joins in the diff, send +# a notification. +import codecs +import os +import re +from datetime import datetime + +import requests + +from secrets import IFTTT_WEBHOOK_KEY_TYLER, IFTTT_WEBHOOK_KEY_KAELAN + +LOG_FILENAME = '/srv/minecraft-panic-shack/logs/latest.log' +OLD_LOG_FILENAME = '/srv/minecraft-panic-shack/logs/last-read.log' +USERNAME_BLACKLIST = ['anarchyeight', 'kinedactyl'] +IFTTT_EVENT_NAME = 'user_joined_panic_shack' + + +def read_log(filename): + with codecs.open(filename, encoding='utf-8') as log: + return log.readlines() + + +def save_log(filename, lines): + with codecs.open(filename, 'w', encoding='utf-8') as log: + log.writelines(lines) + + +if __name__ == '__main__': + if (datetime.fromtimestamp(os.path.getmtime(LOG_FILENAME)) > + datetime.fromtimestamp(os.path.getmtime(OLD_LOG_FILENAME))): + new_log = read_log(LOG_FILENAME) + old_log = read_log(OLD_LOG_FILENAME) + if new_log[0] != old_log[0]: + # A log rotate occured + old_log = [] + if len(new_log) > len(old_log): + for new_line in new_log[len(old_log):]: + match = re.match('[\[][0-9:]+[\]]\s[\[]Server thread/INFO]: (\S+) joined the game', new_line) + if match: + username = match.group(1) + if username not in USERNAME_BLACKLIST: + # IFTTT does not support sharing Applets anymore :( + r = requests.post( + 'https://maker.ifttt.com/trigger/{}/with/key/{}'.format(IFTTT_EVENT_NAME, + IFTTT_WEBHOOK_KEY_TYLER), + data={'value1': username}) + r = requests.post( + 'https://maker.ifttt.com/trigger/{}/with/key/{}'.format(IFTTT_EVENT_NAME, + IFTTT_WEBHOOK_KEY_KAELAN), + data={'value1': username}) + save_log(OLD_LOG_FILENAME, new_log) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..dcc8dce --- /dev/null +++ b/tox.ini @@ -0,0 +1,2 @@ +[flake8] +max_line_length = 120