Newer
Older
navi-1 / tools / instagram_viewer.py
import json
from tools.instagram_engine import execute as engine_execute

name = "instagram_viewer"
description = "View Instagram profiles, posts, and metadata using browser automation."
parameters = {
    "type": "object",
    "properties": {
        "action": {
            "type": "string",
            "enum": ["profile", "posts"],
            "description": "Action to perform: 'profile' for metadata, 'post' for recent posts.",
        },
        "username": {
            "type": "string",
            "description": "The Instagram username to view.",
        },
        "limit": {
            "type": "integer",
            "description": "Number of posts to retrieve (only for 'posts' action).",
            "default": 5,
        },
        "proxy": {
            "type": "object",
            "description": "Proxy configuration: {'server': '...', 'username': '...', 'password': '...'}",
        },
    },
    "required": ["action", "username"],
}

async def execute(params: dict) -> str:
    action = params.get("action")
    username = params.get("username")
    limit = params.get("limit", 5)
    proxy = params.get("proxy")

    # The engine handles the heavy lifting. We just pass the parameters through.
    # We map our tool's 'action' to the engine's 'scrape' action.
    engine_params = {
        "action": "scrape",
        "username": username,
        "limit": limit,
        "proxy": proxy
    }

    try:
        result_json = await engine_execute(engine_params)
        result = json.loads(result_json)

        if result.get("status") == "error":
            return f"Error viewing Instagram profile: {result.get('message')}"

        if action == "profile":
            # Return only profile metadata
            profile_info = {
                "username": result.get("username"),
                "profile": result.get("profile")
            }
            return json.dumps(profile_info, indent=2, ensure_ascii=False)
        
        elif action == "posts":
            # Return only posts
            posts_info = {
                "username": result.get("username"),
                "recent_posts": result.get("recent_posts")
            }
            return json.dumps(posts_info, indent=2, ensure_ascii=False)
        
        else:
            return "Unsupported action. Use 'profile' or 'posts'."

    except Exception as e:
        return f"An error occurred while executing the Instagram viewer: {str(e)}"