-
-
Save stefanruijsenaars/cf94eb3bc1cdc7fdde954052e30ed75a to your computer and use it in GitHub Desktop.
Follow Friday
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
# -*- coding: utf-8 -*- | |
import json | |
import time | |
import logging | |
from collections import Counter | |
from TwitterAPI import TwitterAPI | |
# Forked from: https://gist.github.com/elaineo/b9d73ce88ac418d4fed8128231bc15e7 | |
# Go to https://apps.twitter.com/ to get your v2 API keys. | |
API_KEY = '' | |
API_KEY_SECRET = '' | |
# Look up your numeric ID at: https://codeofaninja.com/tools/find-twitter-id/ | |
MY_TWITTER_ID = '' | |
# Current year, minus 1. Stop scraping likes here. | |
LAST_YEAR = '2020' | |
def main(): | |
logging.basicConfig(level=logging.INFO) | |
api = TwitterAPI(API_KEY, API_KEY_SECRET, auth_type='oAuth2', api_version='2') | |
counter = Counter([]) | |
pagination_token = None | |
while True: | |
tweets = api.request(f'users/:{MY_TWITTER_ID}/liked_tweets', | |
{'expansions': 'author_id', 'tweet.fields': 'created_at', 'pagination_token': pagination_token}) | |
print(tweets) | |
if all(tweet.get('created_at')[0:4] == '2020' for tweet in tweets): | |
break | |
for t in tweets: | |
counter[t.get('author_id')] += 1 | |
print(counter) | |
pagination_token = tweets.json()['meta']['next_token'] | |
# Stick to rate limit imposed by Twitter | |
time.sleep(20) | |
top = [t[0] for t in counter.most_common(50)] | |
friends = api.request("users", {'ids': ','.join(top)}) | |
for friend in friends: | |
print(f"@{friend.get('username')} ({counter[friend.get('id')]})") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment