MinervaBot/main.py

156 lines
4.5 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 datetime
import time
2015-03-23 12:59:01 +00:00
from subprocess import Popen, PIPE, STDOUT
2015-03-23 12:19:36 +00:00
ACCESS_TOKEN_KEY = "holder"
2015-03-23 12:18:42 +00:00
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 12:59:01 +00:00
print(datetime.datetime.now().isoformat() + ": " + text)
2015-01-27 01:10:53 +00:00
def connect():
global ACCESS_TOKEN_KEY
global ACCESS_TOKEN_SECRET
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:
2015-03-23 12:18:42 +00:00
if ACCESS_TOKEN_KEY == "holder":
2015-03-23 11:43:14 +00:00
print("auth url: " + auth.get_authorization_url())
2015-03-23 11:58:46 +00:00
pin = input("pin: ").strip()
2015-03-23 11:32:05 +00:00
token = auth.get_access_token(
2015-03-23 12:01:26 +00:00
verifier = pin
2015-03-23 11:32:05 +00:00
)
2015-03-23 12:13:43 +00:00
genconf = open("genconfig.py", "w")
genconf.write("# don't edit this file\n\n")
2015-03-23 12:15:28 +00:00
genconf.write("ACCESS_TOKEN_KEY = '" + token[0] + "'\n")
genconf.write("ACCESS_TOKEN_SECRET = '" + token[1] + "'\n")
2015-03-23 12:09:36 +00:00
genconf.close()
2015-03-23 11:55:33 +00:00
ACCESS_TOKEN_KEY = token[0]
ACCESS_TOKEN_SECRET = token[1]
2015-03-23 11:32:05 +00:00
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():
2015-03-23 11:43:14 +00:00
print("error! auth failed.")
2015-03-23 11:32:05 +00:00
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-03-23 12:49:32 +00:00
lastChangeT = 0
lastChangeDM = 0
2015-01-27 01:10:53 +00:00
2015-03-23 12:49:32 +00:00
lastChangeDM = api.direct_messages()
if len(lastChangeDM) == 0:
lastChangeDM = 0
2015-03-23 12:14:41 +00:00
else:
2015-03-23 12:49:32 +00:00
lastChangeDM = lastChangeDM[0].GetId()
lastChangeT = api.mentions_timeline()
2015-03-23 12:50:25 +00:00
if len(lastChangeT) != 0:
2015-03-23 12:49:32 +00:00
lastChangeT = lastChangeT[0].GetId()
2015-01-27 01:10:53 +00:00
2015-03-23 12:51:59 +00:00
while True:
2015-01-27 01:10:53 +00:00
if ALLOW_COMMANDS:
2015-03-23 12:49:32 +00:00
dms = api.direct_messages(since_id = lastChangeDM)
2015-01-27 01:10:53 +00:00
commandsToExecute = []
for dm in dms:
2015-03-23 12:49:32 +00:00
lastChangeDM = 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 12:51:59 +00:00
log("unprivileged user @" + dm.author.screen_name + " tried to execute command (dm) \"" + dm.text.replace("\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 12:49:32 +00:00
mentions = api.mentions_timeline(since_id = lastChangeT)
2015-01-27 01:10:53 +00:00
for mention in mentions:
2015-03-23 12:49:32 +00:00
lastChangeT = 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 12:51:59 +00:00
log("unprivileged user @" + mention.author.screen_name + " tried to execute command \"" + mention.text.replace("\n", "\\n") + "\"")
2015-01-27 01:10:53 +00:00
for command in commandsToExecute:
log("executing command (@" + command[0] + ") \"" + command[1].replace("\n", "\\n") + "\"")
2015-03-23 13:02:55 +00:00
output = Popen(command[1], shell=True, stdout=PIPE, stderr=STDOUT).stdout.read().decode("utf-8")
2015-01-27 01:10:53 +00:00
log("result: " + output);
if (output + command[0]).len() + 2 > 140:
2015-03-23 13:02:55 +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 13:02:55 +00:00
api.update_status(status = (command[0] + " " + output))
2015-01-27 01:10:53 +00:00
for command in UPDATE_COMMANDS:
2015-03-23 13:02:55 +00:00
output = Popen(UPDATE_COMMANDS[command], shell=True, stdout=PIPE, stderr=STDOUT).stdout.read().decode("utf-8")
2015-01-27 20:14:05 +00:00
if len(DESTINATION_ACCOUNTS):
for username in DESTINATION_ACCOUNTS:
2015-03-23 13:09:46 +00:00
text = (username + " " + command + COMMAND_NAME_SEPERATOR + output)
2015-03-23 13:11:50 +00:00
while len(text) != 0:
2015-03-23 13:09:46 +00:00
api.update_status(status = text[:140])
text = text[140:]
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:
2015-03-23 13:02:55 +00:00
output = Popen(WARNING_COMMANDS[command][0], shell=True, stderr=STDOUT, stdout=PIPE).stdout.read().decode("utf-8")
2015-01-27 20:14:05 +00:00
if output != WARNING_COMMANDS[command][1]:
if len(WARNING_DESTINATION_ACCOUNTS):
for username in WARNING_DESTINATION_ACCOUNTS:
2015-03-23 13:02:55 +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 13:02:55 +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 += 1