-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredalert.py
66 lines (61 loc) · 2.02 KB
/
redalert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from twitter import *
import socket
import struct
import os
from time import strftime
from time import sleep
from config import *
##########################################
# be sure to `pip install twitter` first #
##########################################
# Add your stuff in config.py
# Setup
magic = "\xFE"
t = Twitter(auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET))
print "\n"
print "########################################"
print "# savi's server downtime tweeter thing #"
print "########################################"
print "\n"
# assume server is up
down = False
alreadyTweeted = None
while 1:
# Determine whether the server is up or down
try:
time = strftime("%H:%M")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server
# It should error out here, if the server is down.
s.connect((mcip, port))
# Send the magic code
s.send(magic)
# Open our present
data = s.recv(1024)
# Bye!
s.close()
# server is running fine! print MOTD
print str(time) + " > " + data
# caching
down = False
if alreadyTweeted:
t.statuses.update(status=PREFIX + " " + SERVER_NAME + " is back! :) (BTW savi loves ocelot.)")
print "Server is back... tweeting\n"
alreadyTweeted = False
except Exception, e:
# server must be down
time = strftime("%H:%M")
# Log
print str(time) + " > " + str(e)
down = True
# Tell everyone
if down and not alreadyTweeted:
if DM_ME:
# Send DM
t.direct_messages.new(user=DM_USER,text="Hey, " + SERVER_NAME + " is down! And savi loves ocelot.")
if TWEET_DOWNTIME:
# Send tweet
t.statuses.update(status=PREFIX + " " + SERVER_NAME + " is down as of " + time + " EST. :(")
print "Server down... tweeting\n"
alreadyTweeted = True
sleep(interval)