[PYTHON] Follow back by specifying conditions using tweepy

Introduction

It is often followed on Twitter, but it is troublesome and troublesome to check the user and return the follow each time. So I posted an article because it would be convenient if I could follow them all at once by specifying appropriate conditions (probably nth decoction).

Like no pheasant

I will introduce how to follow (follow back) users who follow me but do not return follow using tweepy which can easily handle Twitter API with python module!

There are two conditions to follow back this time

(I think there are various other follow-back criteria such as the difference between the number of followers and followers, hobbies and tastes ...)

Kaihatsukankyo

OS X 10.9.4 (Marvericks) python 2.7.5 tweepy 2.3 http://www.tweepy.org/

Jizenjunbi

Get an access token from Twitter Developers

Twitter Developers https://dev.twitter.com/

The following URL is very easy to understand about how to get an access token. Follow steps 1 and 2 to get an access token. http://syncer.jp/twitter-api-how-to-get-access-token

Introduced tweepy 2.3

Introduce tweepy with python's package management system pip (if you can use pyenv, use that as well) Basically, it is recommended to install the latest version, but since the specifications of tweepy change frequently, the version specification method is described for the time being. pip install tweepy==2.3

Process flow

  1. Get api after authentication
  2. Get all user objects that follow you and each other
  3. Get all user objects for your followers
  4. Follow unfollowed users if they meet the criteria

Source code

follow_back.py



#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import logging

import tweepy


def get_api():
    API_KEY = "api key"
    API_SECRET = "api secret"
    ACCESS_TOKEN = "access token"
    ACCESS_TOKEN_SECRET = "access token secret"

    auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

    return tweepy.API(auth_handler=auth, wait_on_rate_limit=True)

def get_all_my_friends(api):
    all_my_friends = list()
    for friend in tweepy.Cursor(api.friends).items():
        logger.warning('Retrieving data from Twitter.')
        all_my_friends.append(friend)
    return  all_my_friends

def get_all_my_followers(api):
    all_my_followers = list()
    for follower in tweepy.Cursor(api.followers).items():
        logger.warning('Retrieving data from Twitter.')
        all_my_followers.append(follower)
    return all_my_followers

def follow_user_with_conditions(target_user, upper_limit_of_friends=2000, upper_limit_of_crazy=50):
    twitter_experience_days = (datetime.datetime.now() - target_user.created_at).days
    crazy = target_user.statuses_count * 1. / twitter_experience_days

    if target_user.friends_count < upper_limit_of_friends and crazy < upper_limit_of_crazy:
        target_user.follow()
        print u"User name:{0:15}Number of tweets:{1:<9d}Degree of abandonment(tweets/a day):{2:.2f}".format(target_user.screen_name, target_user.statuses_count, crazy)
        return True
    return False


if __name__ == "__main__":

    FORMAT = '%(asctime)s - %(name)s - %(message)s'
    logging.basicConfig(format=FORMAT)
    logger = logging.getLogger('twitter_api')

    api = get_api()

    all_my_friends = get_all_my_friends(api)

    all_my_followers = get_all_my_followers(api)

    #Follow users who are followers and do not follow each other
    for unknown_follower in all_my_followers:
        if unknown_follower not in all_my_friends:
            follow_user_with_conditions(unknown_follower)

Hosoku

in conclusion

In this article, I introduced how to follow back using tweepy. The follow-back conditions are set to the number of mutual follow-ups and the number of tweets per day.

Future outlook

As an extension method in the future, it will take time to be regulated because you will hit the API frequently, but you can get up to 3200 comments from users you follow, check whether it is spam, or have a tendency for hobbies and tastes. I think there is something similar to me. From here, tools such as nltk (natural language processing), scikit-learn (machine learning), and orange (Data Mining) come into play! Also, even if it doesn't become that big, it seems possible to filter using other user information. If there seems to be demand, I will think next time (I do not say that I will do it

Recommended Posts

Follow back by specifying conditions using tweepy
Sort by specifying conditions in CASTable
Sort the elements of the array by specifying the conditions
Try using Tweepy [Python2.7]