Suggestions
Just suggestions….
I'm using path.dirname for config.txt path. It works well with linux distros like OpenShift:
script_path = os.path.dirname(os.path.abspath( __file__ ))
config_file= script_path + "/config.txt")
Idea: I'm using blacklist.txt for retweets. It's same as get_do_not_follow_list. Useful if you dont want to retweet from someone.
BLACKLIST_FILE = script_path + '/blacklist.txt'
def get_do_not_retweet_list(self):
"""
Returns list of users the bot dont retweet.
"""
if not os.path.isfile(BLACKLIST_FILE):
with open(BLACKLIST_FILE, "r") as out_file:
out_file.write("")
do_not_retweet = set()
dnr_list = []
with open(BLACKLIST_FILE) as in_file:
for line in in_file:
dnr_list.append(int(line))
do_not_retweet.update(set(dnr_list))
del dnr_list
return do_not_retweet
Idea: don't try to retweet already tweeted. Looks like trying to retweet same tweet(searching same hash) many times cause a problem(spam and rate limit). I am going to try to record all retweeted tweet ID's as txt file. If the Tweet ID match do not to try to retweet.
Improving Rate Limit: Rate Limit returns how long you shoud wait as time format. This is complex i know. Just an idea… Don't break the script, wait until limit and continue….
Not sure if it's correct or not. Don't try to retweet multiple times:
def get_do_not_retweet_tweet(self):
"""
Returns list of current retweets.
"""
if not os.path.isfile(self.BOT_CONFIG["TWEETS_FILE"]):
with open(self.BOT_CONFIG["TWEETS_FILE"], "r") as out_file:
out_file.write("")
do_not_retweet_tweet = set()
dnrt_list = []
with open(self.BOT_CONFIG["TWEETS_FILE"]) as in_file:
for line in in_file:
dnrt_list.append(int(line))
do_not_retweet_tweet.update(set(dnrt_list))
del dnrt_list
return do_not_retweet_tweet
def auto_rt(self, q, count=100, result_type="recent"):
"""
Retweets tweets that match a phrase (hashtag, word, etc.).
"""
result = self.search_tweets(q, count, result_type)
do_not_user_retweet = self.get_do_not_retweet_list()
do_not_retweet_tweet = self.get_do_not_retweet_tweet()
for tweet in result["statuses"]:
if tweet["id"] not in do_not_retweet_tweet:
print("TwittID: %s" % tweet["id"])
try:
# don't retweet your own tweets
if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]:
continue
if tweet["user"]["id"] not in do_not_user_retweet:
result = self.TWITTER_CONNECTION.statuses.retweet(id=tweet["id"])
with open(self.BOT_CONFIG["TWEETS_FILE"], "a") as out_file:
out_file.write(str(tweet["id"]) + "\n")
print("retweeted: %s" % (result["text"].encode("utf-8")))
# when you have already retweeted a tweet, this error is thrown
except TwitterHTTPError as e:
# quit on rate limit errors
if "rate limit" in str(e).lower():
print("You have been rate limited. Wait a while before running the bot again.")
return
print("error: %s" % (str(e)))
:+1: Can you please submit this code as a pull request once you've tested it a little bit?
Regarding
Improving Rate Limit: Rate Limit returns how long you should wait as time format. This is complex i know. Just an idea… Don't break the script, wait until limit and continue….
I've never actually looked in detail how rate limiting works on the Twitter API. And do all actions equally count toward the API? I've seen times when I was rate limiting for following users but could still search tweets and favorite them.
Anyway I can retweet a users latest tweets? Or only retweet/fav a users timeline?
@Loaft: That's currently not implemented in the bot.
Honestly, I'm unsure about implementing auto-messaging features because that leads to very annoying, spammy messages being sent out all the time.