roadie/Roadie.Api.Library/Models/ThirdPartyApi/Subsonic/Request.cs

306 lines
10 KiB
C#
Raw Normal View History

2018-11-23 04:18:48 +00:00
using Newtonsoft.Json;
using Roadie.Library.Extensions;
2019-07-03 16:21:29 +00:00
using Roadie.Library.Models.Pagination;
2018-11-20 04:47:12 +00:00
using Roadie.Library.Utility;
using System;
namespace Roadie.Library.Models.ThirdPartyApi.Subsonic
{
[Serializable]
public class Request
{
2018-11-20 04:47:12 +00:00
public const string ArtistIdIdentifier = "A:";
public const string CollectionIdentifier = "C:";
2018-12-16 23:37:19 +00:00
public const short MaxPageSize = 100;
2018-11-21 06:34:53 +00:00
public const string PlaylistdIdentifier = "P:";
2018-11-20 04:47:12 +00:00
public const string ReleaseIdIdentifier = "R:";
public const string TrackIdIdentifier = "T:";
2018-11-21 06:34:53 +00:00
public Guid? ArtistId
{
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(id)) return null;
if (id.StartsWith(ArtistIdIdentifier)) return SafeParser.ToGuid(id);
2018-11-21 06:34:53 +00:00
return null;
}
}
2018-11-20 04:47:12 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// A unique string identifying the client application.
/// </summary>
public string c { get; set; }
/// <summary>
2019-07-03 16:21:29 +00:00
/// <seealso cref="f" />
/// </summary>
public string callback { get; set; }
2018-11-21 06:34:53 +00:00
public Guid? CollectionId
{
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(id)) return null;
if (id.StartsWith(CollectionIdentifier)) return SafeParser.ToGuid(id);
2018-11-21 06:34:53 +00:00
return null;
}
}
/// <summary>
2019-07-03 16:21:29 +00:00
/// Request data to be returned in this format. Supported values are "xml", "json" (since 1.4.0) and "jsonp" (since
/// 1.6.0). If using jsonp, specify name of javascript callback function using a callback parameter.
/// </summary>
public string f { get; set; }
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// A string which uniquely identifies the music folder. Obtained by calls to getIndexes or getMusicDirectory.
2018-11-21 06:34:53 +00:00
/// </summary>
public string id { get; set; }
2018-11-25 23:34:17 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Some operations have an array of ids, see savePlayQue
2018-11-25 23:34:17 +00:00
/// </summary>
public string[] ids { get; set; }
2019-07-03 16:21:29 +00:00
public bool IsCallbackSet => !string.IsNullOrEmpty(callback);
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Request data to be returned in this format. Supported values are "xml", "json" (since 1.4.0) and "jsonp" (since
/// 1.6.0). If using jsonp, specify name of javascript callback function using a callback parameter.
2018-11-21 06:34:53 +00:00
/// </summary>
public bool IsJSONRequest
{
2018-11-21 06:34:53 +00:00
// Default should be false (XML)
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(f)) return false;
return f.ToLower().StartsWith("j");
}
}
/// <summary>
2019-07-03 16:21:29 +00:00
/// The password, either in clear text or hex-encoded with a "enc:" prefix. Since 1.13.0 this should only be used for
/// testing purposes.
/// </summary>
2018-11-23 04:18:48 +00:00
[JsonIgnore]
public string p { get; set; }
2018-11-23 04:18:48 +00:00
[JsonIgnore]
public string Password
{
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(p)) return null;
if (p.StartsWith("enc:")) return p.ToLower().Replace("enc:", "").FromHexString();
return p;
}
}
2018-11-21 06:34:53 +00:00
public Guid? PlaylistId
2018-11-20 04:47:12 +00:00
{
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(id)) return null;
if (id.StartsWith(PlaylistdIdentifier)) return SafeParser.ToGuid(id);
2018-11-20 04:47:12 +00:00
return null;
}
}
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Search query.
2018-11-21 06:34:53 +00:00
/// </summary>
public string Query { get; set; }
2018-11-20 04:47:12 +00:00
public Guid? ReleaseId
{
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(id)) return null;
if (id.StartsWith(ReleaseIdIdentifier)) return SafeParser.ToGuid(id);
2018-11-20 04:47:12 +00:00
return null;
}
}
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// A random string ("salt") used as input for computing the password hash. See below for details.
2018-11-21 06:34:53 +00:00
/// </summary>
public string s { get; set; }
/// <summary>
2019-07-03 16:21:29 +00:00
/// Whether this is a "submission" or a "now playing" notification.
/// </summary>
public string submission { get; set; }
/// <summary>
/// The authentication token computed as md5(password + salt). See below for details
2018-11-21 06:34:53 +00:00
/// </summary>
public string t { get; set; }
2018-11-20 04:47:12 +00:00
2019-07-03 16:21:29 +00:00
/// <summary>
/// The time (in milliseconds since 1 Jan 1970) at which the song was listened to.
/// </summary>
public string time { get; set; }
2018-11-20 14:36:07 +00:00
public Guid? TrackId
{
get
{
2019-07-03 16:21:29 +00:00
if (string.IsNullOrEmpty(id)) return null;
if (id.StartsWith(TrackIdIdentifier)) return SafeParser.ToGuid(id);
2018-11-20 14:36:07 +00:00
return null;
}
}
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// The username
2018-11-21 06:34:53 +00:00
/// </summary>
public string u { get; set; }
/// <summary>
2019-07-03 16:21:29 +00:00
/// The protocol version implemented by the client, i.e., the version of the subsonic-rest-api.xsd schema used (see
/// below).
2018-11-21 06:34:53 +00:00
/// </summary>
public string v { get; set; }
2018-11-20 14:36:07 +00:00
#region Paging and List Related
/// <summary>
2019-07-03 16:21:29 +00:00
/// Maximum number of albums to return.
2018-11-20 14:36:07 +00:00
/// </summary>
2018-12-16 23:37:19 +00:00
public short? AlbumCount { get; set; }
2018-11-20 14:36:07 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Search result offset for albums. Used for paging.
2018-11-20 14:36:07 +00:00
/// </summary>
2018-11-21 06:34:53 +00:00
public int? AlbumOffset { get; set; }
2018-11-20 14:36:07 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Maximum number of artists to return.
2018-11-20 14:36:07 +00:00
/// </summary>
2018-12-16 23:37:19 +00:00
public short? ArtistCount { get; set; }
2018-11-20 14:36:07 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// The artist name.
/// <see cref="getTopSongs" />
2018-11-20 14:36:07 +00:00
/// </summary>
2019-07-03 16:21:29 +00:00
public string ArtistName { get; set; }
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Search result offset for artists. Used for paging.
/// </summary>
2019-07-03 16:21:29 +00:00
public int? ArtistOffset { get; set; }
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// The first year in the range. If fromYear > toYear a reverse chronological list is returned.
2018-11-21 06:34:53 +00:00
/// </summary>
public int? FromYear { get; set; }
2018-11-20 14:36:07 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// The name of the genre, e.g., "Rock".
2018-11-20 14:36:07 +00:00
/// </summary>
public string Genre { get; set; }
2019-07-03 16:21:29 +00:00
// When adding a chat message this is the message to add
public string Message { get; set; }
2018-11-20 14:36:07 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Only return albums in the music folder with the given ID. See getMusicFolders.
2018-11-20 14:36:07 +00:00
/// </summary>
public int? MusicFolderId { get; set; }
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// The list offset. Useful if you for example want to page through the list of newest albums.
2018-11-21 06:34:53 +00:00
/// </summary>
public int? Offset { get; set; }
2018-11-20 14:36:07 +00:00
2019-07-03 16:21:29 +00:00
public PagedRequest PagedRequest
2018-11-20 14:36:07 +00:00
{
get
{
2019-07-03 16:21:29 +00:00
var limit = Size ?? MaxPageSize;
2019-07-07 16:55:55 +00:00
var page = Offset > 0 ? (int)Math.Ceiling(Offset.Value / (decimal)limit) + 1 : 1;
2019-07-03 16:21:29 +00:00
var pagedRequest = new PagedRequest();
switch (Type)
2018-11-20 14:36:07 +00:00
{
2018-11-21 06:34:53 +00:00
case ListType.Newest:
pagedRequest.Sort = "CreatedDate";
pagedRequest.Order = "DESC";
break;
case ListType.Highest:
pagedRequest.Sort = "Rating";
pagedRequest.Order = "DESC";
pagedRequest.FilterRatedOnly = true;
break;
case ListType.Frequent:
pagedRequest.Sort = "TrackPlayedCount";
pagedRequest.Order = "DESC";
break;
case ListType.Recent:
pagedRequest.Sort = "LastPlayed";
pagedRequest.Order = "DESC";
break;
case ListType.AlphabeticalByName:
pagedRequest.Sort = "Release.Text";
pagedRequest.Order = "ASC";
break;
case ListType.AlphabeticalByArtist:
pagedRequest.Sort = "Artist.Text";
pagedRequest.Order = "ASC";
break;
case ListType.Starred:
pagedRequest.FilterRatedOnly = true;
pagedRequest.Sort = "Rating";
pagedRequest.Order = "DESC";
break;
case ListType.ByGenre:
2019-07-03 16:21:29 +00:00
pagedRequest.FilterByGenre = Genre;
2018-11-21 06:34:53 +00:00
break;
case ListType.ByYear:
2019-07-03 16:21:29 +00:00
pagedRequest.FilterFromYear = FromYear;
pagedRequest.FilterToYear = ToYear;
2018-11-21 06:34:53 +00:00
pagedRequest.Sort = "ReleaseDate";
2019-07-03 16:21:29 +00:00
pagedRequest.Order = FromYear > ToYear ? "DESC" : "ASC";
2018-11-21 06:34:53 +00:00
break;
2018-11-20 14:36:07 +00:00
}
2019-07-03 16:21:29 +00:00
2018-11-21 06:34:53 +00:00
pagedRequest.Limit = limit;
pagedRequest.Page = page;
return pagedRequest;
2018-11-20 14:36:07 +00:00
}
}
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// The number of albums to return. Max 500.
/// <see>Various *Count properties depending on objects being searched and client version.</see>
/// <remark>Something this value is posted as 'count' versus 'size'</remark>
2018-11-21 06:34:53 +00:00
/// </summary>
2018-12-16 23:37:19 +00:00
public short? Size { get; set; }
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Maximum number of songs to return.
2018-11-21 06:34:53 +00:00
/// </summary>
2018-12-16 23:37:19 +00:00
public short? SongCount { get; set; }
2018-11-21 06:34:53 +00:00
/// <summary>
2019-07-03 16:21:29 +00:00
/// Search result offset for songs. Used for paging.
2018-11-21 06:34:53 +00:00
/// </summary>
public int? SongOffset { get; set; }
/// <summary>
2019-07-03 16:21:29 +00:00
/// The last year in the range.
2018-11-21 06:34:53 +00:00
/// </summary>
public int? ToYear { get; set; }
public ListType Type { get; set; }
2018-11-20 04:47:12 +00:00
2018-11-21 06:34:53 +00:00
#endregion Paging and List Related
}
}