# Scorpia Social Media Harvester

> **Status:** In daily use: runs on a Linux home server, queued from the web (Meta Collector); previously on a production VPS  
> **Repository:** Private Internal Repository  
> **Tech Stack:** Python 3, Flask, Playwright (Chromium), Xvfb / Headed Display, systemd, rclone + Google Drive, PostgreSQL (Supabase), Next.js front end on Vercel

---

## 1. Overview & Introduction
The **Scorpia Social Media Harvester** is a specialized multi-platform archiving engine designed for content operators managing Facebook, Instagram, TikTok, and Pinterest channels. It automates media archiving, highlights capture, story downloads, and profile ingest into structured cloud vaults without manual file management.

## 2. The Operational Problem
Managing digital media and content archives for business pages and clients involves severe challenges:
- Stories and ephemeral content disappear within 24 hours.
- Major platforms (Meta, TikTok) actively detect and block headless browser automation by analyzing browser fingerprints.
- Manual downloading and organizing hundreds of media files into client Google Drive folders takes hours of repetitive manual effort.

## 3. System Architecture & Technical Innovation
```
[User / Operator (Flask Web UI)]
       │
       ▼
[Sequential Job Queue (In-Memory / SQLite)]
       │
       ▼
[Playwright Chromium Instance]
  ├── Persistent Tenant User Profile (Cookies / LocalStorage)
  ├── Headed Execution via virtual display (Xvfb / DISPLAY)
  └── Window Hidden After Auth Verification
       │
       ▼
[Media Download & Content Ingest Engine]
       │
       ▼
[Google Drive API (OAuth 2.0 per-user folder upload)]
```

### Key Engineering Decisions
- **Headed, Then Hidden:** Instead of running standard `headless=True` (which causes Meta bot detection to revoke sessions), the harvester runs a true headed Chromium instance on a virtual frame buffer (`Xvfb`). Once verified, the window is hidden, preserving authenticated login sessions.
- **Tenant Isolation:** Every signed-in operator has a private browser profile and unique Google Drive directory, preventing credential cross-contamination.
- **Sequential Rate-Limiting:** Social platforms penalize burst parallel logins; jobs are processed sequentially with randomized delays to emulate human browsing patterns.

## 4. Core Features
- **Multi-Platform Support:** Archives wall posts, highlights, My Day stories, reels, and video assets across Facebook, Instagram, TikTok, and Pinterest.
- **Auto-Sync to Google Drive:** Direct integration with Google Drive API (`drive.file` scope) uploads structured media folders directly to client drives.
- **Interactive Queue & Ledger:** Live web dashboard tracking job status, downloaded asset counts, and queue health.

## 5. How It Runs Now (November 2026)
```
[Meta Collector (Next.js on Vercel)] ──► [Job queue in Postgres]
                                               ▲
                                               │ polls, outbound only
[Linux home server: systemd services] ── [Queue bridge] (no open ports at home)
       │
       ▼
[Harvester: Flask + headed Playwright] ──► [Local disk first] ──► [rclone ► Google Drive archive]
                                                                       (verified file by file)
```
- **Queued from anywhere, nothing exposed at home:** jobs are added on the Meta Collector web app (or from a phone); a bridge on the home server polls the queue outbound, so the machine running the browsers has no public ports.
- **Local first, cloud second:** each job saves to local disk at full speed, then uploads to the Google Drive archive in the background with `rclone`, checked file by file before the local copy is cleared. Writing straight into a mounted Drive had made one large job take 9.5 hours just to save ~7,600 files; local-first saving cut that step to **under 10 minutes**.
- **Incremental page scanning:** long profile walls used to slow down as the page grew, because every pass re-read the whole page. The scanner now remembers what it has already seen inside the page and only reads new elements, with a cheap filter for page furniture. A 3,000-pass wall scan went from **7.5 hours to 1.5 hours**, holding a steady rate to the end.
- **End to end:** a comparable large first-time job went from **19 hours to 2.6 hours**.
- **Updates, not repeats:** re-running a profile restores its manifest from the archive and fetches only what is new; very deep walls are recorded as capped and only re-walked weekly, while reels and videos get a monthly deep check.
- **Operated like a service:** systemd units with automatic restarts, health endpoints, aggregate-only stats published to an admin page (counts, never names), and alerts to the owner's phone through Scorpia Monitor.
