332 lines
No EOL
12 KiB
Markdown
332 lines
No EOL
12 KiB
Markdown
# newMusic Application - Developer Guide
|
||
|
||
## Overview
|
||
|
||
This document provides comprehensive documentation for the newMusic application, a sophisticated music download manager with Spotify integration. The application enables users to search, download, and organize music from the Soulseek network while providing advanced Spotify matching capabilities for metadata enhancement and folder organization.
|
||
|
||
## Architecture
|
||
|
||
The application follows a modular architecture with clear separation of concerns:
|
||
|
||
- **Core Module**: Business logic and external service integrations
|
||
- **UI Module**: Qt-based user interface components
|
||
- **Services Module**: High-level service orchestration
|
||
- **Main Module**: Application entry point and global management
|
||
|
||
## Key Components
|
||
|
||
### Core Components
|
||
|
||
#### SoulseekClient (`/core/soulseek_client.py`)
|
||
Primary interface for Soulseek P2P network operations.
|
||
|
||
**Key Functions:**
|
||
- `search()` - Performs comprehensive search with real-time progress tracking
|
||
- `download()` - Initiates file downloads from network peers
|
||
- `search_and_download_best()` - Automated search and download of highest quality matches
|
||
- `get_all_downloads()` - Retrieves status of all active downloads
|
||
- `cancel_download()` - Cancels specific downloads by ID
|
||
- `clear_all_completed_downloads()` - Batch cleanup of completed downloads
|
||
|
||
#### SpotifyClient (`/core/spotify_client.py`)
|
||
OAuth-authenticated Spotify Web API integration.
|
||
|
||
**Key Functions:**
|
||
- `get_user_playlists()` - Retrieves all user playlists with full track metadata
|
||
- `search_tracks()` - Searches Spotify catalog for tracks (critical for artist matching)
|
||
- `get_track_features()` - Retrieves audio features for advanced matching
|
||
- `is_authenticated()` - Validates current authentication status
|
||
|
||
#### MusicMatchingEngine (`/core/matching_engine.py`)
|
||
Sophisticated matching algorithm for correlating tracks across services.
|
||
|
||
**Key Functions:**
|
||
- `calculate_match_confidence()` - Computes similarity scores between tracks
|
||
- `find_best_match()` - Identifies highest confidence matches from candidates
|
||
- `match_playlist_tracks()` - Batch matching for entire playlists
|
||
- `normalize_string()` - Text normalization for consistent matching
|
||
- `create_search_queries()` - Generates multiple query variations for improved results
|
||
|
||
#### PlexClient (`/core/plex_client.py`)
|
||
Integration with Plex Media Server for library management.
|
||
|
||
**Key Functions:**
|
||
- `get_all_tracks()` - Retrieves complete music library metadata
|
||
- `update_track_metadata()` - Modifies track information in Plex database
|
||
- `_find_track()` - Locates specific tracks using flexible search criteria
|
||
|
||
### UI Components
|
||
|
||
#### DownloadsPage (`/ui/pages/downloads.py`)
|
||
Central hub for search, download, and queue management.
|
||
|
||
**Key Functions:**
|
||
- `perform_search()` - Initiates search operations with progress tracking
|
||
- `create_search_result_item()` - Generates UI widgets for search results
|
||
- `on_search_results_partial()` - Handles real-time result updates
|
||
- `clear_search_results()` - Resets search interface state
|
||
|
||
#### DownloadQueue (`/ui/pages/downloads.py`)
|
||
Visual queue management for download operations.
|
||
|
||
**Key Functions:**
|
||
- `add_download_item()` - Adds new items to download queue with progress tracking
|
||
- `clear_completed_downloads()` - Removes completed items and updates UI
|
||
- `move_to_finished()` - Transitions downloads through status states
|
||
|
||
#### MediaPlayer (`/ui/sidebar.py`)
|
||
Integrated media player with playback controls.
|
||
|
||
**Key Functions:**
|
||
- `set_track_info()` - Updates player with current track metadata
|
||
- `toggle_expansion()` - Switches between collapsed and expanded views
|
||
- Playback control functions for audio streaming
|
||
|
||
### Service Layer
|
||
|
||
#### PlaylistSyncService (`/services/sync_service.py`)
|
||
High-level orchestration of playlist synchronization operations.
|
||
|
||
**Key Functions:**
|
||
- `sync_playlist()` - Complete playlist synchronization workflow
|
||
- `get_sync_preview()` - Generates preview of sync operations for user confirmation
|
||
- Progress tracking and error handling for complex operations
|
||
|
||
## Data Models
|
||
|
||
### Track Model
|
||
```python
|
||
@dataclass
|
||
class Track:
|
||
id: str
|
||
title: str
|
||
artist: str
|
||
album: str
|
||
duration: int
|
||
spotify_id: Optional[str] = None
|
||
```
|
||
|
||
### Search Result Models
|
||
```python
|
||
@dataclass
|
||
class TrackResult:
|
||
filename: str
|
||
user: str
|
||
size: int
|
||
bit_rate: int
|
||
sample_rate: int
|
||
duration: int
|
||
format: str
|
||
# Auto-parsed metadata
|
||
title: str
|
||
artist: str
|
||
album: str
|
||
track_number: Optional[int]
|
||
```
|
||
|
||
### Download Status Model
|
||
```python
|
||
@dataclass
|
||
class DownloadStatus:
|
||
id: str
|
||
filename: str
|
||
user: str
|
||
state: str # "Queued", "InProgress", "Completed", "Cancelled"
|
||
progress: float
|
||
transferred_bytes: int
|
||
total_bytes: int
|
||
start_time: Optional[datetime]
|
||
end_time: Optional[datetime]
|
||
```
|
||
|
||
## Download System Architecture
|
||
|
||
*Reference: [download-details.md](download-details.md)*
|
||
|
||
The newMusic application implements a sophisticated download management system handling music downloads from the Soulseek P2P network. The download system provides the foundation for all file acquisition operations and serves as the base for the Spotify matching functionality.
|
||
|
||
### Download Flow Overview
|
||
|
||
The download system follows a well-defined flow from user interaction to file completion:
|
||
|
||
1. **Button Interaction**: Download buttons on tracks/albums trigger request signals
|
||
2. **Queue Management**: Downloads are added to active queue with progress tracking
|
||
3. **Thread Execution**: Background threads handle actual file downloads
|
||
4. **Status Monitoring**: Real-time progress updates via API polling
|
||
5. **Queue Transitions**: Completed downloads move to finished queue
|
||
6. **Cleanup Operations**: Clear completed functionality removes finished items
|
||
|
||
### Key Integration Points
|
||
|
||
The current download system provides several extension points for Spotify matching:
|
||
- **`start_download()`** at `downloads.py:5065` - Primary download initiation
|
||
- **`add_download_item()`** at `downloads.py:3006` - Queue item creation
|
||
- **Download completion handling** at `downloads.py:5860` - Post-download processing
|
||
- **Clear operations** at `downloads.py:5976` - Cleanup and organization
|
||
|
||
## Spotify Matching System Implementation
|
||
|
||
*Reference: [spotify_matching_spec.md](spotify_matching_spec.md)*
|
||
|
||
### Core Matching Flow
|
||
|
||
The Spotify matching system provides intelligent artist matching and metadata enhancement:
|
||
|
||
1. **Artist Matching Modal**: Elegant interface with auto-suggestions and manual search
|
||
2. **Confidence Scoring**: Percentage-based matching using multiple criteria
|
||
3. **Metadata Enhancement**: Spotify API data for accurate tagging
|
||
4. **Folder Organization**: Automatic structure creation based on album detection
|
||
|
||
### Key Implementation Areas
|
||
|
||
#### Modal Interface Components
|
||
- **Auto-matching Section**: Top 5 likely artists with confidence percentages
|
||
- **Manual Search Section**: User-driven artist search with similar result display
|
||
- **Artist Selection**: Click-to-select functionality triggering download process
|
||
|
||
#### Matching Algorithm Enhancement
|
||
```python
|
||
# Core matching function for artist suggestions
|
||
def generate_artist_suggestions(track_query: str, limit: int = 5) -> List[ArtistMatch]:
|
||
"""
|
||
Generate top artist matches for a given track query
|
||
Returns list of ArtistMatch objects with confidence scores
|
||
"""
|
||
# Implementation leverages existing MusicMatchingEngine functions
|
||
# - normalize_string() for query processing
|
||
# - similarity_score() for confidence calculation
|
||
# - SpotifyClient.search_tracks() for candidate retrieval
|
||
```
|
||
|
||
#### Folder Structure Management
|
||
The system implements intelligent folder organization:
|
||
|
||
**Single Track Structure:**
|
||
```
|
||
Transfer/
|
||
|