Skip to content

Commit

Permalink
Split AudioStationClient to multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
geloczi committed Dec 19, 2021
1 parent 01357e8 commit c39ccf9
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 270 deletions.
43 changes: 43 additions & 0 deletions SynologyDotNet.AudioStation/AudioStationClient.Album.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SynologyDotNet.AudioStation.Model;
using SynologyDotNet.Core.Model;
using SynologyDotNet.Core.Responses;

namespace SynologyDotNet.AudioStation
{
public partial class AudioStationClient
{
/// <summary>
/// List albums
/// </summary>
/// <param name="limit">Maximum number of items to return</param>
/// <param name="offset">Start position in the list (use it for paging)</param>
/// <param name="artist">Filter by artist name</param>
/// <param name="queryParameters">Filter parameters</param>
/// <returns></returns>
public async Task<ApiListRessponse<AlbumList>> ListAlbumsAsync(int limit, int offset, string artist = null, params (AlbumQueryParameters, object)[] queryParameters)
{
var args = new List<(string, object)>(queryParameters.Select(f => (f.Item1.ToString(), f.Item2)));
args.Add(GetLibraryArg());
if (!string.IsNullOrWhiteSpace(artist))
args.Add(("artist", artist));

return await Client.QueryListAsync<ApiListRessponse<AlbumList>>(SYNO_AudioStation_Album, "list", limit, offset, args.ToArray());
}

/// <summary>
/// Download album cover image
/// </summary>
/// <param name="artist">Artist name</param>
/// <param name="album">Album title</param>
/// <returns></returns>
public async Task<ByteArrayData> GetAlbumCoverAsync(string artist, string album)
{
return await Client.QueryByteArrayAsync(SYNO_AudioStation_Cover, "getcover",
("album_name", album),
("album_artist_name", artist));
}
}
}
33 changes: 33 additions & 0 deletions SynologyDotNet.AudioStation/AudioStationClient.Artist.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading.Tasks;
using SynologyDotNet.AudioStation.Model;
using SynologyDotNet.Core.Model;
using SynologyDotNet.Core.Responses;

namespace SynologyDotNet.AudioStation
{
public partial class AudioStationClient
{
/// <summary>
/// List artists
/// </summary>
/// <param name="limit">Maximum number of items to return</param>
/// <param name="offset">Start position in the list (use it for paging)</param>
/// <returns></returns>
public async Task<ApiListRessponse<ArtistList>> ListArtistsAsync(int limit, int offset)
{
return await Client.QueryListAsync<ApiListRessponse<ArtistList>>(SYNO_AudioStation_Artist, "list", limit, offset, GetLibraryArg()); //personal
}

/// <summary>
/// Download artist cover image
/// </summary>
/// <param name="artist">Artist name</param>
/// <returns></returns>
public async Task<ByteArrayData> GetArtistCoverAsync(string artist)
{
return await Client.QueryByteArrayAsync(SYNO_AudioStation_Cover, "getcover",
GetLibraryArg(),
("artist_name", artist));
}
}
}
40 changes: 40 additions & 0 deletions SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Linq;
using System.Threading.Tasks;
using SynologyDotNet.AudioStation.Model;
using SynologyDotNet.Core.Responses;

namespace SynologyDotNet.AudioStation
{
public partial class AudioStationClient
{
/// <summary>
/// Lists the playlists.
/// </summary>
/// <param name="limit">The limit.</param>
/// <param name="offset">The offset.</param>
/// <returns></returns>
public async Task<ApiListRessponse<PlaylistList>> ListPlaylistsAsync(int limit, int offset)
{
var result = await Client.QueryListAsync<ApiListRessponse<PlaylistList>>(SYNO_AudioStation_Playlist, "list", limit, offset,
("library", "all"));
return result;
}

/// <summary>
/// Gets the songs on a specific playlist.
/// </summary>
/// <param name="limit">The limit.</param>
/// <param name="offset">The offset.</param>
/// <param name="id">The identifier.</param>
/// <returns></returns>
public async Task<ApiDataResponse<Playlist>> GetPlaylistAsync(int limit, int offset, string id)
{
var result = await Client.QueryListAsync<ApiListRessponse<PlaylistList>>(SYNO_AudioStation_Playlist, "getinfo", limit, offset,
("library", "all"),
("id", id),
("additional", "songs") //("additional", "songs_song_tag,songs_song_audio,songs_song_rating,sharing_info")
);
return new ApiDataResponse<Playlist>(result, result.Data?.playlists?.FirstOrDefault() ?? default);
}
}
}
136 changes: 136 additions & 0 deletions SynologyDotNet.AudioStation/AudioStationClient.Song.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SynologyDotNet.AudioStation.Model;
using SynologyDotNet.Core.Helpers;
using SynologyDotNet.Core.Model;
using SynologyDotNet.Core.Responses;

namespace SynologyDotNet.AudioStation
{
public partial class AudioStationClient
{
/// <summary>
/// List songs
/// </summary>
/// <param name="limit">Maximum number of items to return</param>
/// <param name="offset">Start position in the list (use it for paging)</param>
/// <param name="additional">Additional filds to load</param>
/// <param name="queryParameters">Filter parameters</param>
/// <returns></returns>
public async Task<ApiListRessponse<SongList>> ListSongsAsync(int limit, int offset, SongQueryAdditional additional, params (SongQueryParameters, object)[] queryParameters)
{
var args = new List<(string, object)>(queryParameters.Select(f => (f.Item1.ToString(), f.Item2)));
args.Add(GetLibraryArg());
if (additional != SongQueryAdditional.None)
{
args.Add(("additional", string.Join(",", (new[] {
SongQueryAdditional.song_audio,
SongQueryAdditional.song_rating,
SongQueryAdditional.song_tag
})
.Where(x => additional.HasFlag(x))
.Select(x => x.ToString()))));
}
var result = await Client.QueryListAsync<ApiListRessponse<SongList>>(SYNO_AudioStation_Song, "list", limit, offset, args.ToArray());
return result;
}

/// <summary>
/// Gets a song by ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ApiListRessponse<SongList>> GetSongByIdAsync(string id)
{
var args = new List<(string, object)>();
args.Add(("id", id));
args.Add(("additional", "song_tag, song_audio, song_rating")); // request detailed song info
var result = await Client.QueryObjectAsync<ApiListRessponse<SongList>>(SYNO_AudioStation_Song, "getinfo", args.ToArray());
return result;
}

/// <summary>
/// Set song rating
/// </summary>
/// <param name="songId"></param>
/// <param name="rating">Value from 0 to 5</param>
/// <returns></returns>
public async Task<ApiResponse> RateSongAsync(string songId, int rating)
{
if (rating < 0 || rating > 5)
throw new ArgumentOutOfRangeException(nameof(rating), "Value range: 0 - 5");
var result = await Client.QueryObjectAsync<ApiResponse>(SYNO_AudioStation_Song, "setrating",
("id", songId),
("rating", rating));
return result;
}

/// <summary>
/// Download the song from the server.
/// </summary>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="transcode">Transcode method</param>
/// <param name="songId">ID of the song to download</param>
/// <param name="positionSeconds">Start position in seconds</param>
/// <param name="readStreamAction">The action called to download the song bytes.</param>
/// <returns></returns>
public async Task StreamSongAsync(
CancellationToken cancellationToken,
TranscodeMode transcode,
string songId,
double positionSeconds,
Action<StreamResult> readStreamAction)
{
var req = CreateSongStreamRequest(SYNO_AudioStation_Stream, transcode, songId, positionSeconds);
await Client.QueryStreamAsync(req, readStreamAction, cancellationToken);
}

private RequestBuilder CreateSongStreamRequest(string apiName, TranscodeMode transcode, string songId, double positionInSeconds)
{
string method = "stream";
string subEndpoint = null;
string format = null;
switch (transcode)
{
case TranscodeMode.MP3_128Kbps:
case TranscodeMode.MP3_192Kbps:
case TranscodeMode.MP3_256Kbps:
case TranscodeMode.MP3_320Kbps:
subEndpoint = "/0.mp3";
method = "transcode";
format = "mp3";
break;
case TranscodeMode.WAV:
subEndpoint = "/0.wav";
method = "transcode";
format = "wav";
break;
}
var req = new RequestBuilder(Client.GetApiInfo(apiName), subEndpoint).Method(method).SetParam("id", songId);
if (!string.IsNullOrEmpty(format))
req.SetParam("format", format);
if (positionInSeconds > 0)
req.SetParam("position", Math.Round(positionInSeconds, 4).ToString(CultureInfo.InvariantCulture));
switch (transcode)
{
case TranscodeMode.MP3_128Kbps:
req.SetParam("bitrate", "128000");
break;
case TranscodeMode.MP3_192Kbps:
req.SetParam("bitrate", "192000");
break;
case TranscodeMode.MP3_256Kbps:
req.SetParam("bitrate", "256000");
break;
case TranscodeMode.MP3_320Kbps:
req.SetParam("bitrate", "320000");
break;
}
return req;
}
}
}
48 changes: 48 additions & 0 deletions SynologyDotNet.AudioStation/AudioStationClient.Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SynologyDotNet.AudioStation.Model;
using SynologyDotNet.Core.Helpers;
using SynologyDotNet.Core.Responses;

namespace SynologyDotNet.AudioStation
{
public partial class AudioStationClient
{
/// <summary>
/// Query song tags
/// </summary>
/// <param name="paths">The internal path of the music file. Must contain forward slaashes '/'</param>
/// <returns></returns>
public async Task<FileTags> GetSongFileTags(params string[] paths)
{
if (paths?.Any() != true)
throw new ArgumentNullException(nameof(paths));
if (paths.Any(p => p.Contains("\\")))
throw new ArgumentException("Invalid path. Path must contain forward slashes '/', not back-slashes '\\'");
var req = new RequestBuilder().SetEndpoint(TagEditorEndpoint).Action("load");
req["audioInfos"] = JsonConvert.SerializeObject(paths.Select(p => new { path = p }));
req["requestFrom"] = string.Empty;
var result = await Client.QueryObjectAsync<FileTags>(req);
return result;
}

/// <summary>
/// Batch edit music file tags
/// </summary>
/// <param name="change"></param>
/// <returns></returns>
public async Task<ApiResponse> SetSongFileTags(FileTagChange change)
{
if (change?.AudioInfos?.Any() != true)
throw new ArgumentNullException($"{nameof(change)}.{nameof(change.AudioInfos)}");

var req = new RequestBuilder().SetEndpoint(TagEditorEndpoint).Action("apply");
req["data"] = JsonConvert.SerializeObject(new object[] { change });
var result = await Client.QueryObjectAsync<ApiResponse>(req);
return result;
}

}
}
Loading

0 comments on commit c39ccf9

Please sign in to comment.