2015-01-27 01:10:53 +00:00
|
|
|
import twitter
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
|
|
|
|
from config import *
|
|
|
|
|
|
|
|
logfile = open(LOG_FILE, LOG_TYPE)
|
|
|
|
|
|
|
|
def log(text):
|
|
|
|
logfile.write(datetime.datetime.now().isoformat() + ": " + text))
|
|
|
|
|
|
|
|
def connect():
|
2015-01-27 09:52:28 +00:00
|
|
|
return twitter.Api(
|
2015-01-27 01:10:53 +00:00
|
|
|
consume_key = CONSUMER_KEY,
|
|
|
|
consumer_secret = CONSUMER_SECRET,
|
|
|
|
access_token_key = ACCESS_TOKEN_KEY,
|
|
|
|
access_token_secret = ACCESS_TOKEN_SECRET
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
log("starting minerva")
|
|
|
|
|
2015-01-27 09:52:28 +00:00
|
|
|
api = connect()
|
|
|
|
log("connected to Twitter API")
|
|
|
|
|
2015-01-27 01:10:53 +00:00
|
|
|
lastChange = 0
|
|
|
|
|
|
|
|
lastChange = api.getDirectMessages(since_id = lastChange)[0].GetId()
|
|
|
|
lastChange = api.getMentions(since_id = lastChange)[0].GetId()
|
|
|
|
|
|
|
|
while true:
|
|
|
|
if ALLOW_COMMANDS:
|
|
|
|
dms = api.getDirectMessages(since_id = lastChange)
|
|
|
|
|
|
|
|
commandsToExecute = []
|
|
|
|
for dm in dms:
|
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:
|
|
|
|
if dm.GetSenderScreenName() == user:
|
|
|
|
commandsToExecute.append([
|
|
|
|
dm.GetSenderScreenName(),
|
|
|
|
dm.GetText()
|
|
|
|
])
|
2015-01-27 10:24:34 +00:00
|
|
|
else:
|
2015-01-27 01:10:53 +00:00
|
|
|
log("unprivileged user @" + dm.GetSenderScreenName() + " tried to execute command (dm) \"" + dm.GetText().replace("\n", "\\n") + "\"\n")
|
|
|
|
|
2015-01-27 09:24:50 +00:00
|
|
|
if not ALLOW_ONLY_DM_COMMANDS:
|
2015-01-27 01:10:53 +00:00
|
|
|
mentions = api.GetMentions(since_id = lastChange)
|
|
|
|
for mention in mentions:
|
2015-01-27 10:24:34 +00:00
|
|
|
if len(COMMAND_SOURCE_ACCOUNTS) == 0:
|
|
|
|
commandsToExecute.append([
|
|
|
|
mention.GetUser().GetScreenName(),
|
|
|
|
mention.GetText()
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
for user in COMMAND_SOURCE_ACCOUNTS:
|
|
|
|
if mention.GetUser().GetScreenName() == user:
|
|
|
|
commandsToExecute.append([
|
|
|
|
mention.GetUser().GetScreenName(),
|
|
|
|
mention.GetText()
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
log("unprivileged user @" + mention.GetUser().GetScreenName() + " tried to execute command \"" + mention.GetText().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:
|
|
|
|
api.PostUpdate(status = command[0] + "Output of command is too long. I'm sry. : /")
|
2015-01-27 10:24:34 +00:00
|
|
|
else:
|
2015-01-27 01:10:53 +00:00
|
|
|
api.PostUpdate(status = command[0] + " " + output)
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
api.PostUpdate(status = (command + COMMAND_NAME_SEPERATOR + output))
|
2015-01-27 01:10:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
time.sleep(5 * 60)
|
|
|
|
|