-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfollowers.py
83 lines (58 loc) · 2.16 KB
/
unfollowers.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
from time import sleep
from os import path
from sys import exit
import random
import ast
try:
from igramscraper.instagram import Instagram
except ImportError:
print("Please run 'pip install -r requirements.txt' before running this script")
exit()
# bot account username
bot_username = 'FILL THIS OUT'
# bot account password
bot_password = 'FILL THIS OUT'
# username of account to track
username = 'FILL THIS OUT'
def get_unfollowers(current, old):
''' returns a list of unfollowers '''
return list(set(old) - set(current))
def check():
''' runs unfollower statistic check '''
try:
print("Running check...")
# instantiation
instagram = Instagram()
instagram.with_credentials(bot_username, bot_password)
instagram.login(force=False, two_step_verificator=True)
sleep(random.randint(1,2))
# get current followers
followers = []
account = instagram.get_account(username)
sleep(random.randint(1,2))
followers = instagram.get_followers(account.identifier, 10**6, 100, delayed=True) # get 100 followers per request at delayed intervals
curr = []
for follower in followers['accounts']:
curr.append(follower.username)
# update followers list
if path.exists("followers.txt"):
with open("followers.txt", "r+") as f:
old = f.read()
f.seek(0)
f.write(str(curr))
f.truncate()
old = ast.literal_eval(str(old))
# calculate followers
unfollowers = get_unfollowers(curr, old)
print('-----------------------')
print(f'Follower Count: {len(curr)}')
print(f'Unfollow Count: {len(unfollowers)}\nUnfollowers: {str(unfollowers)[1:-1]}')
print('-----------------------')
else:
print("This is your first time running. 'followers.txt' generated.")
with open("followers.txt", "w") as f:
f.write(str(curr))
except KeyboardInterrupt: # hides error messages on keyboard interrupt
exit(0)
if __name__ == "__main__":
check()