Actor picture

Instagram IDs to username

pocesar/instagram-ids-to-usernames

Convert Instagram ID numbers back to usernames

No credit card required

Author's avatarPaulo Cesar
  • Modified
  • Users162
  • Runs24,973
Actor picture
Instagram IDs to username

.editorconfig

root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf

.eslintrc

{
    "extends": "@apify"
}

.gitignore

# This file tells Git which files shouldn't be added to source control

.idea
node_modules

Dockerfile

# First, specify the base Docker image. You can read more about
# the available images at https://sdk.apify.com/docs/guides/docker-images
# You can also use any other image from Docker Hub.
FROM apify/actor-node:16

# Second, copy just package.json and package-lock.json since it should be
# the only file that affects "npm install" in the next step, to speed up the build
COPY package*.json ./

# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging too much and print the dependency
# tree for debugging
RUN npm --quiet set progress=false \
 && npm install --only=prod --no-optional \
 && echo "Installed NPM packages:" \
 && (npm list --only=prod --no-optional --all || true) \
 && echo "Node.js version:" \
 && node --version \
 && echo "NPM version:" \
 && npm --version

# Next, copy the remaining files and directories with the source code.
# Since we do this after NPM install, quick build will be really fast
# for most source file changes.
COPY . ./

# Optionally, specify how to launch the source code of your actor.
# By default, Apify's base Docker images define the CMD instruction
# that runs the Node.js source code using the command specified
# in the "scripts.start" section of the package.json file.
# In short, the instruction looks something like this:
#
# CMD npm start

INPUT_SCHEMA.json

{
    "title": "Input schema for the apify_project actor.",
    "type": "object",
    "schemaVersion": 1,
    "properties": {
        "ids": {
            "title": "Instagram Ids",
            "type": "array",
            "default": [],
            "prefill": ["297604134"],
            "description": "Get the usernames of given Instagram Ids",
            "editor": "stringList"
        }
    },
    "required": []
}

README.md

# Instagram IDs to usernames

This actor allows you to convert a list of Instagram user IDs to their appropriate usernames. 

When scraping posts or comments, Instagram often doesn't give you final user username but only their numerical representation.
This actor allows you easily convert them and adds also general user data.

## Input
Input is a list of user IDs, e.g.
```json
{
    "ids": ["898537494"]
}
```

You don't need to use JSON format, the actor allows you to use visual interface

## Webhook

You can point other Instagram scrapers that don't have the username directly to this actor, and it will generate a dataset enriched with the user data for you!

```
https://api.apify.com/v2/acts/pocesar~instagram-ids-to-usernames/runs?token=[YOUR TOKEN HERE]
```

When you call this actor from the webhook, it will write the dataset ID back to your run, under `DATASET_ID` key in the Key value store, and `RUN_ID` with the run 
of this actor, so you can link them together.

## Output
Output is a list of user data containing usernames and user URLs, e.g.

```json
[
    {
        "username": "chizawagames",
        "pk": 898537494,
        "profile_pic_url": "https://scontent-atl3-2.cdninstagram.com/v/t51.2885-19/s150x150/75322217_443843236272081_1698154713637191680_n.jpg?_nc_ht=scontent-atl3-2.cdninstagram.com&_nc_cat=109&_nc_ohc=WDDwSFDUDHgAX8WVXFk&edm=AEF8tYYBAAAA&ccb=7-4&oh=db447812033dfaa22803cfc52e48393d&oe=61A4B454&_nc_sid=a9513d",
        "url": "https://www.instagram.com/chizawagames"
    }
]
```

The output is also available as CSV, Excel, Xml and other formats.

If you call it using a actor, like https://apify.com/pocesar/fast-instagram-hashtag-scraper, it will append the user info on the datasets from it. 

apify.json

{
    "env": { "npm_config_loglevel": "silent" }
}

main.js

// This is the main Node.js source code file of your actor.

// Import Apify SDK. For more information, see https://sdk.apify.com/
const Apify = require('apify');

const { log } = Apify.utils;

const isNumber = (s) => +s == s;
const addRequest = (id, userData) => ({ url: `https://i.instagram.com/api/v1/users/${id}/info/`, userData });

Apify.main(async () => {
    const { ids, resource } = await Apify.getInput();

    const requestQueue = await Apify.openRequestQueue();
    let count = 0;

    if (resource?.defaultDatasetId) {
        const dataset = await Apify.openDataset(resource.defaultDatasetId);

        await dataset.forEach(async (item) => {
            const id = [
                item.ownerId,
                item.owner,
            ].filter(isNumber).map(String);

            if (id[0]) {
                await requestQueue.addRequest(addRequest(id[0], item));
                count++;
            }
        });

        if (resource?.defaultKeyValueStoreId) {
            const kv = await Apify.openKeyValueStore(resource?.defaultKeyValueStoreId);
            const { defaultDatasetId, actorRunId } = Apify.getEnv(); 
            
            await Promise.all([
                kv.setValue('DATASET_ID', defaultDatasetId),
                kv.setValue('RUN_ID', actorRunId),
            ]);
        }
    }

    for (const id of (ids ?? [])) {
        await requestQueue.addRequest(addRequest(id));
        count++;
    }

    const proxyConfiguration = await Apify.createProxyConfiguration({
        groups: ['RESIDENTIAL'],
    });

    const idCount = await requestQueue.getInfo().then(({ totalRequestCount }) => totalRequestCount || count);

    log.info(`Going to fetch information for ${idCount} ids`);

    const crawler = new Apify.CheerioCrawler({
        requestQueue,
        proxyConfiguration,
        preNavigationHooks: [async (context, requestAsBrowserOptions) => {
            requestAsBrowserOptions.headers = {
                ...requestAsBrowserOptions.headers,
                "Accept": "*/*",
                "Alt-Used": "i.instagram.com",
                "Accept-Language": "en-US,en",
                "X-IG-App-ID": "936619743392459",
                "X-ASBD-ID": "198387",
                "X-IG-WWW-Claim": "0",
                "Origin": "https://www.instagram.com",
                "Referrer": "https://www.instagram.com/",
            };
        }],
        handlePageFunction: async (context) => {
            const { request, json } = context;

            if (json?.status !== "ok") {
                throw new Error(`Got wrong status "${json?.status ?? json?.message ?? '-'}"`);
            }

            if (!json?.user) {
                throw new Error(`Missing user property from response`);
            }

            await Apify.pushData({ 
                ...json.user,
                url: `https://www.instagram.com/${json.user.username}`,
                ...request.userData,
            });
        }
    });

    await crawler.run();
});

package.json

{
    "name": "project-empty",
    "version": "0.0.1",
    "description": "This is a boilerplate of an Apify actor.",
    "dependencies": {
        "apify": "^2.3.2"
    },
    "scripts": {
        "start": "node main.js",
        "lint": "./node_modules/.bin/eslint ./src --ext .js,.jsx",
        "lint:fix": "./node_modules/.bin/eslint ./src --ext .js,.jsx --fix",
        "test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
    },
    "author": "It's not you it's me",
    "license": "ISC"
}