# --- The MIT License (MIT) Copyright (c) <NAME>, Sun Oct 6 01:48am 2021 ---
# author: <NAME> (<EMAIL>)
# v0.1.2

from instagram_private_api import Client
import json
import sys
'''
Please put your username and password in a json formatted file likes below:
{
  "user_name": "your_ig_login_email_address",
  "password": "<PASSWORD>"
}
'''
with open('./.apikeys/your_ig_user_password.json', 'r', encoding='utf-8') as f:
    your_ig_user_password = json.load(f)
user_name = your_ig_user_password['<PASSWORD>']
password = <PASSWORD>_ig_<PASSWORD>_password['password']

try:
    api = Client(user_name, password)
except Exception as e:
    print('發生錯誤: %s！' % e)
    sys.exit(1)

user_id = api.authenticated_user_id
rank_token = Client.generate_uuid()

my_ig_following_lists = []
next_max_id = True

while next_max_id:
    results = api.user_following(user_id, rank_token, max_id=next_max_id)
    my_ig_following_lists.extend(results.get('users', []))
    next_max_id = results.get('next_max_id', None)

if not my_ig_following_lists:
    print('沒有資料！')
    sys.exit(1)
my_ig_following_lists.sort(key=lambda user: user['username'])
with open('my_ig_following_lists.json', 'w', encoding='utf-8-sig') as f:
    json.dump(my_ig_following_lists, f, ensure_ascii=False, indent=4)
print('完成資料存檔！')
# --- The MIT License (MIT) Copyright (c) <NAME>, Sun Oct 6 01:48am 2021 ---
# author: <NAME> (<EMAIL>)
# v0.1.2

from instagram_private_api import Client
import json
import sys
'''
Please put your username and password in a json formatted file likes below:
{
  "user_name": "your_ig_login_email_address",
  "password": "<PASSWORD>"
}
'''
with open('./.apikeys/your_ig_user_password.json', 'r', encoding='utf-8') as f:
    your_ig_user_password = json.load(f)
user_name = your_ig_user_password['<PASSWORD>']
password = <PASSWORD>_ig_<PASSWORD>_password['password']

try:
    api = Client(user_name, password)
except Exception as e:
    print('發生錯誤: %s！' % e)
    sys.exit(1)

user_id = api.authenticated_user_id
rank_token = Client.generate_uuid()

my_ig_following_lists = []
next_max_id = True

while next_max_id:
    results = api.user_following(user_id, rank_token, max_id=next_max_id)
    my_ig_following_lists.extend(results.get('users', []))
    next_max_id = results.get('next_max_id', None)

if not my_ig_following_lists:
    print('沒有資料！')
    sys.exit(1)
my_ig_following_lists.sort(key=lambda user: user['username'])
with open('my_ig_following_lists.json', 'w', encoding='utf-8-sig') as f:
    json.dump(my_ig_following_lists, f, ensure_ascii=False, indent=4)
print('完成資料存檔！')
# --- The MIT License (MIT) Copyright (c) <NAME>, Sun Oct 6 01:48am 2021 --- # author: <NAME> (<EMAIL>) # v0.1.2 Please put your username and password in a json formatted file likes below:
 {
   "user_name": "your_ig_login_email_address",
   "password": "<PASSWORD>"
 }