11from instagrapi import Client
2- from instagrapi .exceptions import LoginRequired
2+ from instagrapi .exceptions import LoginRequired , UserNotFound
33from credentials import *
44import json
55from datetime import datetime , timedelta
66import os
77import gspread
88
9+ def get_username (user_id ):
10+ """returns username from user_id, with error handling"""
11+ try :
12+ name = cl .user_info (user_id ).username
13+ except UserNotFound as e :
14+ name = str (user_id ) + " err"
15+ return name
916
1017
1118def login_user ():
@@ -104,11 +111,11 @@ def read_data(read_date):
104111
105112
106113def get_dates ():
107- global today_date , yesterday_date
114+ global timeday , today_date , yesterday_date
108115
109116 now = datetime .now ()
110117 # 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
118+ timeday = now .strftime ("%Y-%m-%d %H:%M:%S" ) # Date in format YYYY-MM-DD HH:MM:SS
112119 today_date = now .strftime ("%Y-%m-%d" ) # Date in format YYYY-MM-DD
113120 yesterday_date = (now - timedelta (days = 1 )).strftime ("%Y-%m-%d" ) # Date in format YYYY-MM-DD
114121
@@ -124,25 +131,41 @@ def compare_data(cl, followers, following):
124131 nolonger_following = list (set (old_following ) - set (following ))
125132
126133 # 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 ]
134+ new_followers = [get_username (user_id ) for user_id in new_followers ]
135+ nolonger_followers = [get_username (user_id ) for user_id in nolonger_followers ]
136+ new_following = [get_username (user_id ) for user_id in new_following ]
137+ nolonger_following = [get_username (user_id ) for user_id in nolonger_following ]
138+
131139
132140 # Print the changes
133141 print (f"New Followers: { new_followers } " )
134142 print (f"No Longer Followers: { nolonger_followers } " )
135143 print (f"New Following: { new_following } " )
136144 print (f"No Longer Following: { nolonger_following } " )
137145
138- return new_followers , nolonger_followers , new_following , nolonger_following
146+ return ( new_followers , nolonger_followers , new_following , nolonger_following )
139147
140- def write_to_spreadsheet (new_followers , nolonger_followers , new_following , nolonger_following ):
141148
142- gc = gspread .service_account (filename = service_account_path )
143- sh = gc .open_by_key (sheet_key )
144- worksheet = sh .get_worksheet (0 )
149+ def get_profile_info (cl ):
150+ return cl .user_info_by_username ('jayden.kah' ).model_dump ()
145151
152+ def write_to_spreadsheet (follow_change , profile ):
153+ new_followers , nolonger_followers , new_following , nolonger_following = follow_change
154+
155+ # Authenticate, open and select worksheet
156+ gc = gspread .service_account (filename = service_account_path )
157+ spreadsheet = gc .open_by_key (sheet_key )
158+ worksheet = spreadsheet .worksheet (SCRAPE_USERNAME )
159+
160+ # Construct row to write
161+ row = [
162+ timeday , profile ["username" ], profile ["full_name" ], profile ["biography" ],
163+ profile ["media_count" ], profile ["is_private" ], profile ["follower_count" ],
164+ profile ["following_count" ], len (new_followers ), ", " .join (new_followers ),
165+ len (nolonger_followers ), ", " .join (nolonger_followers ), len (new_following ),
166+ ", " .join (new_following ), len (nolonger_following ), ", " .join (nolonger_following )]
167+
168+ worksheet .append_row (row , value_input_option = "USER_ENTERED" , insert_data_option = "INSERT_ROWS" )
146169
147170 print ("Data written to Google Sheets" )
148171
@@ -162,7 +185,12 @@ def main():
162185
163186 followers , following = read_data (today_date )
164187
165- new_followers , nolonger_followers , new_following , nolonger_following = compare_data (cl , followers , following )
188+ follow_change = compare_data (cl , followers , following )
189+ profile = get_profile_info (cl )
190+
191+ write_to_spreadsheet (follow_change , profile )
192+
193+
166194
167195
168196
0 commit comments