import json import sys def main(): try: from instagrapi import Client except ImportError: print(json.dumps({"error": "instagrapi not installed (pip install -r requirements.txt)"})) return try: req = json.loads(sys.stdin.read() or "{}") except Exception as e: print(json.dumps({"error": f"bad input: {e}"})) return action = req.get("action", "user") username = (req.get("username") or "").lstrip("@") if not username: print(json.dumps({"error": "username required"})) return try: cl = Client() cl.delay_range = [1, 3] if action == "user": info = cl.user_info_by_username_v1(username) print(json.dumps({ "found": True, "platform": "instagram", "username": username, "fullName": info.full_name, "biography": info.biography, "followers": info.follower_count, "following": info.following_count, "posts": info.media_count, "private": info.is_private, "verified": info.is_verified, "profileUrl": f"https://instagram.com/{username}", })) elif action == "posts": uid = cl.user_id_from_username(username) medias = cl.user_medias(uid, amount=int(req.get("count", 12) or 12)) print(json.dumps({ "username": username, "posts": [{ "id": str(m.id), "caption": (m.caption_text or "")[:500], "likes": m.like_count, "takenAt": str(m.taken_at), } for m in medias], })) else: print(json.dumps({"error": f"unknown action {action}"})) except Exception as e: print(json.dumps({"found": False, "username": username, "error": str(e)[:500]})) if __name__ == "__main__": main()