#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import time
import random
import requests
import json

instagram_url = 'https://www.instagram.com'
login_route = '%s/accounts/login/ajax/' % (instagram_url)
logout_route = '%s/accounts/logout/' % (instagram_url)
query_route = '%s/graphql/query/' % (instagram_url)
unfollow_route = '%s/web/friendships/%s/unfollow/'

session = requests.Session()

def login():
    session.headers.update({
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'en-US,en;q=0.8',
        'Connection': 'keep-alive',
        'Content-Length': '0',
        'Host': 'www.instagram.com',
        'Origin': 'https://www.instagram.com',
        'Referer': 'https://www.instagram.com/',
        'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 \
            (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36'),
        'X-Instagram-AJAX': '1',
        'X-Requested-With': 'XMLHttpRequest'
    })
    session.cookies.update({
        'ig_pr': '1',
        'ig_vw': '1920',
    })

    reponse = session.get(instagram_url)
    session.headers.update({
        'X-CSRFToken': reponse.cookies['csrftoken']
    })

    time.sleep(random.randint(2, 6))

    post_data = {
        'username': os.environ.get('USERNAME'),
        'password': os.environ.get('PASSWORD')
    }

    reponse = session.post(login_route, data=post_data, allow_redirects=True)
    reponse_data = json.loads(reponse.text)

    if reponse_data['authenticated']:
        session.headers.update({
            'X-CSRFToken': reponse.cookies['csrftoken']
        })

    return reponse_data['authenticated']

# Not so useful, it's just to simulate human actions better
def get_user_profile(username):
    response = session.get('https://www.instagram.com/%s/?__a=1' % (username))
    response = json.loads(response.text)

    return response['user']

def get_follows_list():
    follows_list = []

    follows_post = {
        'query_id': 17874545323001329,
        'variables': {
            'id': session.cookies['ds_user_id'],
            'first': 20
        }
    }
    follows_post['variables'] = json.dumps(follows_post['variables'])
    response = session.post(query_route, data=follows_post)
    response = json.loads(response.text)

    for edge in response['data']['user']['edge_follow']['edges']:
        follows_list.append(edge['node'])

    while response['data']['user']['edge_follow']['page_info']['has_next_page']:
        time.sleep(random.randint(1, 4))

        follows_post = {
            'query_id': 17874545323001329,
            'variables': {
                'id': session.cookies['ds_user_id'],
                'first': 10,
                'after': response['data']['user']['edge_follow']['page_info']['end_cursor']
            }
        }
        follows_post['variables'] = json.dumps(follows_post['variables'])
        response = session.post(query_route, data=follows_post)
        response = json.loads(response.text)

        for edge in response['data']['user']['edge_follow']['edges']:
            follows_list.append(edge['node'])

    return follows_list

def get_followers_list():
    followers_list = []

    followers_post = {
        'query_id': 17851374694183129,
        'variables': {
            'id': session.cookies['ds_user_id'],
            'first': 20
        }
    }
    followers_post['variables'] = json.dumps(followers_post['variables'])
    response = session.post(query_route, data=followers_post)
    response = json.loads(response.text)

    for edge in response['data']['user']['edge_followed_by']['edges']:
        followers_list.append(edge['node'])

    while response['data']['user']['edge_followed_by']['page_info']['has_next_page']:
        time.sleep(random.randint(1, 4))

        followers_post = {
            'query_id': 17851374694183129,
            'variables': {
                'id': session.cookies['ds_user_id'],
                'first': 10,
                'after': response['data']['user']['edge_followed_by']['page_info']['end_cursor']
            }
        }
        followers_post['variables'] = json.dumps(followers_post['variables'])
        response = session.post(query_route, data=followers_post)
        response = json.loads(response.text)

        for edge in response['data']['user']['edge_followed_by']['edges']:
            followers_list.append(edge['node'])

    return followers_list

def unfollow(user):
    print('unfollowing {}'.format(user['username']))

    response = session.post(unfollow_route % (instagram_url, user['id']))
    response = json.loads(response.text)

    if response['status'] != 'ok':
        print('ERROR: {}'.format(unfollow.text))
        sys.exit('might be unfollowing too fast, quit to prevent ban...')

def logout():
    post_data = {
        'csrfmiddlewaretoken': session.cookies['csrftoken']
    }

    logout = session.post(logout_route, data=post_data)

    if logout.status_code == 200:
        return True
    return False


def main():
    if not os.environ.get('USERNAME') or not os.environ.get('PASSWORD'):
        sys.exit('please provide USERNAME and PASSWORD environement variables. Abording...')

    is_logged = login()
    if is_logged == False:
        sys.exit('login failed, verify user/password combination')

    print('You\'re now logged as {}'.format(os.environ.get('USERNAME')))

    get_user_profile(os.environ.get('USERNAME'))

    time.sleep(random.randint(2, 6))

    followers_list = get_followers_list()
    print('found {} followers'.format(len(followers_list)))

    follows_list = get_follows_list()
    print('found {} following'.format(len(follows_list)))

    unfollow_users_list = [user for user in follows_list if user not in followers_list]
    print('you are following {} user(s) who aren\'t following you.'.format(len(unfollow_users_list)))

    if len(unfollow_users_list) > 0:
        print('Begin to unfollow users...')

        for user in unfollow_users_list:
            unfollow(user)

    is_logged_out = logout()
    if is_logged_out:
        sys.exit(0)


if __name__ == "__main__":
    main()

@@ -4448,62 +4448,8 @@
 r):%0A
-    print('unfollowing %7B%7D'.format(user%5B'username'%5D))%0A%0A
     
@@ -5922,16 +5922,77 @@
 s_list:%0A
+            print('unfollowing %7B%7D'.format(user%5B'username'%5D))%0A
         

Move print outside of a func