Skip to content

Instantly share code, notes, and snippets.

@koehntopp
Created September 20, 2017 10:43
Show Gist options
  • Select an option

  • Save koehntopp/2bc2dbf770c7909b17240ceafe5c2a97 to your computer and use it in GitHub Desktop.

Select an option

Save koehntopp/2bc2dbf770c7909b17240ceafe5c2a97 to your computer and use it in GitHub Desktop.
Tweepy test: Check for posters with hashtag that follow a certain user and block them
# Tweepy API documentation: http://docs.tweepy.org/en/latest/index.html
# Vorher mit 'pip install tweepy' Tweepy installieren
# Twitter App registrieren unter https://apps.twitter.com/
import tweepy
auth = tweepy.OAuthHandler("Consumer Key (API Key)", "Consumer Secret (API Secret)")
auth.set_access_token("Access Token", "Access Token Secret")
api = tweepy.API(auth)
# The user that people follow
auser = api.get_user("TwitterHandle")
auserid=auser.id
# Iterate through 100 posts with a certain hashtag, may hit API throttling eventually (15 minutes wait)
for tweet in tweepy.Cursor(api.search, q="#hashtag", count=10, include_entities=True).items(100):
userid = tweet.user.id
userfollower = tweet.user.followers_count
username = tweet.user.name.encode('utf-8', errors = 'ignore')
# Does the tweet author follow the user defined above?
status = api.show_friendship(source_id=userid, target_id=auserid)
# If he does print
if status[0].following:
print username + " folgt " + auser + "(" + str(userfollower) + "Followers)"
# Block the user
if api.create_block(id = userid):
print " " + username + " blocked successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment