Skip to content

Instantly share code, notes, and snippets.

@anthonyalayo
Last active October 5, 2021 05:48
Show Gist options
  • Save anthonyalayo/ba76b12cd2335b829ae0a08536a3ac76 to your computer and use it in GitHub Desktop.
Save anthonyalayo/ba76b12cd2335b829ae0a08536a3ac76 to your computer and use it in GitHub Desktop.
Instagram Script For Funsies
import csv
from context import Instagram # pylint: disable=no-name-in-module
from time import sleep
# login strings
my_user = 'changeme'
my_pass = 'changeme'
# log in
print('Logging in...')
instagram = Instagram()
instagram.with_credentials(my_user, my_pass, '.') # dot means cache files can just go here
instagram.login(force=False,two_step_verificator=True)
sleep(2) # rate limit
# get my account
print('Getting account...')
my_account = instagram.get_account(my_user)
print('My account: {}'.format(my_account))
sleep(2) # rate limit
# get X people I follow
# number must be >= page size for instagram API
num_following = 4 # change me
following_page_size = 4 # change me
print('Getting who I follow...')
users_im_following = instagram.get_following(my_account.identifier, num_following, following_page_size, delayed=True)
sleep(2) # rate limit
print('Getting the followers of who I follow...')
# number must be >= page size for instagram API
num_followers = 50 # change me
followers_page_size = 50 # change me
# loop over the users im following
for user_im_following in users_im_following['accounts']:
print('Getting followers for user: {}'.format(user_im_following.full_name))
sleep(5) # rate limit
# for each user i'm following, get me their followers
their_followers = instagram.get_followers(user_im_following.identifier, num_followers, followers_page_size, delayed=True)
# now write their followers to a file
# make the filename their username
filename = '{}.csv'.format(user_im_following.username)
print('Writing followers to {}'.format(filename))
# https://stackoverflow.com/questions/20717095/python-csv-overwrite
with open(filename, 'w+') as file:
writer = csv.writer(file, delimiter=",")
# write the headers
writer.writerow(['id','name'])
# for each of their followers, write them out row by row
for their_follower in their_followers['accounts']:
writer.writerow([their_follower.identifier,their_follower.full_name])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment