Skip to content

Commit

Permalink
Merge pull request #64 from kenncann/master
Browse files Browse the repository at this point in the history
Method for favoriting a tweet and then following user
  • Loading branch information
Randy Olson committed Aug 26, 2015
2 parents 6bef68b + a3ea01b commit 3e0dbe9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,21 @@ This bot has several functions for programmatically interacting with Twitter:
from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_follow("phrase")
my_bot.auto_follow_from_phrase("phrase")

You can also search based on hashtags:

from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_follow("#hashtag")
my_bot.auto_follow_from_phrase("#hashtag")

By default, the bot looks up the 100 most recent tweets. You can change this number with the `count` parameter:

from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_follow("phrase", count=1000)
my_bot.auto_follow_from_phrase("phrase", count=1000)

####Automatically follow any users that have followed you

Expand All @@ -151,14 +151,28 @@ By default, the bot looks up the 100 most recent tweets. You can change this num
from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_fav("phrase", count=1000)
my_bot.auto_fav_phrase("phrase", count=1000)

####Automatically favorite a tweet with a specific phrase, then follow that user

from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_fav_phrase_then_follow("phrase", count=1000)

####Automatically retweet any tweets that have a specific phrase

from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_rt("phrase", count=1000)
my_bot.auto_rt_phrase("phrase", count=1000)

####Automatically retweet a tweet with a specific phrase, then follow that user

from TwitterFollowBot import TwitterBot

my_bot = TwitterBot()
my_bot.auto_rt_phrase_then_follow("phrase", count=1000)

####Automatically unfollow any users that have not followed you back

Expand Down
64 changes: 49 additions & 15 deletions TwitterFollowBot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,60 @@ def get_follows_list(self):

return set(follows_list)




def search_tweets(self, phrase, count=100, result_type="recent"):
"""
Returns a list of tweets matching a phrase (hashtag, word, etc.).
"""

return self.TWITTER_CONNECTION.search.tweets(q=phrase, result_type=result_type, count=count)

def auto_fav(self, phrase, count=100, result_type="recent"):
def auto_fav_phrase(self, phrase, count = 100, result_type="recent"):
searched_tweets = self.search_tweets(phrase, count, result_type)
self.auto_fav(searched_tweets)

def auto_rt_phrase(self, phrase, count = 100, result_type="recent"):
searched_tweets = self.search_tweets(phrase, count, result_type)
self.auto_rt(searched_tweets)

def auto_follow_from_phrase(self, phrase, count = 100, result_type="recent"):
searched_tweets = self.search_tweets(phrase, count, result_type)
self.auto_follow(searched_tweets)

def auto_fav_phrase_then_follow(self, phrase, count=100, result_type="recent"):
"""
Favorites tweets that match a phrase (hashtag, word, etc.).
Follows anyone who tweets about a phrase (hashtag, word, etc.) after you favorite that tweet
"""
searched_tweets = self.search_tweets(phrase, count, result_type)
self.auto_fav(searched_tweets)
self.auto_follow(searched_tweets)

result = self.search_tweets(phrase, count, result_type)
def auto_rt_phrase_then_follow(self, phrase, count=100, result_type="recent"):
"""
Follows anyone who tweets about a phrase (hashtag, word, etc.) after you rt that tweet
"""
searched_tweets = self.search_tweets(phrase, count, result_type)
self.auto_rt(searched_tweets)
self.auto_follow(searched_tweets)

for tweet in result["statuses"]:



def auto_fav(self, searched_tweets):
"""
Favorites tweets that match a phrase (hashtag, word, etc.).
"""

for tweet in searched_tweets["statuses"]:
try:
# don't favorite your own tweets
if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]:
continue

result = self.TWITTER_CONNECTION.favorites.create(_id=tweet["id"])
print("Favorited: %s" % (result["text"].encode("utf-8")), file=sys.stdout)
searched_tweet = self.TWITTER_CONNECTION.favorites.create(_id=tweet["id"])
print("Favorited: %s" % (searched_tweet["text"].encode("utf-8")), file=sys.stdout)

# when you have already favorited a tweet, this error is thrown
except TwitterHTTPError as api_error:
Expand All @@ -252,21 +284,19 @@ def auto_fav(self, phrase, count=100, result_type="recent"):
if "you have already favorited this status" not in str(api_error).lower():
print("Error: %s" % (str(api_error)), file=sys.stderr)

def auto_rt(self, phrase, count=100, result_type="recent"):
def auto_rt(self, searched_tweets):
"""
Retweets tweets that match a phrase (hashtag, word, etc.).
"""

result = self.search_tweets(phrase, count, result_type)

for tweet in result["statuses"]:
for tweet in searched_tweets["statuses"]:
try:
# don't retweet your own tweets
if tweet["user"]["screen_name"] == self.BOT_CONFIG["TWITTER_HANDLE"]:
continue

result = self.TWITTER_CONNECTION.statuses.retweet(id=tweet["id"])
print("Retweeted: %s" % (result["text"].encode("utf-8")), file=sys.stdout)
searched_tweet = self.TWITTER_CONNECTION.statuses.retweet(id=tweet["id"])
print("Retweeted: %s" % (searched_tweet["text"].encode("utf-8")), file=sys.stdout)

# when you have already retweeted a tweet, this error is thrown
except TwitterHTTPError as api_error:
Expand All @@ -278,16 +308,15 @@ def auto_rt(self, phrase, count=100, result_type="recent"):

print("Error: %s" % (str(api_error)), file=sys.stderr)

def auto_follow(self, phrase, count=100, result_type="recent"):
def auto_follow(self, searched_tweets):
"""
Follows anyone who tweets about a phrase (hashtag, word, etc.).
"""

result = self.search_tweets(phrase, count, result_type)
following = self.get_follows_list()
do_not_follow = self.get_do_not_follow_list()

for tweet in result["statuses"]:
for tweet in searched_tweets["statuses"]:
try:
if (tweet["user"]["screen_name"] != self.BOT_CONFIG["TWITTER_HANDLE"] and
tweet["user"]["id"] not in following and
Expand All @@ -314,6 +343,9 @@ def auto_follow(self, phrase, count=100, result_type="recent"):
if "already requested to follow" not in str(api_error).lower():
print("Error: %s" % (str(api_error)), file=sys.stderr)




def auto_follow_followers(self,count=None):
"""
Follows back everyone who's followed you.
Expand Down Expand Up @@ -341,6 +373,8 @@ def auto_follow_followers(self,count=None):
if "already requested to follow" not in str(api_error).lower():
print("Error: %s" % (str(api_error)), file=sys.stderr)



def auto_follow_followers_of_user(self, user_twitter_handle, count=100):
"""
Follows the followers of a specified user.
Expand Down

0 comments on commit 3e0dbe9

Please sign in to comment.