Update utils/instaloader_utils.py
Browse files- utils/instaloader_utils.py +27 -4
utils/instaloader_utils.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
-
# api/utils/instaloader_utils.py
|
2 |
import instaloader
|
3 |
from typing import List, Dict
|
4 |
-
import
|
5 |
|
6 |
# Initialize Instaloader
|
7 |
L = instaloader.Instaloader()
|
@@ -9,10 +8,20 @@ L = instaloader.Instaloader()
|
|
9 |
def fetch_user_posts(username: str, max_posts: int = 50) -> List[Dict]:
|
10 |
"""
|
11 |
Fetch posts from a given Instagram profile (public data only).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
"""
|
13 |
try:
|
|
|
14 |
profile = instaloader.Profile.from_username(L.context, username)
|
15 |
posts = []
|
|
|
|
|
16 |
for post in profile.get_posts():
|
17 |
posts.append({
|
18 |
"username": username,
|
@@ -23,13 +32,27 @@ def fetch_user_posts(username: str, max_posts: int = 50) -> List[Dict]:
|
|
23 |
"date": post.date_utc.isoformat(),
|
24 |
"image_url": post.url
|
25 |
})
|
26 |
-
|
|
|
|
|
27 |
break
|
|
|
|
|
|
|
|
|
28 |
return posts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
except Exception as e:
|
30 |
-
print(f"
|
31 |
return []
|
32 |
|
|
|
33 |
def find_similar_accounts(username: str, rapidapi_key: str) -> List[str]:
|
34 |
"""
|
35 |
Fetch similar accounts using the RapidAPI endpoint.
|
|
|
|
|
1 |
import instaloader
|
2 |
from typing import List, Dict
|
3 |
+
import time
|
4 |
|
5 |
# Initialize Instaloader
|
6 |
L = instaloader.Instaloader()
|
|
|
8 |
def fetch_user_posts(username: str, max_posts: int = 50) -> List[Dict]:
|
9 |
"""
|
10 |
Fetch posts from a given Instagram profile (public data only).
|
11 |
+
|
12 |
+
Args:
|
13 |
+
username (str): Instagram username.
|
14 |
+
max_posts (int): Maximum number of posts to fetch.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
List[Dict]: A list of dictionaries containing post details.
|
18 |
"""
|
19 |
try:
|
20 |
+
# Load the profile
|
21 |
profile = instaloader.Profile.from_username(L.context, username)
|
22 |
posts = []
|
23 |
+
|
24 |
+
# Fetch posts
|
25 |
for post in profile.get_posts():
|
26 |
posts.append({
|
27 |
"username": username,
|
|
|
32 |
"date": post.date_utc.isoformat(),
|
33 |
"image_url": post.url
|
34 |
})
|
35 |
+
|
36 |
+
# Stop if we've reached the maximum number of posts
|
37 |
+
if len(posts) >= max_posts:
|
38 |
break
|
39 |
+
|
40 |
+
# Add a delay to avoid hitting Instagram's rate limits
|
41 |
+
time.sleep(2) # 2-second delay between requests
|
42 |
+
|
43 |
return posts
|
44 |
+
|
45 |
+
except instaloader.exceptions.ProfileNotExistsException:
|
46 |
+
print(f"Error: The profile '{username}' does not exist.")
|
47 |
+
return []
|
48 |
+
except instaloader.exceptions.QueryReturnedBadRequestException:
|
49 |
+
print("Error: Instagram blocked the request. Please wait before trying again.")
|
50 |
+
return []
|
51 |
except Exception as e:
|
52 |
+
print(f"Unexpected error fetching posts for {username}: {e}")
|
53 |
return []
|
54 |
|
55 |
+
|
56 |
def find_similar_accounts(username: str, rapidapi_key: str) -> List[str]:
|
57 |
"""
|
58 |
Fetch similar accounts using the RapidAPI endpoint.
|