Created
May 13, 2019 23:14
-
-
Save rezkam/9bdcd1ad7831b565fb75b7863a1d6498 to your computer and use it in GitHub Desktop.
Remove likes (favorite) from your tweets using twitter API (Working with Python 3.5+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Remove likes (favorite) from your tweets using twitter API | |
you need to install twitter-client for python to use this code: | |
pip install python-twitter ( more info https://github.com/bear/python-twitter) | |
to use this you need to generate authntication tokens for your account | |
find more info on (https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens) | |
""" | |
import asyncio | |
import concurrent.futures | |
import twitter | |
NUMBER_OF_TWEETS = 2000 | |
WORKERS = 10 | |
twitter_api_client = twitter.Api( | |
consumer_key='your_app_consumer_key', | |
consumer_secret='your_app_consumer_secret', | |
access_token_key='your_access_token_key', | |
access_token_secret='your_access_token_secret' | |
) | |
def remove_tweet(tweet_id): | |
""" | |
destroy favorite using twitter API | |
:param tweet_id: status_id of tweet (string) | |
:return: None | |
""" | |
try: | |
twitter_api_client.DestroyFavorite(status_id=tweet_id) | |
print('removed fav from %s' % tweet_id) | |
except twitter.TwitterError as e: | |
print('problem with removing Fav from %s' % tweet_id) | |
print(e) | |
# Get list of favorited(liked) tweets | |
liked_tweets = twitter_api_client.GetFavorites(count=NUMBER_OF_TWEETS) | |
print('liked tweets count: %s' % len(liked_tweets)) | |
async def main(): | |
loop = asyncio.get_event_loop() | |
with concurrent.futures.ThreadPoolExecutor( | |
max_workers=WORKERS) as executor: | |
futures = [ | |
loop.run_in_executor( | |
executor, | |
remove_tweet, | |
tweet.id | |
) | |
for tweet in liked_tweets | |
] | |
await asyncio.gather(*futures) | |
event_loop = asyncio.get_event_loop() | |
event_loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment