Skip to content

About

Personal Instagram live follower counter (Flask + Instaloader)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Instagram Live Follower Counter

A single-page, personal-use follower counter for Instagram. Enter a username, watch the follower count update live in big centered numbers, auto-refreshing every 2 minutes — built to look like the "YouTube live subscriber count" trackers, but for Instagram.

status

Features

  • Enter any public Instagram username and track its live follower count
  • Avatar, full name, verified badge, and handle pulled from the same single profile request used for the follower count — no extra API calls
  • Auto-refreshes every 120 seconds with a visible countdown
  • Odometer-style roll animation — only the digits that actually changed animate, shared leading digits stay static
  • Loading screen (spinner) while a lookup is in flight, replacing the search box entirely
  • Deep-linkable via ?user=<username>, remembers your last search in sessionStorage for the current browser tab
  • Light theme, responsive layout
  • In-memory caching (2 min TTL) so repeated requests don't hammer Instagram

Before you run this

  • This uses your own Instagram account's credentials to look up follower counts via Instaloader. Instagram's Terms of Service prohibit automated access — this is built for personal, light-touch use, not as a public-facing product or a tool for scraping at any volume.
  • The server never logs in by itself. It only ever loads a session file you generate locally and copy over. Logging in fresh from a server's IP is the fastest way to trigger a 2FA/checkpoint challenge that a headless process can't answer.
  • Querying many different profiles back-to-back from one account is the fastest way to get rate-limited or checkpointed by Instagram. Keep usage light — this is a single-user tool, not a multi-tenant service.
  • There is no way to get real-time follower counts for accounts you don't control via any official, free API — Instagram's Graph API only exposes data for Business/Creator accounts that have explicitly authorized your app. This project works the same way the unofficial "live counter" novelty sites do: by reading the same public profile data Instagram's own website loads, via a logged-in session.

How it works

Browser  ──fetch──▶  Flask (app.py)  ──Instaloader──▶  Instagram
                          │
                          └─ in-memory cache (2 min TTL)
  • GET /api/followers/<username> returns { username, full_name, is_verified, followers, updated_at, cached } — all pulled from one Instaloader profile request, cached for 2 minutes.
  • GET /api/avatar/<username> proxies the profile photo through the server. Instagram's CDN blocks browsers from hotlinking the image directly (Referer-based protection), and the raw CDN URL is a signed, expiring token anyway — so the server fetches the bytes itself and serves them from its own domain, cached for an hour client-side.
  • GET /health → { "status": "ok" }, for uptime checks.

1. Local setup

git clone https://github.com/Avdhesh-Varshney/instagram-live-follower-counter.git
cd instagram-live-follower-counter

python3 -m venv venv
source venv/bin/activate      # Windows: venv\Scripts\activate

pip install -r requirements.txt

cp .env.example .env
# edit .env and set INSTAGRAM_USERNAME to your own Instagram username

To close the virtual environment later, run deactivate in the same terminal.

2. Generate a session (run this locally, never on a server)

python generate_session.py

This prompts for your Instagram username/password, logs in once, walks you through a 2FA prompt interactively if Instagram asks for one, and writes sessions/instagram.session. Your password is never stored — only the resulting session file, which app.py loads on startup.

3. Run locally

python app.py

By default it listens on port 5000. On macOS, port 5000 is often taken by AirPlay Receiver — if you hit "Address already in use", run instead:

PORT=5050 python app.py

Visit http://localhost:5000 (or :5050). Try http://localhost:5000/?user=instagram to deep-link directly to a username.

If you ever regenerate the session or change .env, restart app.py — it only reads the session and env vars once, at startup.

API reference

Endpoint Method Response
/api/followers/<username> GET { username, full_name, is_verified, profile_pic_url, followers, updated_at, cached } or { error }
/api/avatar/<username> GET Image bytes (proxied profile photo)
/health GET { "status": "ok" }

Deploying to Render

Render's free tier has no persistent disk and no Shell tab — and free services spin down after ~15 minutes of inactivity, wiping the filesystem on every spin-up. That rules out copying the session file onto disk. Use Option A below for free tier; Option B is for a paid instance type if you'd rather use a disk.

Free-tier services also cold-start (~30–60s) on the first request after being asleep. In practice this app's own 120s polling tends to keep it warm while a browser tab has it open, but the very first visit after a period of nobody using it will be slow to respond.

Option A — Free tier, session via env var or Secret File (recommended)

  1. New → Web Service → connect this repo.

  2. Runtime: Python 3. Build command: pip install -r requirements.txt. Start command: gunicorn app:app --workers 1 --threads 4 --timeout 60 --bind 0.0.0.0:$PORT.

  3. Locally, run base64 -i sessions/instagram.session and copy the printed line (session files are tiny, always a single line).

  4. Set INSTAGRAM_USERNAME and CACHE_TTL_SECONDS=120 as regular Environment Variables. Provide the base64 string from step 3 via either:

    • an Environment Variable named INSTAGRAM_SESSION_B64, or
    • a Secret File (Render's Environment tab → Secret Files) named exactly INSTAGRAM_SESSION_B64, pasting the base64 string as its contents. This is Render's purpose-built feature for exactly this kind of secret, so it's the better choice of the two.

    The app checks both — whichever one you used is picked up automatically.

  5. Deploy. On every startup — including after a free-tier spin-down — the app reconstructs sessions/instagram.session from whichever source is present, so there's nothing to re-upload manually, ever, even though the filesystem itself doesn't persist.

Option B — Paid instance, persistent disk

Use this if you're already on a paid plan and prefer a real disk over an env var.

Blueprint (uses render.yaml, which already defines the disk): push the repo, then New → Blueprint and point it at the repo. Set INSTAGRAM_USERNAME in the dashboard (marked sync: false in render.yaml so it's never committed).

Or manually: New → Web Service with the same build/start commands as Option A, plus SESSION_DIR=/app/sessions. Then open the service's Disks tab → Add Disk → mount path /app/sessions, size 1GB.

Either way, upload the session file through the Shell tab (no file-upload UI exists in the dashboard):

mkdir -p /app/sessions
echo "<paste the base64 string here>" | base64 -d > /app/sessions/instagram.session

Verify with wc -c /app/sessions/instagram.session (should match your local file's size), then restart the service — app.py only reads the session file once at startup. The disk persists across deploys and restarts, so this is a one-time step until the session eventually expires.

Deploying to Railway

  1. Push this repo to your own GitHub account, then create a new Railway project from it.
  2. Railway should auto-detect the Procfile. If not, set the start command manually to:
    gunicorn app:app --workers 1 --threads 4 --timeout 60 --bind 0.0.0.0:$PORT
    
  3. Add a Volume in Railway and mount it at /app/sessions — Railway's default filesystem is ephemeral and wipes on every redeploy otherwise.
  4. Set environment variables: INSTAGRAM_USERNAME, CACHE_TTL_SECONDS=120, SESSION_DIR=/app/sessions.
  5. Upload your locally-generated instagram.session file into that volume (Railway shell, or a one-off deploy step) before the first real request.

Troubleshooting: deploy stuck on "Continuing to scan for open port..."

If Render or Railway's logs show gunicorn listening on one port (e.g. 0.0.0.0:10000) while the platform keeps scanning a different one (e.g. 5000) forever, the start command isn't binding to the platform's $PORT env var — it's using gunicorn's own default instead. The --bind 0.0.0.0:$PORT flag in the Procfile/render.yaml above fixes this by making gunicorn always bind to whatever port the platform actually expects, regardless of its value. If you copy the start command anywhere else, keep that flag.

Why --workers 1

Follower-count caching and the Instagram session both live in this process's memory. Gunicorn with more than one worker spins up separate OS processes that don't share that memory — caching would silently stop working and two workers could race over the same session. --workers 1 --threads 4 keeps everything correct in a single process while still handling several concurrent requests via threads.

Project structure

instagram-counter/
├── app.py                  Flask routes (/, /health, /api/followers, /api/avatar)
├── instagram_service.py    Instaloader wrapper: session loading, error handling
├── cache.py                In-memory TTL cache (2 min default)
├── generate_session.py     Run locally once to create sessions/instagram.session
├── requirements.txt
├── Procfile                Gunicorn start command (Railway/Heroku-style)
├── render.yaml              Render Blueprint: service + persistent disk
├── .env.example
├── .gitignore
├── sessions/                instagram.session goes here (gitignored)
├── templates/
│   └── index.html
└── static/
    ├── style.css
    └── script.js

Environment variables

Variable Used by Purpose
INSTAGRAM_USERNAME app.py, generate_session.py Which account's saved session to load
INSTAGRAM_PASSWORD generate_session.py only One-time login when generating the session locally — never read by the running server
CACHE_TTL_SECONDS app.py How long follower data is cached (default 120)
SESSION_DIR app.py, instagram_service.py, generate_session.py Where the session file lives (default sessions)
PORT app.py Port for the dev server (default 5000)
FLASK_DEBUG app.py Set to 1 for Flask debug mode locally

About

Personal Instagram live follower counter (Flask + Instaloader)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages