The Settings page gets a laut.fm card: a switch, the live access data from Radioadmin (server, port, mountpoint, user, password) and the station id and Radioadmin token for the library. Password and token are written to settings.json encrypted with DPAPI for the Windows user. While laut.fm is on, the Schedule page shows a Live button next to "Jetzt starten". It logs in at the Icecast server with PUT and sends a copy of the broadcast mix as MP3 at 128 kbit/s, encoded by Windows Media Foundation. ProgramOutput hands that copy out through a live feed. Each item that starts sets the stream title; ad items send START_AD_BREAK at most once every 15 minutes. LautFmProvider searches the station's tracks through the Radioadmin API and the Import page lists it while laut.fm is on, so laut.fm tracks are imported into the same library as local songs. Providers can now hide themselves with IsAvailable. Nothing was tested against a real laut.fm account; the tests use a local fake server and fake HTTP answers. Refs #15 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
2.1 KiB
C#
51 lines
2.1 KiB
C#
using System.IO;
|
|
using Radiosender.Models;
|
|
using Radiosender.Services;
|
|
|
|
namespace Radiosender.Controllers;
|
|
|
|
/// <summary>Searches the registered music providers and imports their results into the library.</summary>
|
|
public class OnlineImportController(IEnumerable<IMusicProvider> providers, TrackDownloader downloader, SongRepository songs)
|
|
{
|
|
private readonly IReadOnlyList<IMusicProvider> _providers = providers.ToList();
|
|
|
|
/// <summary>The providers registered in <c>App</c> that are available now, in registration order.</summary>
|
|
public IReadOnlyList<IMusicProvider> Providers => _providers.Where(provider => provider.IsAvailable).ToList();
|
|
|
|
/// <summary>Ids of the provider results that are already in the library.</summary>
|
|
public IReadOnlySet<string> ImportedIds() => songs.GetExternalIds();
|
|
|
|
/// <summary>Downloads the track and stores it as a song with its license and attribution.</summary>
|
|
/// <exception cref="MusicProviderException">The track cannot be imported or the download failed.</exception>
|
|
public async Task<Song> ImportAsync(MusicTrack track, CancellationToken cancellationToken = default)
|
|
{
|
|
if (track.Duration is not { } duration)
|
|
throw new MusicProviderException("Dieses Ergebnis hat keine Dauer und kann nicht importiert werden.");
|
|
if (track.Id.Length > 0 && ImportedIds().Contains(track.Id))
|
|
throw new MusicProviderException($"\"{track.Title}\" ist bereits in der Bibliothek.");
|
|
|
|
var path = await downloader.DownloadAsync(track, cancellationToken);
|
|
var song = new Song
|
|
{
|
|
Title = track.Title,
|
|
Artist = string.IsNullOrWhiteSpace(track.Creator) ? "Unknown" : track.Creator,
|
|
Duration = duration,
|
|
FilePath = path,
|
|
Source = SongSource.Online,
|
|
License = track.License,
|
|
Attribution = track.Attribution,
|
|
ExternalId = track.Id.Length > 0 ? track.Id : null,
|
|
};
|
|
try
|
|
{
|
|
songs.Add(song);
|
|
}
|
|
catch
|
|
{
|
|
File.Delete(path); // no orphaned file without a database entry
|
|
throw;
|
|
}
|
|
return song;
|
|
}
|
|
}
|