forked from ticketfrei/ticketfrei
everything is in small beautiful functions now
This commit is contained in:
parent
48ba20e266
commit
a3e6c36926
108
retweetbot.py
108
retweetbot.py
|
@ -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,61 +75,69 @@ class Retweetbot(object):
|
||||||
toot = status.user.name + ": " + status.text
|
toot = status.user.name + ": " + status.text
|
||||||
return toot
|
return toot
|
||||||
|
|
||||||
|
def crawl_mentions(self):
|
||||||
|
done = False
|
||||||
|
mentions = []
|
||||||
|
while not done:
|
||||||
|
try:
|
||||||
|
mentions = self.api.GetMentions(since_id=self.last_mention)
|
||||||
|
done = True
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
print("[ERROR] Bad Connection.")
|
||||||
|
sleep(10)
|
||||||
|
return mentions
|
||||||
|
|
||||||
|
def trigger_rt(self, status):
|
||||||
|
for triggerword in self.triggers:
|
||||||
|
if status.text.lower().find(triggerword):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def retweet(self, status):
|
||||||
|
done = False
|
||||||
|
while not done:
|
||||||
|
try:
|
||||||
|
self.api.PostRetweet(status.id)
|
||||||
|
self.bridge_mastodon(status)
|
||||||
|
done = True
|
||||||
|
|
||||||
|
# Hopefully we got rid of this error. If not, try to uncomment these lines.
|
||||||
|
# except twitter.error.TwitterError:
|
||||||
|
# print("[ERROR] probably you already retweeted this tweet.")
|
||||||
|
# done = True
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
print("[ERROR] Bad Connection.")
|
||||||
|
sleep(10)
|
||||||
|
|
||||||
def flow(self):
|
def flow(self):
|
||||||
"""
|
""" The flow of crawling mentions and retweeting them."""
|
||||||
|
|
||||||
"""
|
# Store all mentions in a list of Status Objects
|
||||||
try:
|
mentions = self.crawl_mentions()
|
||||||
while 1:
|
|
||||||
sleep(1)
|
|
||||||
|
|
||||||
# Store all mentions in a list of Status Objects
|
for status in mentions:
|
||||||
done = False
|
# Is the Text of the Tweet in the triggerlist?
|
||||||
while not done:
|
should_retweet = self.trigger_rt(status)
|
||||||
try:
|
|
||||||
mentions = self.api.GetMentions(since_id=self.last_mention)
|
|
||||||
done = True
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
print("[ERROR] Bad Connection.")
|
|
||||||
sleep(10)
|
|
||||||
|
|
||||||
print mentions # debug
|
# Retweet status
|
||||||
for status in mentions:
|
if should_retweet:
|
||||||
|
self.retweet(status)
|
||||||
|
|
||||||
# Is the Text of the Tweet in the triggerlist?
|
# save the id so it doesn't get crawled again
|
||||||
should_retweet = False
|
self.last_mention = status.id
|
||||||
for triggerword in self.triggers:
|
print self.last_mention
|
||||||
if status.text.lower().find(triggerword):
|
|
||||||
should_retweet = True
|
|
||||||
break
|
|
||||||
|
|
||||||
# Retweet status
|
def shutdown(self):
|
||||||
if should_retweet:
|
print "[ERROR] Shit went wrong, closing down."
|
||||||
done = False
|
with open(self.historypath, "w") as f:
|
||||||
while not done:
|
f.write(str(self.last_mention))
|
||||||
try:
|
self.api.PostDirectMessage("Help! I broke down. restart me pls :$", self.user_id, self.screen_name)
|
||||||
self.api.PostRetweet(status.id)
|
|
||||||
self.bridge_mastodon(status)
|
|
||||||
done = True
|
|
||||||
|
|
||||||
# Hopefully we got rid of this error. If not, try to uncomment these lines.
|
|
||||||
# except twitter.error.TwitterError:
|
|
||||||
# print("[ERROR] probably you already retweeted this tweet.")
|
|
||||||
# done = True
|
|
||||||
except requests.exceptions.ConnectionError:
|
|
||||||
print("[ERROR] Bad Connection.")
|
|
||||||
sleep(10)
|
|
||||||
|
|
||||||
# save the id so it doesn't get crawled again
|
|
||||||
self.last_mention = status.id
|
|
||||||
print self.last_mention
|
|
||||||
except:
|
|
||||||
print "[ERROR] Shit went wrong, closing down."
|
|
||||||
with open(self.historypath, "w") as f:
|
|
||||||
f.write(str(self.last_mention))
|
|
||||||
self.api.PostDirectMessage("Help! I broke down. restart me pls :$", "801098086005243904", "links_tech")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "main":
|
if __name__ == "main":
|
||||||
# create an Api object
|
# create an Api object
|
||||||
bot = Retweetbot()
|
bot = Retweetbot()
|
||||||
|
try:
|
||||||
|
bot.flow()
|
||||||
|
except:
|
||||||
|
bot.shutdown()
|
||||||
|
|
Loading…
Reference in a new issue