SDK to collect Instagram data (profile and posts) using an external scraper, with a high-level abstraction layer (InstagramCollector) and file-based cache support to reduce API calls.
npm install @dukebot/instagram-collectorInstagramScraperAPI: low-level scraper client (profile/posts).InstagramCollector: high-level orchestration layer for batch collection (arrays of usernames/ids), callbacks, and cache.utils: helpers (loadDotenv,saveDataAsJson).entities:User,Post,Location.
import { InstagramCollector } from '@dukebot/instagram-collector';
const collector = new InstagramCollector({
apiKey: process.env.API_KEY,
usernamesOrIds: ['instagram'],
cache: {
enabled: true,
dir: '.cache/instagram-collector',
ttlMs: 10 * 60 * 1000,
forceRefresh: false,
},
});
const info = await collector.collectUserInfo({ useCache: true });
const posts = await collector.collectUserPosts({ numPosts: 3, useCache: true });const { InstagramCollector } = require('@dukebot/instagram-collector');
const collector = new InstagramCollector({
apiKey: process.env.API_KEY,
usernamesOrIds: ['instagram'],
});
collector.collectUserInfo({ useCache: true }).then(console.log);new InstagramCollector({
apiKey,
usernamesOrIds?,
onUserInfo?,
onUserPosts?,
cache?: {
enabled?: boolean,
dir?: string,
ttlMs?: number,
forceRefresh?: boolean,
}
})- Collects user info for an array of usernames/ids.
- Supports per-item callback (
onUserInfo). - Supports cache keys (
instagram:user-info:<usernameOrId>).
- Collects user posts.
- Supports limiting number of posts (
numPosts). - Supports per-item callback (
onUserPosts). - Supports cache keys (
instagram:user-posts:<usernameOrId>:numPosts:<N>).
In .env:
API_KEY=your_rapid_api_key
INSTAGRAM_USERNAME=instagram
TEST_USERNAMES=instagram,nasa
TEST_NUM_POSTS=3
TEST_CACHE_DIR=.cache/instagram-collector-test
TEST_CACHE_TTL_MS=600000
TEST_FORCE_REFRESH=falseIncluded in the repo:
npm run test:cacheThis command:
- Builds the package (
npm run build) - Loads
.env - Downloads user info and posts for configured accounts
- Stores results in file-based cache
npm run typecheck
npm run build
npm pack --dry-runPublish:
npm publish --access publicThe package is configured for dual output ESM + CJS and TypeScript declarations (
dist/index.mjs,dist/index.cjs,dist/index.d.ts).