everything is in small beautiful functions now

This commit is contained in:
b3yond 2017-06-17 20:16:03 +02:00
parent 48ba20e266
commit a3e6c36926

View file

@ -16,7 +16,9 @@ class Retweetbot(object):
def __init__(self, keypath="appkeys/ticketfrei@twitter.com", def __init__(self, keypath="appkeys/ticketfrei@twitter.com",
historypath="last_mention", historypath="last_mention",
triggerpath="triggerwords"): triggerpath="triggerwords",
user_id="801098086005243904",
screen_name="links_tech"):
""" """
Initializes the bot and loads all the necessary data. Initializes the bot and loads all the necessary data.
@ -31,6 +33,8 @@ class Retweetbot(object):
access_token_secret=keys[3].strip()) access_token_secret=keys[3].strip())
self.historypath = historypath self.historypath = historypath
self.triggerpath = triggerpath self.triggerpath = triggerpath
self.user_id = user_id
self.screen_name = screen_name
self.last_mention = bot.get_history(self.historypath) self.last_mention = bot.get_history(self.historypath)
self.triggers = bot.get_trigger(self.triggerpath) self.triggers = bot.get_trigger(self.triggerpath)
@ -71,16 +75,9 @@ class Retweetbot(object):
toot = status.user.name + ": " + status.text toot = status.user.name + ": " + status.text
return toot return toot
def flow(self): def crawl_mentions(self):
"""
"""
try:
while 1:
sleep(1)
# Store all mentions in a list of Status Objects
done = False done = False
mentions = []
while not done: while not done:
try: try:
mentions = self.api.GetMentions(since_id=self.last_mention) mentions = self.api.GetMentions(since_id=self.last_mention)
@ -88,19 +85,15 @@ class Retweetbot(object):
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
print("[ERROR] Bad Connection.") print("[ERROR] Bad Connection.")
sleep(10) sleep(10)
return mentions
print mentions # debug def trigger_rt(self, status):
for status in mentions:
# Is the Text of the Tweet in the triggerlist?
should_retweet = False
for triggerword in self.triggers: for triggerword in self.triggers:
if status.text.lower().find(triggerword): if status.text.lower().find(triggerword):
should_retweet = True return True
break return False
# Retweet status def retweet(self, status):
if should_retweet:
done = False done = False
while not done: while not done:
try: try:
@ -116,16 +109,35 @@ class Retweetbot(object):
print("[ERROR] Bad Connection.") print("[ERROR] Bad Connection.")
sleep(10) sleep(10)
def flow(self):
""" The flow of crawling mentions and retweeting them."""
# Store all mentions in a list of Status Objects
mentions = self.crawl_mentions()
for status in mentions:
# Is the Text of the Tweet in the triggerlist?
should_retweet = self.trigger_rt(status)
# Retweet status
if should_retweet:
self.retweet(status)
# save the id so it doesn't get crawled again # save the id so it doesn't get crawled again
self.last_mention = status.id self.last_mention = status.id
print self.last_mention print self.last_mention
except:
def shutdown(self):
print "[ERROR] Shit went wrong, closing down." print "[ERROR] Shit went wrong, closing down."
with open(self.historypath, "w") as f: with open(self.historypath, "w") as f:
f.write(str(self.last_mention)) f.write(str(self.last_mention))
self.api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech") self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.user_id, self.screen_name)
if __name__ == "main": if __name__ == "main":
# create an Api object # create an Api object
bot = Retweetbot() bot = Retweetbot()
try:
bot.flow()
except:
bot.shutdown()