matthileo

Minecraft New Players Notification

Aug 21st, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import httplib, urllib
  4. from datetime import datetime
  5. import time
  6.  
  7. AppToken="ThisIsAnAppTokenGETYOUROWN";
  8. UserKey="ThisIsAUserKeyGETYOUROWN";
  9.  
  10. LogLocation = '/opt/games/McMyAdmin/Minecraft/server.log';
  11.  
  12. CurrentTime=str(datetime.now()).split('.')[0];
  13. LastUpdate="NULL";
  14.  
  15. def ConfigSetup():
  16.     print "DEBUG: Opening config file...";
  17.     ConfigFile = open('/root/scripts/server-notify.conf','r');
  18.     TestUpdate="";
  19.     print "DEBUG: Reading from config file..."
  20.     for line in ConfigFile:
  21.         if line.split(": ")[0]=="LastUpdate":
  22.             LastUpdate = line.split(": ")[1];
  23.             TestUpdate=LastUpdate;
  24.     ConfigFile.close();
  25.     LastUpdate = TestUpdate;
  26.     print "DEBUG: Done setting config values.";
  27.     return TestUpdate;
  28.  
  29. def WriteConfig():
  30.     print "DEBUG: Opening config file...";
  31.     ConfigFile = open('/root/scripts/server-notify.conf','w');
  32.     print "DEBUG: Writing new values to config file...";
  33.     ConfigFile.write("LastUpdate: " + CurrentTime);
  34.     ConfigFile.close();
  35.     print "DEBUG: Done writing config values.";
  36.     return;
  37.    
  38. def notify(message,title="KatzKraft"):
  39.     print "DEBUG: Sending notification...";
  40.     conn = httplib.HTTPSConnection("api.pushover.net:443");
  41.     conn.request("POST", "/1/messages", urllib.urlencode({"token": AppToken,"user": UserKey,"message": message,"title": title,}), { "Content-type": "application/x-www-form-urlencoded" });
  42.     print "DEBUG: " + str({"token": AppToken,"user": UserKey,"message": message,"title": title,});
  43.     print "DEBUG: Done sending notification.";
  44.     return;
  45.  
  46. def CheckNewUsers():
  47.     "Checks for new users since last check, notifies if there are.";
  48.     print "DEBUG: Checking for new users...";
  49.     ServerLog = open(LogLocation, 'r');
  50.     NewUsers=[];
  51.     ReturnMessage="";
  52.     for line in ServerLog:
  53.         if "Creating empty config" in line:
  54.             Username=line.split('/')[8];
  55.             Date=line.split(' ')[0];
  56.             Time=line.split(' ')[1];
  57.             DateTime = Date+" "+Time;
  58.             Username = Username.replace(".yml","").replace('\n',"");
  59.             if time.strptime(DateTime, "%Y-%m-%d %H:%M:%S") > time.strptime(LastUpdate, "%Y-%m-%d %H:%M:%S"):
  60.                 NewUsers.append({"Date":Date,"Time":Time,"Username":Username});
  61.     ServerLog.close();
  62.     if len(NewUsers)==0:
  63.         print "DEBUG: No new users were found.";
  64.         ReturnMessage = "No new users found.";
  65.     elif len(NewUsers)==1:
  66.         print "DEBUG: 1 new user was found.";
  67.         ReturnMessage=str(NewUsers[0]["Username"]) + " joined the server at " + NewUsers[0]["Time"] +"!";
  68.     else:
  69.         print "DEBUG: " + str(len(NewUsers)) + " new users were found.";
  70.         ReturnMessage="The following new players have joined the server: "+", ".join(str(x["Username"]) for x in NewUsers) + "!";
  71.        
  72.     if ReturnMessage=="No new users found.":
  73.         print "DEBUG: No notification.";
  74.         print "DEBUG: Done checking for new users.";
  75.         return;
  76.     else:
  77.         print "DEBUG: New users were found, sending notification.";
  78.         print "DEBUG: Done checking for new users.";
  79.         notify(ReturnMessage,"New Users");
  80.         return;
  81.  
  82. print "DEBUG: Starting KatzKraft Push Notification Script...";
  83. LastUpdate=ConfigSetup();
  84. print "DEBUG: The last update was at " + LastUpdate;
  85. print "DEBUG: The current time is " + CurrentTime;
  86. CheckNewUsers();
  87. WriteConfig();
  88. print "DEBUG: Ending KatzKraft Push Notification Script...";
  89.  
  90. print "DEBUG: Done.";
Advertisement
Add Comment
Please, Sign In to add comment