Skip to content

Instantly share code, notes, and snippets.

@Alexhuszagh
Created November 14, 2022 22:03
Show Gist options
  • Save Alexhuszagh/3c9f1001ec1c6ef930fe764962f74c8e to your computer and use it in GitHub Desktop.
Save Alexhuszagh/3c9f1001ec1c6ef930fe764962f74c8e to your computer and use it in GitHub Desktop.
Utility to export who you follow/follows you on Twitter.
#!/usr/bin/env python
'''
export_friends
==============
Export friend information from Twitter. Note that
the API JSON document should look like:
{
"consumer_key": "...",
"consumer_secret": "...",
"access_token": "...",
"access_token_secret": "..."
}
Sample Usage:
./export_friends.py \
--friends \
--api api.json \
--user kardonice \
--output kardonice.csv \
--verbose
Requirements:
Python 3.5+
tweepy==3.10.0
'''
__version__ = '0.0.0-dev'
__author__ = 'Alex Huszagh <ahuszagh@gmail.com>'
__license__ = 'Unlicense (Public Domain)'
import argparse
import csv
import json
import tweepy
def print_verbose(message, verbose=True):
if verbose:
print(message)
def generate_api(path):
api_data = json.load(open(path))
consumer_key = api_data['consumer_key']
consumer_secret = api_data['consumer_secret']
access_token = api_data['access_token']
access_token_secret = api_data['access_token_secret']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return tweepy.API(
auth,
timeout=5,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
compression=True
)
def get_user(api, screen_name):
return api.lookup_users(screen_names=[screen_name])[0]
def get_followers(api, user, next_cursor=-1, verbose=True):
cursor = tweepy.Cursor(
api.followers,
user_id=user.id,
screen_name=None,
cursor=next_cursor,
)
for page in cursor.pages():
print_verbose(f'Current cursor at {cursor.iterator.next_cursor}', verbose)
yield from page
def get_friends(api, user, next_cursor=-1, verbose=True):
cursor = tweepy.Cursor(
api.friends,
user_id=user.id,
screen_name=None,
cursor=next_cursor,
)
for page in cursor.pages():
print_verbose(f'Current cursor at {cursor.iterator.next_cursor}', verbose)
yield from page
def write_users(path, iterable, verbose=True):
with open(path, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=csv_fields, dialect='excel-tab')
writer.writeheader()
for index, item in enumerate(iterable):
print_verbose(f'Writing user {item.screen_name}', verbose)
data = {k: item._json.get(k) for k in csv_fields}
writer.writerow(data)
if index % 50 == 0:
file.flush()
csv_fields = [
'id_str',
'name',
'screen_name',
'location',
'profile_location',
'description',
'url',
'protected',
'followers_count',
'friends_count',
'created_at',
'utc_offset',
'time_zone',
'geo_enabled',
'verified',
'statuses_count',
'lang',
'contributors_enabled',
'is_translator',
'is_translation_enabled',
'profile_image_url_https',
'profile_banner_url',
'has_extended_profile',
'default_profile',
'default_profile_image',
'following',
'follow_request_sent',
'notifications',
'suspended',
'needs_phone_verification',
]
def main():
parser = argparse.ArgumentParser()
action_group = parser.add_mutually_exclusive_group(required=True)
action_group.add_argument(
'--friends',
help='Get list of all friends (accounts you follow)',
action='store_true',
)
action_group.add_argument(
'--followers',
help='Get list of all followers (accounts that follow you)',
action='store_true',
)
parser.add_argument(
'-a',
'--api',
help='JSON document with the API credentials.',
default='api.json',
)
parser.add_argument(
'-u',
'--user',
help='Screen name of user to get friends of.',
required=True,
)
parser.add_argument(
'-o',
'--output',
help='Output CSV file name.',
)
parser.add_argument(
'-V',
'--version',
action='version',
version=f'%(prog)s {__version__}'
)
parser.add_argument(
'-v',
'--verbose',
action='store_true',
)
args = parser.parse_args()
if args.output:
output = args.output
elif args.friends:
output = f'{args.user}_friends.csv'
else:
output = f'{args.user}_following.csv'
api = generate_api(args.api)
user = get_user(api, args.user)
if args.friends:
iterable = get_friends(api, user, verbose=args.verbose)
else:
iterable = get_followers(api, user, verbose=args.verbose)
write_users(output, iterable, verbose=args.verbose)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment