Skip to content

Robert-Ernst/Qbittorrent_Downloadhandler

Repository files navigation

Torrent Management System

This repository provides an automated torrent management system that integrates with qBittorrent's Web API to categorize, process, and organize downloads from multiple sources (Empornium, OneJav). It includes robust error handling, file system monitoring, and automatic cleanup of non-media content.

Features

  • Automatic Torrent Categorization: Identifies torrent origin from metadata and assigns appropriate categories
  • Smart File Processing: Moves completed downloads to organized destination folders, removes non-video files, and handles cross-filesystem moves
  • Reliable Monitoring: Alternative monitor system using polling and a systemd service (bypasses qBittorrent's unreliable external program feature)
  • OneJav Torrent Fetcher: Automated script to download torrent files from OneJav.com with pagination support
  • Comprehensive Logging: Detailed logging system with error tracking and rotation support
  • Configuration Validation: Built-in tools to verify setup and dependencies
  • Cross-Platform: Built with Deno for consistent behavior across different operating systems

Components

  • qBittorrent Handler: Core system for categorizing and processing torrents based on origin and content
  • OneJav Torrent Fetcher: Standalone script for automated torrent file downloading from OneJav.com -- monitor-torrents.js: Reliable alternative to qBittorrent's external programs using periodic polling (no trigger file support)
  • qbittorrent-monitor.service: Systemd service for automatic monitor startup and management -- trigger-monitor.js (removed): The previous trigger script has been deprecated and removed from active use
  • config.js: Centralized configuration management with environment variable support
  • sharedFunctions.js: Common utilities including logging, API calls, and error handling
  • validate-config.js: Configuration validation and health check tool

Architecture

The system operates in two modes:

1. Traditional Mode

  • qBittorrent calls torrentProcessor.js directly when torrents are added/finished
  • Uses qBittorrent's built-in "Run external program" feature
  • Note: This method has reliability issues in recent qBittorrent versions

-### 2. Monitor Mode

  • Periodic polling: monitor-torrents.js periodically polls the qBittorrent API to find completed torrents and process them.
  • Provides reliable, continuous processing without reliance on qBittorrent external programs or trigger files.
  • Runs as a systemd service for continuous operation
  • Includes fallback polling every 5 minutes for missed events

Requirements

  • Runtime: Deno 1.40+
  • qBittorrent: Version 5.1.2+ with Web UI enabled
  • System: Linux with systemd (for monitor service) or manual process management
  • Permissions: Network access, file system read/write, and process execution
  • Storage: Adequate space for temporary downloads and final organized content

Installation

1. Install Deno

curl -fsSL https://deno.land/install.sh | sh
# Add to PATH: export PATH="$HOME/.deno/bin:$PATH"

2. Clone Repository

git clone https://github.com/yourusername/qbittorrent_downloadhandler.git
cd qbittorrent_downloadhandler

3. Configure Environment (Optional but Recommended)

# Copy example config (if available) or create .env file
cp .env.example .env  # if .env.example exists

# Edit .env with your settings (see Configuration section below)

4. Validate Configuration

deno run --allow-net --allow-read --allow-write --allow-env validate-config.js

5. Set Up Monitor System

# Install systemd service
sudo cp qbittorrent-monitor.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable qbittorrent-monitor.service
sudo systemctl start qbittorrent-monitor.service

# Check status
sudo systemctl status qbittorrent-monitor.service

6. Configure qBittorrent

In qBittorrent settings → Behavior → Run external program:

For Monitor Mode: The monitor operates as a polling service; no qBittorrent external program configuration is necessary. For users who prefer immediate execution via qBittorrent, use torrentProcessor.js under the traditional mode settings instead.

For Traditional Mode:

  • On torrent added: deno run --allow-all torrentProcessor.js "%N" "%L" "%G" "%F" "%R" "%D" "%C" "%Z" "%T" "%I" "%J" "%K" added
  • On torrent finished: deno run --allow-all torrentProcessor.js "%N" "%L" "%G" "%F" "%R" "%D" "%C" "%Z" "%T" "%I" "%J" "%K" finished

Configuration

All settings can be customized via environment variables in a .env file or directly in the shell. The system provides sensible defaults.

qBittorrent API Settings

  • QB_HOST: qBittorrent host (default: localhost)
  • QB_PORT: qBittorrent Web UI port (default: 8080, note: your setup uses 12389)
  • QB_PROTOCOL: HTTP protocol (default: http)
  • QB_USERNAME: Web UI username (default: none)
  • QB_PASSWORD: Web UI password (default: none)

File Paths

  • EMPORNIUM_FINISHED_PATH: Destination for Empornium torrents (default: /mnt/ST26/Torrents/Fertig/Porn/EmporniumFinished/)
  • ONEJAV_FINISHED_PATH: Destination for OneJav torrents (default: /mnt/ST26/Torrents/Fertig/Porn/OnejavFinished/)
  • TEMP_DOWNLOAD_PATH: Temporary download directory (default: /mnt/Torrent/Torrents/Fertig)

Logging

  • LOG_FILE: Main log file (default: ./log.log)
  • ERROR_LOG_FILE: Error log file (default: ./error.log)
  • LOG_LEVEL: Logging verbosity (default: info, options: debug, info, warn, error)

Processing Options

  • MAX_RETRIES: API retry attempts (default: 3)
  • API_TIMEOUT: API request timeout in ms (default: 30000)
  • MAX_LOG_SIZE: Maximum log file size in bytes (default: 1048576 / 1MB)
  • MAX_FILE_SIZE: Maximum file size to process in bytes (default: 10737418240 / 10GB)

OneJav Fetcher

  • ONEJAV_ENABLED: Enable/disable OneJav fetcher (default: true)
  • ONEJAV_BASE_URL: OneJav website base URL (default: https://onejav.com)
  • ONEJAV_DOWNLOAD_PATH: Local torrent download directory (default: ./torrents/)
  • ONEJAV_RATE_LIMIT: Delay between requests in ms (default: 1000)
  • ONEJAV_MAX_PAGES: Maximum pages to fetch per run (default: 10)

Example .env File

# qBittorrent Settings
QB_HOST=localhost
QB_PORT=12389
QB_USERNAME=admin
QB_PASSWORD=yourpassword

# File Paths
EMPORNIUM_FINISHED_PATH=/mnt/ST26/Torrents/Fertig/Porn/EmporniumFinished/
ONEJAV_FINISHED_PATH=/mnt/ST26/Torrents/Fertig/Porn/OnejavFinished/
TEMP_DOWNLOAD_PATH=/mnt/Torrent/Torrents/Fertig

# Logging
LOG_LEVEL=info
LOG_FILE=/var/log/qbittorrent-handler.log

# Processing
MAX_RETRIES=3
API_TIMEOUT=30000

Usage Examples

1. Fetch OneJav Torrents

# Fetch yesterday's torrents
deno run --allow-net --allow-write fetchOnejav.js

# Fetch torrents from specific date
deno run --allow-net --allow-write fetchOnejav.js "2024-10-28"

2. Validate Configuration

deno run --allow-net --allow-read --allow-write --allow-env validate-config.js

3. Manual Torrent Processing (Testing)

# Test with sample arguments
deno run --allow-all torrentProcessor.js "Test Torrent" "" "" "/tmp/test" "/tmp/root" "/tmp/save" "1" "1000000" "http://test.tracker" "testhash1" "testhash2" "testid123" "finished"

4. Monitor Service Management

# Start monitor
sudo systemctl start qbittorrent-monitor.service

# Check logs
sudo journalctl -u qbittorrent-monitor.service -f

# Restart monitor
sudo systemctl restart qbittorrent-monitor.service

File Structure

qbittorrent_downloadhandler/
├── config.js                 # Configuration management
├── sharedFunctions.js        # Common utilities and logging
├── torrentProcessor.js       # Main entry point for traditional mode
├── handleAddedTorrent.js     # Categorization logic
├── handleFinishedTorrent.js  # File processing and cleanup
├── monitor-torrents.js       # Reliable monitoring system
├── (trigger-monitor.js removed)
├── fetchOnejav.js           # OneJav torrent fetcher
├── validate-config.js       # Configuration validation
├── qbittorrent-monitor.service # Systemd service file
├── docs/
│   ├── Qbittorrent.md       # qBittorrent handler documentation
│   └── Onejav.md            # OneJav fetcher documentation
├── log.log                  # Main application log
├── error.log                # Error log
└── torrents/                # Downloaded torrent files

Troubleshooting

Common Issues

  1. Permission Denied Errors

    • Ensure Deno has proper permissions: --allow-net --allow-read --allow-write --allow-run
    • Check file system permissions for destination paths
    • Verify systemd service has correct ReadWritePaths
  2. qBittorrent API Connection Failed

    • Verify Web UI is enabled in qBittorrent settings
    • Check QB_HOST and QB_PORT match your qBittorrent configuration
    • Test API access: curl http://localhost:12389/api/v2/app/version
  3. Monitor Not Starting

    • Check systemd service status: sudo systemctl status qbittorrent-monitor.service
    • View logs: sudo journalctl -u qbittorrent-monitor.service
    • Verify Deno binary path in service file
  4. Files Not Moving to Destination

    • Check if paths exist and are writable: ls -la /mnt/ST26/Torrents/Fertig/Porn/
    • Verify cross-filesystem moves work (monitor logs for cross-device errors)
    • Test manual move: mv /tmp/testfile /mnt/ST26/Torrents/Fertig/Porn/test/

Log Analysis

Monitor log files for detailed debugging:

# View recent logs
tail -f log.log

# View errors only
grep "ERROR" error.log

# Monitor service logs
sudo journalctl -u qbittorrent-monitor.service -f

Development

Running Tests

The project includes basic validation. For comprehensive testing:

# Run configuration validation
deno run --allow-net --allow-read --allow-write validate-config.js

# Test individual components
deno run --allow-all test_args.txt  # Uses test arguments

Adding New Features

  1. New Torrent Sources: Extend getTorrentOrigin() in sharedFunctions.js
  2. Custom Processing: Modify handleFinishedTorrent.js for specific file types
  3. Enhanced Monitoring: Add new trigger types in monitor-torrents.js

Security Considerations

  • API Access: Restrict qBittorrent Web UI to localhost only
  • File Permissions: Set proper ownership on destination directories
  • Network Access: Monitor only allows necessary network permissions
  • Log Files: Rotate logs regularly to prevent disk space issues

License

This project is open source. See LICENSE for details.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Update documentation
  5. Submit a pull request

For major changes, please open an issue first to discuss your ideas.


Current Status: Production-ready with monitor system. Traditional external program mode available but less reliable.

Todo: See todo.md for planned enhancements and bug fixes.

About

This repository provides an automated torrent management system that integrates with qBittorrent's Web API and fetches torrents from OneJav.com.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors