File size: 2,291 Bytes
0ee2df7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Instagram via instaloader. Public data needs no auth; pass an optional
session cookie in tool_keys (tool='instagram') for stories/highlights/saved.
"""
import instaloader
from supabase_client import get_tool_keys


def _get_loader() -> instaloader.Instaloader:
    L = instaloader.Instaloader()
    creds = get_tool_keys("instagram")
    if creds and creds.get("api_key"):
        try:
            L.context._session.cookies.set("sessionid", creds["api_key"])
        except Exception:
            pass
    return L


def run(params: dict) -> dict:
    mode = params.get("mode")
    target = params.get("target")
    if mode not in ("profile", "hashtag") or not target:
        raise ValueError("mode must be 'profile' or 'hashtag', and target is required")
    max_posts = int(params.get("max_posts", 20))

    L = _get_loader()

    if mode == "profile":
        profile = instaloader.Profile.from_username(L.context, target)
        posts = []
        for i, post in enumerate(profile.get_posts()):
            if i >= max_posts:
                break
            posts.append(
                {
                    "shortcode": post.shortcode,
                    "caption": (post.caption or "")[:200],
                    "likes": post.likes,
                    "comments": post.comments,
                    "date": str(post.date_utc),
                    "url": f"https://www.instagram.com/p/{post.shortcode}/",
                }
            )
        return {
            "profile": target,
            "followers": profile.followers,
            "bio": profile.biography,
            "post_count": profile.mediacount,
            "posts": posts,
        }

    # hashtag mode
    hashtag = instaloader.Hashtag.from_name(L.context, target)
    posts = []
    for i, post in enumerate(hashtag.get_posts()):
        if i >= max_posts:
            break
        posts.append(
            {
                "shortcode": post.shortcode,
                "caption": (post.caption or "")[:200],
                "likes": post.likes,
                "comments": post.comments,
                "date": str(post.date_utc),
                "url": f"https://www.instagram.com/p/{post.shortcode}/",
            }
        )
    return {"hashtag": target, "count": len(posts), "results": posts}