feat: add Openverse audio search and import

Add an Openverse page that searches free audio through the anonymous
Openverse API, previews a result by streaming its URL with MediaPlayer
and imports it into the library.

OpenverseClient uses HttpClient with a timeout and a generic User-Agent.
It downloads only the result's https URL, limits files to 50 MB, never
overwrites an existing file and deletes partial files on failure.
Timeouts, HTTP 429 and other errors become readable messages. Imported
songs get Source = Openverse plus license and attribution from the API.
Results without a duration cannot be imported, because songs need one.

Refs #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
fraujulianandClaude Opus 5.5 committed 2026-11-23 10:26:11 +01:00
1 parent 89b7959404
commit fc461b74cb
7 files changed
+620

No files matched your search

@@ -0,0 +1,62 @@
<Page x:Class="Radiosender.Views.Pages.OpenversePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Title="Openverse"
Unloaded="OnUnloaded">
<Grid Margin="24">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ui:TextBlock Grid.Row="0" FontTypography="Title" Text="Openverse" />
<Grid Grid.Row="1" Margin="0,12,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBox x:Name="QueryBox"
PlaceholderText="Search free audio, for example &quot;jazz piano&quot;"
AutomationProperties.Name="Search term"
KeyDown="OnQueryKeyDown" />
<ui:Button x:Name="SearchButton" Grid.Column="1" Margin="8,0,0,0"
Appearance="Primary" Content="Search"
Icon="{ui:SymbolIcon Search24}"
Click="OnSearch" />
</Grid>
<ui:InfoBar x:Name="Message" Grid.Row="2" Margin="0,12,0,0" IsClosable="True" />
<ui:ListView x:Name="Results" Grid.Row="3" Margin="0,12,0,0"
SelectionMode="Single"
AutomationProperties.Name="Search results"
SelectionChanged="OnSelectionChanged">
<ui:ListView.View>
<ui:GridView>
<GridViewColumn Header="Title" Width="320" DisplayMemberBinding="{Binding Title}" />
<GridViewColumn Header="Creator" Width="200" DisplayMemberBinding="{Binding Creator}" />
<GridViewColumn Header="Duration" Width="90" DisplayMemberBinding="{Binding DurationText}" />
<GridViewColumn Header="License" Width="150" DisplayMemberBinding="{Binding LicenseName}" />
</ui:GridView>
</ui:ListView.View>
</ui:ListView>
<StackPanel Grid.Row="4" Margin="0,12,0,0" Orientation="Horizontal">
<ui:Button x:Name="PreviewButton" Content="Preview" Icon="{ui:SymbolIcon Play24}"
IsEnabled="False" Click="OnPreview" />
<ui:Button x:Name="StopButton" Margin="8,0,0,0" Content="Stop" Icon="{ui:SymbolIcon Stop24}"
IsEnabled="False" Click="OnStop" />
<ui:Button x:Name="ImportButton" Margin="8,0,0,0" Appearance="Primary"
Content="Import to library" Icon="{ui:SymbolIcon ArrowDownload24}"
IsEnabled="False" Click="OnImport" />
<ui:TextBlock x:Name="ImportHint" Margin="12,0,0,0" VerticalAlignment="Center"
Visibility="Collapsed"
Text="This result has no duration and cannot be imported." />
</StackPanel>
</Grid>
</Page>
@@ -0,0 +1,128 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Radiosender.Controllers;
using Radiosender.Models;
using Radiosender.Services;
using Wpf.Ui.Controls;
namespace Radiosender.Views.Pages;
public partial class OpenversePage : Page
{
// One controller (and HttpClient) for the app lifetime; created on first use, after App.Songs exists.
private static OpenverseController? _controller;
private static OpenverseController Controller => _controller ??= new(OpenverseClient.CreateDefault(), App.Songs);
private readonly MediaPlayer _preview = new();
private bool _busy;
public OpenversePage()
{
InitializeComponent();
_preview.MediaEnded += (_, _) => OnStop(this, new RoutedEventArgs());
_preview.MediaFailed += (_, e) =>
{
OnStop(this, new RoutedEventArgs());
Show(InfoBarSeverity.Error, "Preview failed", e.ErrorException.Message);
};
}
private OpenverseTrack? Selected => Results.SelectedItem as OpenverseTrack;
private void OnQueryKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
OnSearch(sender, e);
}
private async void OnSearch(object sender, RoutedEventArgs e)
{
var query = QueryBox.Text.Trim();
if (query.Length == 0 || _busy)
return;
SetBusy(true);
Message.IsOpen = false;
try
{
var tracks = await Controller.SearchAsync(query);
Results.ItemsSource = tracks;
if (tracks.Count == 0)
Show(InfoBarSeverity.Informational, "No results", $"Openverse found no audio for \"{query}\".");
}
catch (OpenverseException ex)
{
Show(InfoBarSeverity.Error, "Search failed", ex.Message);
}
finally
{
SetBusy(false);
}
}
private void OnPreview(object sender, RoutedEventArgs e)
{
if (Selected is not { } track || !Uri.TryCreate(track.Url, UriKind.Absolute, out var uri) || uri.Scheme != Uri.UriSchemeHttps)
return;
_preview.Open(uri); // streams the file, nothing is saved
_preview.Play();
StopButton.IsEnabled = true;
}
private void OnStop(object sender, RoutedEventArgs e)
{
_preview.Stop();
_preview.Close();
StopButton.IsEnabled = false;
}
private async void OnImport(object sender, RoutedEventArgs e)
{
if (Selected is not { CanImport: true } track || _busy)
return;
SetBusy(true);
Message.IsOpen = false;
try
{
var song = await Controller.ImportAsync(track);
Show(InfoBarSeverity.Success, "Imported", $"\"{song.Title}\" was added to the library.");
}
catch (Exception ex) when (ex is OpenverseException or Microsoft.Data.Sqlite.SqliteException)
{
Show(InfoBarSeverity.Error, "Import failed", ex.Message);
}
finally
{
SetBusy(false);
}
}
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) => UpdateButtons();
private void OnUnloaded(object sender, RoutedEventArgs e) => OnStop(sender, e);
private void SetBusy(bool busy)
{
_busy = busy;
SearchButton.IsEnabled = !busy;
UpdateButtons();
}
private void UpdateButtons()
{
PreviewButton.IsEnabled = Selected is not null;
ImportButton.IsEnabled = !_busy && Selected is { CanImport: true };
ImportHint.Visibility = Selected is { CanImport: false } ? Visibility.Visible : Visibility.Collapsed;
}
private void Show(InfoBarSeverity severity, string title, string message)
{
Message.Severity = severity;
Message.Title = title;
Message.Message = message;
Message.IsOpen = true;
}
}