Back

Instagram Stalker

81 views
rapp Code by: @rapp

Description

Base url: https://instagram.com" target="_blank" rel="noopener noreferrer">https://instagram.com

Features

Code (javascript)

igstalk.js
const axios = require('axios');

class InstagramStalker {
    constructor() {
        this.client = axios.create({
            headers: {
                'User-Agent': 'Mozilla/5.0 (Linux; Android 10; Mobile) AppleWebKit/537.36',
                'Accept': 'application/json, text/plain, */*'
            },
            timeout: 30000
        });
    }

    async getUserInfo(username) {
        try {
            const response = await this.client.get(`https://www.instagram.com/api/v1/users/web_profile_info/`, {
                params: { username: username },
                headers: {
                    'X-IG-App-ID': '936619743392459'
                }
            });

            if (response.data && response.data.data) {
                const user = response.data.data.user;
                return {
                    username: user.username,
                    full_name: user.full_name,
                    biography: user.biography,
                    followers: user.edge_followed_by?.count || 0,
                    following: user.edge_follow?.count || 0,
                    posts: user.edge_owner_to_timeline_media?.count || 0,
                    is_private: user.is_private,
                    is_verified: user.is_verified,
                    profile_pic: user.profile_pic_url_hd,
                    external_url: user.external_url
                };
            }

            throw new Error('User not found');

        } catch (error) {
            return await this.getUserInfoAlternative(username);
        }
    }

    async getUserInfoAlternative(username) {
        try {
            const response = await this.client.get(`https://www.instagram.com/${username}/`);
            const html = response.data;

            const dataMatch = html.match(/"username":"([^"]+)","full_name":"([^"]*)","biography":"([^"]*)","followers":(\d+),"following":(\d+),"posts":(\d+)/);
            
            if (dataMatch) {
                return {
                    username: dataMatch[1],
                    full_name: dataMatch[2],
                    biography: dataMatch[3],
                    followers: parseInt(dataMatch[4]),
                    following: parseInt(dataMatch[5]),
                    posts: parseInt(dataMatch[6]),
                    is_private: html.includes('"is_private":true'),
                    is_verified: html.includes('"is_verified":true'),
                    profile_pic: this.extractProfilePic(html)
                };
            }

            throw new Error('Could not extract user info');

        } catch (error) {
            throw new Error(`Failed to get user info: ${error.message}`);
        }
    }

    extractProfilePic(html) {
        const picMatch = html.match(/"profile_pic_url_hd":"([^"]+)"/);
        return picMatch ? picMatch[1].replace(/\\u0026/g, '&') : null;
    }

    async getUserPosts(username) {
        try {
            const response = await this.client.get(`https://www.instagram.com/api/v1/feed/user/${username}/username/`, {
                params: { count: 12 },
                headers: {
                    'X-IG-App-ID': '936619743392459'
                }
            });

            if (response.data && response.data.items) {
                return response.data.items.map(post => ({
                    id: post.id,
                    caption: post.caption?.text || '',
                    like_count: post.like_count || 0,
                    comment_count: post.comment_count || 0,
                    timestamp: post.taken_at,
                    permalink: `https://www.instagram.com/p/${post.code}/`,
                    media_url: post.image_versions2?.candidates?.[0]?.url || 
                               post.carousel_media?.[0]?.image_versions2?.candidates?.[0]?.url
                }));
            }

            return [];

        } catch (error) {
            throw new Error(`Failed to get posts: ${error.message}`);
        }
    }
}

async function main() {
    const username = process.argv[2];
    const action = process.argv[3] || 'info';
    
    if (!username) {
        console.log(JSON.stringify({error: "Username required"}));
        process.exit(1);
    }

    const stalker = new InstagramStalker();
    
    try {
        let result;

        if (action === 'posts') {
            result = await stalker.getUserPosts(username);
        } else {
            result = await stalker.getUserInfo(username);
        }

        console.log(JSON.stringify(result, null, 2));
        
    } catch (error) {
        console.log(JSON.stringify({error: error.message}, null, 2));
        process.exit(1);
    }
}

if (require.main === module) {
    main();
}