코드 조각

들어가며

여기에는 당신이 Tweepy를 사용하는 데에 도움을 줄 몇 개의 코드 조각들이 있습니다. 직접 만든 코드를 기여하거나, 여기 있는 코드를 개선해주세요!

OAuth

auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")

# Redirect user to Twitter to authorize
redirect_user(auth.get_authorization_url())

# Get access token
auth.get_access_token("verifier_value")

# Construct the API instance
api = tweepy.API(auth)

모든 팔로워 팔로우하기

아래 코드는 현재 인증된 사용자의 모든 팔로워를 팔로우 하도록 합니다.

for follower in tweepy.Cursor(api.followers).items():
    follower.follow()

커서 이용 한도의 처리

커서는 커서 안의 next() 메소드 안에서 RateLimitError 를 일으킵니다. 이 오류는 커서를 반복자로 감쌈으로써 처리할 수 있습니다.

이 코드를 실행하면 당신이 팔로우한 모든 유저 중 300명 이하를 팔로우하는 유저들을 출력하고, 속도 제한에 도달할 때마다 15분간 기다릴 것입니다. 이 코드는 명백한 스팸봇을 제외하기 위한 예제입니다.

# In this example, the handler is time.sleep(15 * 60),
# but you can of course handle it in any way you want.

def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            time.sleep(15 * 60)

for follower in limit_handled(tweepy.Cursor(api.followers).items()):
    if follower.friends_count < 300:
        print(follower.screen_name)