Search code examples
pythontweetstwython

How to auto follow someone with Twython


I am making a bot that automatically re-tweets and favorites anything containing the keyword 'Australia'

I am successfully able to re-tweet and favorite the tweet automatically, but I have no idea how to follow them automatically (follow everyone I re-tweet automatically)

search_results = twitter.search(q='Australia', count=10)
try:
    for tweet in search_results["statuses"]:
        try:

            twitter.retweet(id = tweet["id_str"])
            twitter.create_favorite(id = tweet["id_str"])
            twitter.create_friendship(user_id=?????)
        except TwythonError as e:
            print (e)
except TwythonError as e:
        print (e)

Solution

  • After lot of trying found this. Try the below

    twitter = Twython(consumer_key,consumer_secret, access_token, access_secret)
    search_results = twitter.search(q='Australia', count=10)
    try:
        for tweet in search_results["statuses"]:
            try:
                twitter.retweet(id = tweet["id_str"])
                twitter.create_favorite(id = tweet["id_str"])
                st=tweet["entities"]["user_mentions"]
                if st != []:
                    twitter.create_friendship(screen_name=st[0]["screen_name"])
            except TwythonError as e:
                print e
    except TwythonError as e:
            print e
    

    use screen_name to follow the respective handle.