Add country metadata extraction and assignment features

- Updated `README.md` to include instructions for setting up the TMDB API key and new admin endpoints for managing country metadata.
- Implemented `/admin/missing-countries` endpoint to list media items without country metadata, with filtering options for source and media type.
- Added `/admin/assign-country` endpoint to manually assign a country code to a media item.
- Enhanced country extraction logic in `sync.py` to utilize TMDB and MusicBrainz APIs for automatic country retrieval based on available metadata.
- Updated configuration in `config.py` to include optional TMDB API key setting.
- Improved error handling and logging for country extraction failures.
- Ensured that country data is stored and utilized during media item synchronization across Radarr, Sonarr, and Lidarr.
This commit is contained in:
Danilo Reyes
2025-12-28 21:47:03 -06:00
parent 6cffbef8c6
commit 335a53ee62
4 changed files with 337 additions and 22 deletions

View File

@@ -49,6 +49,9 @@ RADARR_API_KEY=your_radarr_api_key
LIDARR_API_KEY=your_lidarr_api_key
PORT=8080
HOST=0.0.0.0 # Use 127.0.0.1 for localhost only
# Optional: External APIs for country data
TMDB_API_KEY=your_tmdb_api_key # Get from https://www.themoviedb.org/settings/api
```
3. (Optional) Set up frontend environment variables (create `.env.local` in `frontend/`):
@@ -220,6 +223,8 @@ The service will be available at `http://0.0.0.0:8080` (or your server's IP addr
### Admin
- `POST /admin/sync` - Trigger sync from all *arr instances (requires admin token if configured)
- `GET /admin/missing-countries?source_kind=sonarr&media_type=show&limit=100` - List items without country metadata
- `POST /admin/assign-country?item_id=<uuid>&country_code=US` - Manually assign country to an item
## Database Schema
@@ -233,13 +238,71 @@ The application creates a `moviemap` schema in the `jawz` database with the foll
## Country Extraction
The sync process extracts country information from *arr metadata:
The sync process extracts country information using multiple methods:
- **Radarr**: Uses `productionCountries` from movie metadata
- **Sonarr**: Uses `originCountry` from series metadata (if available)
- **Lidarr**: Uses `country` field from artist metadata
### Automatic Extraction
If country information is not available, the item is stored without a country association (excluded from map visualization).
- **Radarr (Movies)**:
- First tries `productionCountries` from Radarr metadata
- Falls back to TMDB API (requires `TMDB_API_KEY` env var) using `tmdbId`
- **Sonarr (TV Shows)**:
- First tries `seriesMetadata.originCountry` from Sonarr metadata
- Falls back to TMDB API (requires `TMDB_API_KEY` env var) using `tmdbId`
- **Lidarr (Music)**:
- First tries `country` field from Lidarr metadata
- Falls back to MusicBrainz API (no API key required) using `foreignArtistId` (MBID)
### External API Setup
**TMDB API (for Movies & TV Shows):**
1. Get a free API key from https://www.themoviedb.org/settings/api
2. Set environment variable: `TMDB_API_KEY=your_api_key_here`
3. Re-run sync to fetch country data
**MusicBrainz API (for Music):**
- No API key required (uses public API)
- Automatically used if `foreignArtistId` (MusicBrainz ID) is available in Lidarr
### Manual Assignment
If automatic extraction fails, you can manually assign countries:
1. **View missing countries:**
```bash
curl http://127.0.0.1:8888/admin/missing-countries?source_kind=sonarr&limit=50
```
2. **Assign country manually:**
```bash
curl -X POST "http://127.0.0.1:8888/admin/assign-country?item_id=<uuid>&country_code=US"
```
Items without country information are stored but excluded from map visualization until a country is assigned.
## Implementation Status
### ✅ Completed Features
- ✅ Project scaffolding (FastAPI backend + React frontend)
- ✅ Database schema and migrations (PostgreSQL)
- ✅ *arr sync integration (Radarr, Sonarr, Lidarr)
- ✅ Collection Map UI (View 1) with filters
- ✅ Watched Map UI (View 2) with manual tracking
- ✅ NixOS module and systemd deployment
- ✅ TMDB API integration for movies and TV shows
- ✅ MusicBrainz API integration for music
- ✅ Manual country assignment feature
- ✅ Missing metadata admin view
### 🔄 Future Enhancements (Optional)
- Batch country assignment UI
- Country extraction from file paths/metadata
- Export/import functionality
- Statistics and analytics
- Multi-user support
## License