-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_friends.py
222 lines (179 loc) · 8 KB
/
common_friends.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import requests
import json
from collections import deque
################################################################################
# 1) API CONFIGURATION
################################################################################
API_HOST = "instagram-scraper-api2.p.rapidapi.com"
API_KEY = "0acf61faf2msh0b14d26175218ffp167e2cjsn3492bbf87bc2"
HEADERS = {
"x-rapidapi-host": API_HOST,
"x-rapidapi-key": API_KEY
}
################################################################################
# 2) PAGINATED REQUESTS - FETCH ALL FOLLOWERS
################################################################################
def fetch_followers(user):
"""
Returns *all* followers for `user`, handling pagination in batches of up to 1000.
"""
# First request: get the total follower count with amount=1
url = f"https://{API_HOST}/v1/followers?username_or_id_or_url={user}&amount=1"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
total_followers = data["data"]["count"] # total number of followers
all_followers = []
pagination_token = ""
remaining = total_followers
while remaining > 0:
batch_size = min(remaining, 1000)
if pagination_token:
url = (
f"https://{API_HOST}/v1/followers?username_or_id_or_url={user}"
f"&amount={batch_size}&pagination_token={pagination_token}"
)
else:
url = f"https://{API_HOST}/v1/followers?username_or_id_or_url={user}&amount={batch_size}"
resp = requests.get(url, headers=HEADERS)
resp.raise_for_status()
resp_data = resp.json()
data_obj = resp_data.get("data", {})
items_list = data_obj.get("items", [])
# Add followers
for item in items_list:
username = item.get("username")
if username:
all_followers.append(username)
# Update pagination token
pagination_token = resp_data.get("pagination_token", "")
remaining -= batch_size
# If no more pagination token is provided and we've requested less than 1000, we're done
if not pagination_token and batch_size < 1000:
break
return all_followers
################################################################################
# 3) PAGINATED REQUESTS - FETCH ALL FOLLOWINGS
################################################################################
def fetch_followings(user):
"""
Returns *all* accounts that `user` follows, handling pagination in batches of up to 1000.
"""
# First request: get the total 'followed' count with amount=1
url = f"https://{API_HOST}/v1/following?username_or_id_or_url={user}&amount=1"
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
total_followings = data["data"]["count"] # total number of followings
all_followings = []
pagination_token = ""
remaining = total_followings
while remaining > 0:
batch_size = min(remaining, 1000)
if pagination_token:
url = (
f"https://{API_HOST}/v1/following?username_or_id_or_url={user}"
f"&amount={batch_size}&pagination_token={pagination_token}"
)
else:
url = f"https://{API_HOST}/v1/following?username_or_id_or_url={user}&amount={batch_size}"
resp = requests.get(url, headers=HEADERS)
resp.raise_for_status()
resp_data = resp.json()
data_obj = resp_data.get("data", {})
items_list = data_obj.get("items", [])
# Add followers
for item in items_list:
username = item.get("username")
if username:
all_followings.append(username)
# Update pagination token
pagination_token = resp_data.get("pagination_token", "")
remaining -= batch_size
if not pagination_token and batch_size < 1000:
break
return all_followings
################################################################################
# 4) DETERMINE MUTUAL FRIENDS
################################################################################
def get_mutual_friends(user):
"""
Returns a set of users who both follow `user` AND are followed by `user`.
"""
followers = set(fetch_followers(user))
followings = set(fetch_followings(user))
return followers.intersection(followings)
################################################################################
# 5) BFS TO FIND SHORTEST "MUTUAL-FRIEND" DISTANCE
################################################################################
def mutual_connection_distance(user1, user2, max_depth):
"""
Returns the minimum number of 'mutual-friend hops' between user1 and user2.
- 0 if user1 == user2
- 1 if user2 is a direct mutual friend of user1
- etc.
- The path is also returned as a list of usernames, displayed by joining the elements with ' -> '.
If no connection is found within `max_depth`, returns None.
"""
if user1 == user2:
return 0
visited = set([user1])
queue = deque([(user1, 0)]) # (current_user, distance)
predecessors = {user1: None}
while queue:
current_user, distance = queue.popleft()
# If we've reached the max search depth, don't go further
if distance >= max_depth:
continue
# Get current user's mutual friends
current_mutuals = get_mutual_friends(current_user)
if user2 in current_mutuals:
path = [user2]
while current_user is not None:
path.append(current_user)
current_user = predecessors[current_user]
return distance + 1, path[::-1] # Return distance and path
# Enqueue each mutual friend for further exploration
for mf in current_mutuals:
if mf not in visited:
visited.add(mf)
queue.append((mf, distance + 1))
predecessors[mf] = current_user
# No connection found up to max_depth
return None, None
################################################################################
# 6) EXAMPLE USAGE
################################################################################
if __name__ == "__main__":
# Replace these with actual usernames you want to test
user1 = "leslieramirez21311"
user2 = "liu_kevin5"
distance = mutual_connection_distance(user1, user2, max_depth=3)
if distance is None:
print(f"No mutual-friend connection found between {user1} and {user2} (depth <= 3).")
elif distance == 0:
for users in distance:
print(f"{user1} and {user2} are the same user.")
else:
print(f"{user1} is {distance[0]} mutual-friend hop(s) away from {user2}. \n Path: {' -> '.join(distance[1])}")
################################################################################
# 7) JSON WITH MUTUAL FRIEND DATA
################################################################################
trimmed_distance = distance[1][1:-1] # Remove the first and last elements (user1 and user2)
total_user_data = {}
for user in trimmed_distance:
url = f"https://{API_HOST}/v1/info?username_or_id_or_url={user}&url_embed_safe=true" # Gets data for each user
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
n = 1
total_user_data[f"connection{n}"] = {
"username": data["data"].get("username", ""),
"full_name": data["data"].get("full_name", ""),
"profile_pic_url": data["data"].get("hd_profile_pic_url", ""),
"bio": data["data"].get("biography", ""),
} # Adds user data to the JSON file
n =+ 1
with open("mutual_friends.json", "w") as f:
json.dump(total_user_data, f, indent=4)
print("JSON file created successfully!")