mirror of
https://github.com/sigmasternchen/MinervaBot
synced 2025-03-15 07:59:00 +00:00
some changes
This commit is contained in:
parent
eeba6ba3df
commit
5d163f5ebd
3 changed files with 61 additions and 23 deletions
|
@ -1,7 +1,10 @@
|
|||
CONSUMER_KEY = 'Your Twitter API Consumer Key'
|
||||
CONSUMER_SECRET = 'Your Consumer Secret Key'
|
||||
ACCESS_TOKEN_KEY = 'Your Twitter API Access Token Key'
|
||||
ACCESS_TOKEN_SECRET = 'Your Access Token Secret'
|
||||
|
||||
USE_PIN_AUTH = false
|
||||
|
||||
NP_ACCESS_TOKEN_KEY = 'Your Twitter API Access Token Key'
|
||||
NP_ACCESS_TOKEN_SECRET = 'Your Access Token Secret'
|
||||
|
||||
# empty array for no mention
|
||||
# else add comma-separated, quote-enclosed twitter names with @
|
||||
|
|
1
gen-config.py
Normal file
1
gen-config.py
Normal file
|
@ -0,0 +1 @@
|
|||
# don't edit this file
|
76
main.py
76
main.py
|
@ -1,23 +1,56 @@
|
|||
import twitter
|
||||
import tweepy
|
||||
import sys
|
||||
import subprocess
|
||||
import datetime
|
||||
import time
|
||||
|
||||
import urllib
|
||||
import urllib2
|
||||
import string
|
||||
import random
|
||||
|
||||
from config import *
|
||||
from gen-config import *
|
||||
|
||||
logfile = open(LOG_FILE, LOG_TYPE)
|
||||
|
||||
def log(text):
|
||||
logfile.write(datetime.datetime.now().isoformat() + ": " + text))
|
||||
logfile.write(datetime.datetime.now().isoformat() + ": " + text + "\n"))
|
||||
print datetime.datetime.now().isoformat() + ": " + text + "\n"
|
||||
|
||||
def connect():
|
||||
return twitter.Api(
|
||||
consume_key = CONSUMER_KEY,
|
||||
consumer_secret = CONSUMER_SECRET,
|
||||
access_token_key = ACCESS_TOKEN_KEY,
|
||||
access_token_secret = ACCESS_TOKEN_SECRET
|
||||
auth = tweepy.OAuthHandler(
|
||||
consumer_key = CONSUMER_KEY,
|
||||
consumer_secret = CONSUMER_SECRET
|
||||
)
|
||||
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
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -31,15 +64,16 @@ if __name__ == "__main__":
|
|||
|
||||
lastChange = 0
|
||||
|
||||
lastChange = api.getDirectMessages(since_id = lastChange)[0].GetId()
|
||||
lastChange = api.getMentions(since_id = lastChange)[0].GetId()
|
||||
lastChange = api.direct_messages(since_id = lastChange)[0].GetId()
|
||||
lastChange = api.mentions_timeline(since_id = lastChange)[0].GetId()
|
||||
|
||||
while true:
|
||||
if ALLOW_COMMANDS:
|
||||
dms = api.getDirectMessages(since_id = lastChange)
|
||||
dms = api.direct_messages(since_id = lastChange)
|
||||
|
||||
commandsToExecute = []
|
||||
for dm in dms:
|
||||
lastChange = dm.id
|
||||
if len(COMMAND_SOURCE_ACCOUNTS) == 0:
|
||||
commandsToExecute.append([
|
||||
dm.GetSenderScreenName(),
|
||||
|
@ -47,31 +81,31 @@ if __name__ == "__main__":
|
|||
])
|
||||
else:
|
||||
for user in COMMAND_SOURCE_ACCOUNTS:
|
||||
if dm.GetSenderScreenName() == user:
|
||||
if dm.author.screen_name == user:
|
||||
commandsToExecute.append([
|
||||
dm.GetSenderScreenName(),
|
||||
dm.GetText()
|
||||
dm.author.screen_name,
|
||||
dm.text
|
||||
])
|
||||
else:
|
||||
log("unprivileged user @" + dm.GetSenderScreenName() + " tried to execute command (dm) \"" + dm.GetText().replace("\n", "\\n") + "\"\n")
|
||||
log("unprivileged user @" + dm.author.screen_name + " tried to execute command (dm) \"" + dm.text.replace("\n", "\\n") + "\"\n")
|
||||
|
||||
if not ALLOW_ONLY_DM_COMMANDS:
|
||||
mentions = api.GetMentions(since_id = lastChange)
|
||||
mentions = api.mention_timeline(since_id = lastChange)
|
||||
for mention in mentions:
|
||||
if len(COMMAND_SOURCE_ACCOUNTS) == 0:
|
||||
commandsToExecute.append([
|
||||
mention.GetUser().GetScreenName(),
|
||||
mention.GetText()
|
||||
mention.author.screen_name,
|
||||
mention.text
|
||||
])
|
||||
else:
|
||||
for user in COMMAND_SOURCE_ACCOUNTS:
|
||||
if mention.GetUser().GetScreenName() == user:
|
||||
if mention.author.screen_name == user:
|
||||
commandsToExecute.append([
|
||||
mention.GetUser().GetScreenName(),
|
||||
mention.GetText()
|
||||
mention.author.screen_name,
|
||||
mention.text
|
||||
])
|
||||
else:
|
||||
log("unprivileged user @" + mention.GetUser().GetScreenName() + " tried to execute command \"" + mention.GetText().replace("\n", "\\n") + "\"\n")
|
||||
log("unprivileged user @" + mention.author.screen_name + " tried to execute command \"" + mention.text.replace("\n", "\\n") + "\"\n")
|
||||
|
||||
|
||||
for command in commandsToExecute:
|
||||
|
|
Loading…
Reference in a new issue