Update utils/instaloader_utils.py
Browse files- utils/instaloader_utils.py +12 -23
utils/instaloader_utils.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import instaloader
|
2 |
-
from typing import List, Dict
|
3 |
-
import
|
4 |
|
5 |
# Initialize Instaloader
|
6 |
L = instaloader.Instaloader()
|
@@ -10,47 +10,36 @@ def fetch_user_posts(username: str, max_posts: int = 50) -> List[Dict]:
|
|
10 |
Fetch posts from a given Instagram profile (public data only).
|
11 |
|
12 |
Args:
|
13 |
-
username (str): Instagram
|
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,
|
28 |
-
"caption": post.caption,
|
29 |
-
"hashtags": post.caption_hashtags,
|
30 |
"likes": post.likes,
|
31 |
"comments": post.comments,
|
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:
|
47 |
-
|
48 |
-
|
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 |
-
|
|
|
54 |
|
55 |
|
56 |
def find_similar_accounts(username: str, rapidapi_key: str) -> List[str]:
|
|
|
1 |
import instaloader
|
2 |
+
from typing import List, Dict, Optional
|
3 |
+
import instaloader.exceptions
|
4 |
|
5 |
# Initialize Instaloader
|
6 |
L = instaloader.Instaloader()
|
|
|
10 |
Fetch posts from a given Instagram profile (public data only).
|
11 |
|
12 |
Args:
|
13 |
+
username (str): The username of the Instagram profile.
|
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 |
profile = instaloader.Profile.from_username(L.context, username)
|
21 |
posts = []
|
|
|
|
|
22 |
for post in profile.get_posts():
|
23 |
posts.append({
|
24 |
"username": username,
|
25 |
+
"caption": post.caption or "", # Handle None captions
|
26 |
+
"hashtags": post.caption_hashtags if post.caption else [],
|
27 |
"likes": post.likes,
|
28 |
"comments": post.comments,
|
29 |
"date": post.date_utc.isoformat(),
|
30 |
"image_url": post.url
|
31 |
})
|
32 |
+
if len(posts) >= max_posts: # Limit the number of posts
|
|
|
|
|
33 |
break
|
|
|
|
|
|
|
|
|
34 |
return posts
|
|
|
35 |
except instaloader.exceptions.ProfileNotExistsException:
|
36 |
+
print(f"Error: Profile '{username}' does not exist.")
|
37 |
+
except instaloader.exceptions.ConnectionException:
|
38 |
+
print("Error: Unable to connect to Instagram. Check your connection.")
|
|
|
|
|
39 |
except Exception as e:
|
40 |
+
print(f"Unexpected error while fetching posts for {username}: {e}")
|
41 |
+
return []
|
42 |
+
|
43 |
|
44 |
|
45 |
def find_similar_accounts(username: str, rapidapi_key: str) -> List[str]:
|