Skip to content

Commit be9ac04

Browse files
committed
Implement downloads and comparison
1 parent 25a57fd commit be9ac04

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CLIENT_SECRET.*
33
credentials.*
44
service_account.*
55
session.json
6+
data*
67

78
# Instagram Specific Text Files
89
following_me_only.txt

check.py

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from instagrapi import Client
22
from instagrapi.exceptions import LoginRequired
33
from credentials import *
4+
import json
5+
from datetime import datetime, timedelta
6+
import os
7+
import gspread
48

59

610

@@ -12,7 +16,7 @@ def login_user():
1216

1317
cl = Client()
1418
# cl.handle_exception = handle_exception
15-
cl.delay_range = [1, 3]
19+
cl.delay_range = [0.5, 1.5]
1620

1721

1822
session = cl.load_settings("session.json")
@@ -55,6 +59,116 @@ def login_user():
5559
return cl
5660

5761

62+
def download_list(cl):
63+
# Get followers and following, as list of user ids
64+
followers = list(cl.user_followers(SCRAPE_ID).keys())
65+
following = list(cl.user_following(SCRAPE_ID).keys())
66+
67+
# check the lists are not null, if they are retry
68+
if not followers or not following:
69+
print("FAIL - Retrying 1/1 to get followers and following")
70+
followers = list(cl.user_followers(SCRAPE_ID).keys())
71+
following = list(cl.user_following(SCRAPE_ID).keys())
72+
73+
if not followers or not following:
74+
raise Exception("Couldn't get followers and following")
75+
76+
# Change list of IDs to int type
77+
followers = list(map(int, followers))
78+
following = list(map(int, following))
79+
80+
print("Writing data to json")
81+
82+
with open(f"data/{today_date}.json", "w") as f:
83+
json.dump({"followers": followers, "following": following}, f)
84+
85+
print("Data written to json")
86+
return followers, following
87+
88+
89+
def read_data(read_date):
90+
"""
91+
Returns followers and following from the json file with the given date
92+
"""
93+
if os.path.exists(f"data/{read_date}.json"):
94+
with open(f"data/{read_date}.json", "r") as f:
95+
data = json.load(f)
96+
97+
followers = data["followers"]
98+
following = data["following"]
99+
else:
100+
followers = []
101+
following = []
102+
103+
return followers, following
104+
105+
106+
def get_dates():
107+
global today_date, yesterday_date
108+
109+
now = datetime.now()
110+
# init_time = now.strftime("%H:%M:%S") # Date in format HH:MM:SS
111+
# init_time_with_day = now.strftime("%Y-%m-%d %H:%M:%S") # Date in format YYYY-MM-DD HH:MM:SS
112+
today_date = now.strftime("%Y-%m-%d") # Date in format YYYY-MM-DD
113+
yesterday_date = (now - timedelta(days=1)).strftime("%Y-%m-%d") # Date in format YYYY-MM-DD
114+
115+
116+
def compare_data(cl, followers, following):
117+
"Read yesterdays data and compare to todays passed data"
118+
old_followers, old_following = read_data(yesterday_date)
119+
120+
# Compare followers
121+
new_followers = list(set(followers) - set(old_followers))
122+
nolonger_followers = list(set(old_followers) - set(followers))
123+
new_following = list(set(following) - set(old_following))
124+
nolonger_following = list(set(old_following) - set(following))
125+
126+
# For change list, get the usernames
127+
new_followers = [cl.user_info(user_id).username for user_id in new_followers]
128+
nolonger_followers = [cl.user_info(user_id).username for user_id in nolonger_followers]
129+
new_following = [cl.user_info(user_id).username for user_id in new_following]
130+
nolonger_following = [cl.user_info(user_id).username for user_id in nolonger_following]
131+
132+
# Print the changes
133+
print(f"New Followers: {new_followers}")
134+
print(f"No Longer Followers: {nolonger_followers}")
135+
print(f"New Following: {new_following}")
136+
print(f"No Longer Following: {nolonger_following}")
137+
138+
return new_followers, nolonger_followers, new_following, nolonger_following
139+
140+
def write_to_spreadsheet(new_followers, nolonger_followers, new_following, nolonger_following):
141+
142+
gc = gspread.service_account(filename=service_account_path)
143+
sh = gc.open_by_key(sheet_key)
144+
worksheet = sh.get_worksheet(0)
145+
146+
147+
print("Data written to Google Sheets")
148+
149+
150+
def main():
151+
# Login and download follower/following data
152+
cl = login_user()
153+
followers, following = download_list(cl)
154+
155+
# Compare data
156+
compare_data(cl, followers, following)
157+
58158

59159
if __name__ == "__main__":
60160
cl = login_user()
161+
get_dates()
162+
163+
followers, following = read_data(today_date)
164+
165+
new_followers, nolonger_followers, new_following, nolonger_following = compare_data(cl, followers, following)
166+
167+
168+
169+
# cl = login_user()
170+
# download_list(cl)
171+
172+
173+
174+

0 commit comments

Comments
 (0)