How koff75/node-toogoodtogo-watcher works
Project Type and Purpose
• This is an open-source Node.js command-line application (“toogoodtogo-watcher”) for monitoring TooGoodToGo business stock changes.
• Purpose: periodically poll the TooGoodToGo API for favorite businesses, then notify the user via desktop notifications, console output, or Telegram.
File Structure Analysis
• Root files:
• index.js – CLI entry point, command dispatcher
• config.defaults.json – default config template
• Dockerfile / .dockerignore – containerization
• package.json / package-lock.json – dependencies & scripts
• lib/ directory (core modules):
• lib/config.js – reads/writes user config
• lib/api.js – wraps TooGoodToGo HTTP API calls
• lib/poller.js – schedules/fires periodic polling jobs
• lib/notifier.js – routes notification events to console or desktop
• lib/telegram-bot.js – Telegram integration and conversational handlers
• lib/emaillogin.js – handles email-based login flow
• .vscode, .eslintrc.js, .prettierrc – developer/editor configs
README Insights
• Describes CLI commands (login, config, watch, etc.)
• Highlights features: desktop notifications, console output, Telegram bot commands
• Details Docker usage with a host directory mounted for persistent config
Components
a. Main Components
1. CLI Layer
• index.js: parses commands via yargs or similar
2. Configuration Service
• lib/config.js: reads config from ~/.config/toogoodtogo-watcher-nodejs/config.json
3. Authentication Service
• lib/emaillogin.js / lib/api.js: login & token management
4. Polling Engine
• lib/poller.js: timers, scheduling logic
5. API Client
• lib/api.js: REST calls to TooGoodToGo endpoints
6. Notification Dispatcher
• lib/notifier.js: desktop & console notifications
• lib/telegram-bot.js: Telegram Bot API integration
7. External Systems
• TooGoodToGo backend (HTTPS API)
• Telegram Bot API
• OS desktop notification center
• File system (config volume in Docker)
b. Relationships & Data Flow
• User invokes CLI → index.js
• index.js loads Configuration Service → fetch credentials & user preferences
• For “watch” command:
• Authentication Service obtains/refreshes token
• Polling Engine periodically calls API Client
• API Client fetches business stock info → returns JSON
• Poller compares new vs. old state → emits change events
• Notification Dispatcher receives events → fans out to:
– Desktop notifier (OS API)
– Console logger
– Telegram-bot client (webhook/long-polling to Telegram API)
• Config changes (via “config” command) update config file on disk (mounted host volume in Docker)
c. Architectural Patterns & Principles
• Modular separation: each concern in its own module (SRP)
• Event-driven polling model: poller emits events to notifier
• Adapter pattern for different notification channels (console, desktop, Telegram)
• CLI command pattern for subcommands (login, watch, config)
d. Key Technologies & Libraries
• Node.js (runtime)
• yargs or similar CLI parser (implied in index.js)
• node-telegram-bot-api or direct HTTPS calls (lib/telegram-bot.js)
• Node FS module for config persistence
• Docker for container deployment
Essential Detail
• Lib modules as distinct components
• Persistent config in file system or Docker volume
• How the poll-and-notify loop interacts with both OS and network endpoints