Snippety¶
Wprowadzenie¶
Tutaj znajdują się snippety, które mogą pomóc tobie w użytkowaniu Tweepy. Możesz także dodać swoje własne snippety lub usprawnić te, które się tutaj znajdują!
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)
Stronnicowanie¶
# Iterate through all of the authenticated user's friends
for friend in tweepy.Cursor(api.friends).items():
# Process the friend here
process_friend(friend)
# Iterate through the first 200 statuses in the home timeline
for status in tweepy.Cursor(api.home_timeline).items(200):
# Process the status here
process_status(status)
FollowAll¶
Ten snippet będzie obserwował każdego kto obserwuje uwierzytelnionego użytkownika.
for follower in tweepy.Cursor(api.followers).items():
follower.follow()
Obsługiwanie limitu wartości używając kursorów¶
Ponieważ kursory podnoszą RateLimitErrorw swoich metodach next(), obsługiwanie ich może być wykonane poprzez zapakowanie kursora jako iterator.
Uruchomienie tego snippeta wyświetli listę wszystkich użytkowników których obserwujesz, a którzy sami obserwują mniej niż 300 osób - między innymi by wykluczyć oczywiste spamboty - dodatkowo snippet będzie czekał 15 minut za każdym razem gdy osiągnie limit wartości.
# 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)