Skip to content

Instantly share code, notes, and snippets.

@omgwtfgames
Last active October 13, 2015 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omgwtfgames/4249352 to your computer and use it in GitHub Desktop.
Save omgwtfgames/4249352 to your computer and use it in GitHub Desktop.
Tailing a log file and tweeting events (3079 server example)
#!/usr/bin/env python
import sys, time, datetime
dont_actually_tweet = False
twitter_account_name = "3079.omgwtfgames.com"
logfilename = '/tmp/3079.log'
consumer_key=""
consumer_secret=""
access_token=""
access_token_secret=""
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
if api.me().name != twitter_account_name:
print api.me().name
print str(datetime.datetime.now()) + " : OAuth failed ..."
sys.exit()
else:
print str(datetime.datetime.now()) + " : Restarted and authorised."
"""
# Stub for using python-twitter instead.
import twitter
api = twitter.Api()
api = twitter.Api(consumer_key=consumer_key, \
consumer_secret=consumer_secret, \
access_token_key=access_token, \
access_token_secret=access_token_secret)
if api.VerifyCredentials()['name'] != twitter_account_name:
print api.VerifyCredentials()
print "OAuth failed ..."
sys.exit()
"""
def watch(fn, words, tail=True):
fp = open(fn, 'r')
if tail: fp.readlines()
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
num_players = 0
last_status = ""
words = ['has connected','has disconnected']
for hit_word, hit_sentence in watch(logfilename, words):
nickname = hit_sentence.split()[1]
if hit_word == "has connected":
num_players += 1
status = "%s connected to 3079.omgwtfgames.com (%i currently connected) #3079server http://3079.phr00t.com" % (nickname, num_players)
if status != last_status:
if dont_actually_tweet:
print str(datetime.datetime.now()) + "Would tweet: " + status
else:
api.update_status(status)
print str(datetime.datetime.now()) + " : " + status
last_status = status
#api.PostUpdate(status) # for python-twitter
if hit_word == "has disconnected":
num_players -= 1
print str(datetime.datetime.now()) + " : %s (%i players connected) " % (hit_sentence, num_players)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment