Browse Source

Add script that notifies of joins over IFTTT

Tyler Hallada 6 years ago
parent
commit
75ef33e181
3 changed files with 55 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 52 0
      notify/notify_joins.py
  3. 2 0
      tox.ini

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
1 1
 server.log
2 2
 map
3
+secrets.py

+ 52 - 0
notify/notify_joins.py

@@ -0,0 +1,52 @@
1
+#!/usr/bin/env python
2
+# Read the Minecraft server log and diff it against the server log it saw last. If there any new joins in the diff, send
3
+# a notification.
4
+import codecs
5
+import os
6
+import re
7
+from datetime import datetime
8
+
9
+import requests
10
+
11
+from secrets import IFTTT_WEBHOOK_KEY_TYLER, IFTTT_WEBHOOK_KEY_KAELAN
12
+
13
+LOG_FILENAME = '/srv/minecraft-panic-shack/logs/latest.log'
14
+OLD_LOG_FILENAME = '/srv/minecraft-panic-shack/logs/last-read.log'
15
+USERNAME_BLACKLIST = ['anarchyeight', 'kinedactyl']
16
+IFTTT_EVENT_NAME = 'user_joined_panic_shack'
17
+
18
+
19
+def read_log(filename):
20
+    with codecs.open(filename, encoding='utf-8') as log:
21
+        return log.readlines()
22
+
23
+
24
+def save_log(filename, lines):
25
+    with codecs.open(filename, 'w', encoding='utf-8') as log:
26
+        log.writelines(lines)
27
+
28
+
29
+if __name__ == '__main__':
30
+    if (datetime.fromtimestamp(os.path.getmtime(LOG_FILENAME)) >
31
+            datetime.fromtimestamp(os.path.getmtime(OLD_LOG_FILENAME))):
32
+        new_log = read_log(LOG_FILENAME)
33
+        old_log = read_log(OLD_LOG_FILENAME)
34
+        if new_log[0] != old_log[0]:
35
+            # A log rotate occured
36
+            old_log = []
37
+        if len(new_log) > len(old_log):
38
+            for new_line in new_log[len(old_log):]:
39
+                match = re.match('[\[][0-9:]+[\]]\s[\[]Server thread/INFO]: (\S+) joined the game', new_line)
40
+                if match:
41
+                    username = match.group(1)
42
+                    if username not in USERNAME_BLACKLIST:
43
+                        # IFTTT does not support sharing Applets anymore :(
44
+                        r = requests.post(
45
+                            'https://maker.ifttt.com/trigger/{}/with/key/{}'.format(IFTTT_EVENT_NAME,
46
+                                                                                    IFTTT_WEBHOOK_KEY_TYLER),
47
+                            data={'value1': username})
48
+                        r = requests.post(
49
+                            'https://maker.ifttt.com/trigger/{}/with/key/{}'.format(IFTTT_EVENT_NAME,
50
+                                                                                    IFTTT_WEBHOOK_KEY_KAELAN),
51
+                            data={'value1': username})
52
+        save_log(OLD_LOG_FILENAME, new_log)

+ 2 - 0
tox.ini

@@ -0,0 +1,2 @@
1
+[flake8]
2
+max_line_length = 120