-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (83 loc) · 3.55 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
import argparse
from instagrapi import Client
from instagrapi.exceptions import LoginRequired
import sys
import os
import time
import random
if len(sys.argv) != 7:
print("Usage: python main.py -u <username> -p <password> -f <path to the json file>")
sys.exit()
parser = argparse.ArgumentParser(description='Script description')
parser.add_argument('-u', '--username', help='username')
parser.add_argument('-p', '--password', help='password')
parser.add_argument('-f', '--file', help='Path to the json file.')
args = parser.parse_args()
api = Client()
def LoadOldData():
AlreadyUnfollowed = []
if os.path.exists("done.txt"):
with open('done.txt', 'r', encoding='utf-8') as f:
data = f.readlines()
for username in data:
AlreadyUnfollowed.append(username[:-1])
return AlreadyUnfollowed
def Login(cl: Client, USERNAME: str, PASSWORD: str):
session = cl.load_settings("session.json") if os.path.exists("session.json") else None
login_via_session = False
login_via_pw = False
if session:
try:
cl.set_settings(session)
cl.login(USERNAME, PASSWORD)
try:
cl.get_timeline_feed()
except LoginRequired:
print(f"[+]-> Session is invalid, need to login via username and password")
old_session = cl.get_settings()
cl.set_settings({})
cl.set_uuids(old_session["uuids"])
cl.login(USERNAME, PASSWORD)
login_via_session = True
except Exception as e:
print(f"[+]-> Couldn't login user using session information: %s" % e)
if not login_via_session:
try:
print(f"[+]-> Attempting to login via username and password. username: %s" % USERNAME)
if cl.login(USERNAME, PASSWORD):
login_via_pw = True
except Exception as e:
print(f"[+]-> Couldn't login user using username and password: %s" % e)
if not login_via_pw and not login_via_session:
print(f"[+]-> Couldn't login user with either password or session")
sys.exit()
def Unfollow(username):
UserID = api.user_info_by_username_v1(username).pk
if api.user_unfollow(UserID):
with open('done.txt', 'a', encoding='utf-8') as f:
f.write(username + "\n")
print(f"[+]-> {username} unfollowed successfully.")
else:
print(f"[+]-> an error has occurred, with the user {username}")
def ExtractUsernames(filepath):
OldData = LoadOldData()
with open(filepath, 'r') as f:
data = json.loads(f.read())
# i can use the below code but in case the key's names were different it wont work:
# for line in data["relationships_follow_requests_sent"]:
# username = line["string_list_data"][0]["value"]
# thats why i used method below:
for line in data[next(iter(data))]:
username = line[list(line)[2]][0][list(line[list(line)[2]][0])[1]]
if username not in OldData:
Unfollow(username)
# it is recommended to keep the delay time at 10 seconds to avoid being blacklisted by instagram for automation
RandomSleep = random.randint(15,20)
time.sleep(RandomSleep)
else:
print(f"[+]-> {username} already unfollowed")
if __name__ == "__main__":
Login(api, args.username, args.password)
api.dump_settings("session.json")
ExtractUsernames(args.file)