MinervaBot/main.py

144 lines
4.2 KiB
Python
Raw Normal View History

2015-03-23 11:32:05 +00:00
import tweepy
2015-01-27 01:10:53 +00:00
import sys
import subprocess
import datetime
import time
2015-03-23 11:32:05 +00:00
import urllib
import urllib2
import string
import random
2015-01-27 01:10:53 +00:00
from config import *
2015-03-23 11:36:09 +00:00
from genconfig import *
2015-01-27 01:10:53 +00:00
logfile = open(LOG_FILE, LOG_TYPE)
def log(text):
2015-03-23 11:36:59 +00:00
logfile.write(datetime.datetime.now().isoformat() + ": " + text + "\n")
2015-03-23 11:32:05 +00:00
print datetime.datetime.now().isoformat() + ": " + text + "\n"
2015-01-27 01:10:53 +00:00
def connect():
2015-03-23 11:32:05 +00:00
auth = tweepy.OAuthHandler(
consumer_key = CONSUMER_KEY,
consumer_secret = CONSUMER_SECRET
2015-01-27 01:10:53 +00:00
)
2015-03-23 11:32:05 +00:00
if USE_PIN_AUTH:
if not ('ACCESS_TOKEN_KEY' in vars() or 'a' in globals()):
print "auth url: %s" % auth.get_authorization_url()
pin = raw_input("pin: ").strip()
token = auth.get_access_token(
verfier = pin
)
genconf = open("gen-config.py", w)
genconfig.write("# don't edit this file\n\n")
genconfig.write("ACCESS_TOKEN_KEY = " + token.key + "\n")
genconfig.write("ACCESS_TOKEN_SECRET = " + token.secret + "\n")
ACCESS_TOKEN_KEY = token.key
ACCESS_TOKEN_SECRET = token.secret
else:
ACCESS_TOKEN_KEY = NP_ACCESS_TOKEN_KEY
ACCESS_TOKEN_SECRET = NP_ACCESS_TOKEN_SECRET
auth.secure = True
auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
if not api.verify_credentials():
print "error! auth failed."
sys.exit(1)
else:
return api
2015-01-27 01:10:53 +00:00
if __name__ == "__main__":
log("starting minerva")
2015-01-27 09:52:28 +00:00
api = connect()
log("connected to Twitter API")
2015-01-27 20:14:05 +00:00
counter = 0
2015-01-27 01:10:53 +00:00
lastChange = 0
2015-03-23 11:32:05 +00:00
lastChange = api.direct_messages(since_id = lastChange)[0].GetId()
lastChange = api.mentions_timeline(since_id = lastChange)[0].GetId()
2015-01-27 01:10:53 +00:00
while true:
if ALLOW_COMMANDS:
2015-03-23 11:32:05 +00:00
dms = api.direct_messages(since_id = lastChange)
2015-01-27 01:10:53 +00:00
commandsToExecute = []
for dm in dms:
2015-03-23 11:32:05 +00:00
lastChange = dm.id
2015-01-27 09:36:31 +00:00
if len(COMMAND_SOURCE_ACCOUNTS) == 0:
2015-01-27 01:10:53 +00:00
commandsToExecute.append([
dm.GetSenderScreenName(),
dm.GetText()
])
2015-01-27 10:24:34 +00:00
else:
2015-01-27 01:10:53 +00:00
for user in COMMAND_SOURCE_ACCOUNTS:
2015-03-23 11:32:05 +00:00
if dm.author.screen_name == user:
2015-01-27 01:10:53 +00:00
commandsToExecute.append([
2015-03-23 11:32:05 +00:00
dm.author.screen_name,
dm.text
2015-01-27 01:10:53 +00:00
])
2015-01-27 10:24:34 +00:00
else:
2015-03-23 11:32:05 +00:00
log("unprivileged user @" + dm.author.screen_name + " tried to execute command (dm) \"" + dm.text.replace("\n", "\\n") + "\"\n")
2015-01-27 01:10:53 +00:00
2015-01-27 09:24:50 +00:00
if not ALLOW_ONLY_DM_COMMANDS:
2015-03-23 11:32:05 +00:00
mentions = api.mention_timeline(since_id = lastChange)
2015-01-27 01:10:53 +00:00
for mention in mentions:
2015-03-23 11:36:09 +00:00
lastChange = mention.id
2015-01-27 10:24:34 +00:00
if len(COMMAND_SOURCE_ACCOUNTS) == 0:
commandsToExecute.append([
2015-03-23 11:32:05 +00:00
mention.author.screen_name,
mention.text
2015-01-27 10:24:34 +00:00
])
else:
for user in COMMAND_SOURCE_ACCOUNTS:
2015-03-23 11:32:05 +00:00
if mention.author.screen_name == user:
2015-01-27 10:24:34 +00:00
commandsToExecute.append([
2015-03-23 11:32:05 +00:00
mention.author.screen_name,
mention.text
2015-01-27 10:24:34 +00:00
])
else:
2015-03-23 11:32:05 +00:00
log("unprivileged user @" + mention.author.screen_name + " tried to execute command \"" + mention.text.replace("\n", "\\n") + "\"\n")
2015-01-27 01:10:53 +00:00
for command in commandsToExecute:
log("executing command (@" + command[0] + ") \"" + command[1].replace("\n", "\\n") + "\"")
output = subprocess.Popen(command[1], shell=True, stdout=PIPE).stdout.read()
log("result: " + output);
if (output + command[0]).len() + 2 > 140:
2015-03-23 11:36:09 +00:00
api.update_status(status = command[0] + "Output of command is too long. I'm sry. : /")
2015-01-27 10:24:34 +00:00
else:
2015-03-23 11:36:09 +00:00
api.update_status(status = command[0] + " " + output)
2015-01-27 01:10:53 +00:00
for command in UPDATE_COMMANDS:
2015-01-27 09:28:38 +00:00
output = subprocess.Popen(UPDATE_COMMANDS[command], shell=True, stdout=PIPE).stdout.read()
2015-01-27 20:14:05 +00:00
if len(DESTINATION_ACCOUNTS):
for username in DESTINATION_ACCOUNTS:
2015-03-23 11:36:09 +00:00
api.update_status(status = ("@" + username + " " + command + COMMAND_NAME_SEPERATOR + output))
2015-01-27 20:14:05 +00:00
else:
2015-03-23 11:36:09 +00:00
api.update_status(status = (command + COMMAND_NAME_SEPERATOR + output))
2015-01-27 01:10:53 +00:00
2015-01-27 20:14:05 +00:00
if counter % 3 == 0:
for command in WARNING_COMMANDS:
output = subprocess.Popen(WARNING_COMMANDS[command][0], shell=True, stdout=PIPE).stdout.read()
if output != WARNING_COMMANDS[command][1]:
if len(WARNING_DESTINATION_ACCOUNTS):
for username in WARNING_DESTINATION_ACCOUNTS:
2015-03-23 11:36:09 +00:00
api.update_status(status = username + " WARNING: " + command + COMMAND_NAME_SEPERATOR + WARNING_COMMANDS[command][2])
2015-01-27 20:14:05 +00:00
else:
2015-03-23 11:36:09 +00:00
api.update_status(status = "WARNING: " + command + COMMAND_NAME_SEPERATOR + WARNING_COMMANDS[command][2])
2015-01-27 20:14:05 +00:00
time.sleep(5 * 60)
counter++
2015-01-27 01:10:53 +00:00