Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import httplib, urllib
- from datetime import datetime
- import time
- AppToken="ThisIsAnAppTokenGETYOUROWN";
- UserKey="ThisIsAUserKeyGETYOUROWN";
- LogLocation = '/opt/games/McMyAdmin/Minecraft/server.log';
- CurrentTime=str(datetime.now()).split('.')[0];
- LastUpdate="NULL";
- def ConfigSetup():
- print "DEBUG: Opening config file...";
- ConfigFile = open('/root/scripts/server-notify.conf','r');
- TestUpdate="";
- print "DEBUG: Reading from config file..."
- for line in ConfigFile:
- if line.split(": ")[0]=="LastUpdate":
- LastUpdate = line.split(": ")[1];
- TestUpdate=LastUpdate;
- ConfigFile.close();
- LastUpdate = TestUpdate;
- print "DEBUG: Done setting config values.";
- return TestUpdate;
- def WriteConfig():
- print "DEBUG: Opening config file...";
- ConfigFile = open('/root/scripts/server-notify.conf','w');
- print "DEBUG: Writing new values to config file...";
- ConfigFile.write("LastUpdate: " + CurrentTime);
- ConfigFile.close();
- print "DEBUG: Done writing config values.";
- return;
- def notify(message,title="KatzKraft"):
- print "DEBUG: Sending notification...";
- conn = httplib.HTTPSConnection("api.pushover.net:443");
- conn.request("POST", "/1/messages", urllib.urlencode({"token": AppToken,"user": UserKey,"message": message,"title": title,}), { "Content-type": "application/x-www-form-urlencoded" });
- print "DEBUG: " + str({"token": AppToken,"user": UserKey,"message": message,"title": title,});
- print "DEBUG: Done sending notification.";
- return;
- def CheckNewUsers():
- "Checks for new users since last check, notifies if there are.";
- print "DEBUG: Checking for new users...";
- ServerLog = open(LogLocation, 'r');
- NewUsers=[];
- ReturnMessage="";
- for line in ServerLog:
- if "Creating empty config" in line:
- Username=line.split('/')[8];
- Date=line.split(' ')[0];
- Time=line.split(' ')[1];
- DateTime = Date+" "+Time;
- Username = Username.replace(".yml","").replace('\n',"");
- if time.strptime(DateTime, "%Y-%m-%d %H:%M:%S") > time.strptime(LastUpdate, "%Y-%m-%d %H:%M:%S"):
- NewUsers.append({"Date":Date,"Time":Time,"Username":Username});
- ServerLog.close();
- if len(NewUsers)==0:
- print "DEBUG: No new users were found.";
- ReturnMessage = "No new users found.";
- elif len(NewUsers)==1:
- print "DEBUG: 1 new user was found.";
- ReturnMessage=str(NewUsers[0]["Username"]) + " joined the server at " + NewUsers[0]["Time"] +"!";
- else:
- print "DEBUG: " + str(len(NewUsers)) + " new users were found.";
- ReturnMessage="The following new players have joined the server: "+", ".join(str(x["Username"]) for x in NewUsers) + "!";
- if ReturnMessage=="No new users found.":
- print "DEBUG: No notification.";
- print "DEBUG: Done checking for new users.";
- return;
- else:
- print "DEBUG: New users were found, sending notification.";
- print "DEBUG: Done checking for new users.";
- notify(ReturnMessage,"New Users");
- return;
- print "DEBUG: Starting KatzKraft Push Notification Script...";
- LastUpdate=ConfigSetup();
- print "DEBUG: The last update was at " + LastUpdate;
- print "DEBUG: The current time is " + CurrentTime;
- CheckNewUsers();
- WriteConfig();
- print "DEBUG: Ending KatzKraft Push Notification Script...";
- print "DEBUG: Done.";
Advertisement
Add Comment
Please, Sign In to add comment