/// <summary> /// Returns an indexed structure of all artists. /// </summary> /// <param name="connection"></param> /// <param name="musicFolderId">Required: No; If specified, only return artists in the music folder with the given ID.</param> /// <param name="ifModifiedSince">Required: No; If specified, only return a result if the artist collection has changed since the given time.</param> /// <returns>Dictionary, Key = Artist and Value = id</returns> public static List<SubsonicItem> GetIndexes(ISubsonicConnection connection, string musicFolderId, string ifModifiedSince) { // Load the parameters if provided Dictionary<string, string> parameters = new Dictionary<string, string>(); if (!string.IsNullOrEmpty(musicFolderId)) { parameters.Add("musicFolderId", musicFolderId); } if (!string.IsNullOrEmpty(ifModifiedSince)) { parameters.Add("ifModifiedSince", ifModifiedSince); } // Make the request string result = connection.GetResponse("getIndexes", parameters); // Parse the resulting XML string into an XmlDocument XmlDocument myXml = new XmlDocument(); myXml.LoadXml(result); // Parse the XML document into a List List<SubsonicItem> artists = new List<SubsonicItem>(); if (myXml.ChildNodes[1].Name == "subsonic-response") { if (myXml.ChildNodes[1].FirstChild.Name == "indexes") { for (int i = 0; i < myXml.ChildNodes[1].FirstChild.ChildNodes.Count; i++) { for (int j = 0; j < myXml.ChildNodes[1].FirstChild.ChildNodes[i].ChildNodes.Count; j++) { string artist = myXml.ChildNodes[1].FirstChild.ChildNodes[i].ChildNodes[j].Attributes["name"].Value; string id = myXml.ChildNodes[1].FirstChild.ChildNodes[i].ChildNodes[j].Attributes["id"].Value; artists.Add(new SubsonicItem(artist, id)); } } } } return artists; }
public Stream GetStream(ISubsonicConnection connection) { return Subsonic.StreamSong(connection, this.ID); }
public static Stream StreamSong(ISubsonicConnection connection, string id) { return StreamSong(connection, id, Bitrate.NoPreference); }
/// <summary> /// Performs a search valid for the current version of the subsonic server /// If version is >= 1.4.0 search2 /// Else search /// </summary> /// <param name="connection"></param> /// <param name="query">The Term you want to search for</param> /// <returns>A List of SubsonicItem objects</returns> public static List<SubsonicItem> Search(ISubsonicConnection connection, string query) { Dictionary<string, string> parameters = new Dictionary<string, string>(); Version apiV = new Version(Constants.Subsonic.API_VERSION); Version search2Min = new Version("1.4.0"); string request = ""; // Use search for the server version if (apiV >= search2Min) { request = "search2"; parameters.Add("query", query); } else { request = "search"; parameters.Add("any", query); } // Make the request string result = connection.GetResponse(request, parameters); // Parse the resulting XML string into an XmlDocument XmlDocument myXml = new XmlDocument(); myXml.LoadXml(result); List<SubsonicItem> searchResults = new List<SubsonicItem>(); // Parse the artist if (myXml.ChildNodes[1].Name == "subsonic-response") { if (myXml.ChildNodes[1].FirstChild.Name == "searchResult") { for (int i = 0; i < myXml.ChildNodes[1].FirstChild.ChildNodes.Count; i++) { bool isDir = bool.Parse(myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["isDir"].Value); string title = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["title"].Value; string theId = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["id"].Value; string artist = ""; string album = ""; if (!isDir) { artist = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["artist"].Value; album = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["album"].Value; } SubsonicItem theItem = isDir ? new SubsonicItem(title, theId, SubsonicItemType.Folder, null) : new Song(title, artist, album, theId); searchResults.Add(theItem); } } } return searchResults; }
/// <summary> /// Streams a given music file. (Renamed from request name "stream") /// </summary> /// <param name="connection"></param> /// <param name="id">Required: Yes; A string which uniquely identifies the file to stream. /// Obtained by calls to getMusicDirectory.</param> /// <param name="maxBitRate">Required: No; If specified, the server will attempt to /// limit the bitrate to this value, in kilobits per second. </param> /// <returns></returns> public static Stream StreamSong(ISubsonicConnection connection, string id, Bitrate maxBitRate = Bitrate.NoPreference) { // Reades the id of the song and sets it as a parameter Dictionary<string, string> theParameters = new Dictionary<string, string> {{"id", id}}; if (!maxBitRate.Equals(Bitrate.NoPreference)) { theParameters.Add("maxBitRate", maxBitRate.ToString()); } // Makes the request return connection.GetResponseStream("stream", theParameters); }
/// <summary> /// Returns a list of all SubsonicItems in playlist of given ID /// </summary> /// <param name="connection"></param> /// <param name="playlistId"> /// ID of playlist to be fetched [retreive from GetPlaylists()] /// </param> /// <returns> /// Returns list of SubsonicItems /// </returns> public static List<SubsonicItem> GetPlaylist(ISubsonicConnection connection, string playlistId) { List<SubsonicItem> playlist = new List<SubsonicItem>(); Dictionary<string, string> theParameters = new Dictionary<string, string> {{"id", playlistId}}; string result = connection.GetResponse("getPlaylist", theParameters); /// TODO: Parse result into list return playlist; }
/// <summary> /// Returns a list of all playlists on server /// </summary> public static List<SubsonicItem> GetPlaylists(ISubsonicConnection connection) { List<SubsonicItem> playlists = new List<SubsonicItem>(); Dictionary<string, string> theParameters = new Dictionary<string, string>(); string result = connection.GetResponse("getPlaylists", theParameters); /// TODO: Parse result into list return playlists; }
/// <summary> /// Returns what is currently being played by all users. Takes no extra parameters. /// </summary> public static List<SubsonicItem> GetNowPlaying(ISubsonicConnection connection) { List<SubsonicItem> nowPlaying = new List<SubsonicItem>(); Dictionary<string, string> theParameters = new Dictionary<string, string>(); string result = connection.GetResponse("getNowPlaying", theParameters); /// TODO: Parse result to list return nowPlaying; }
/// <summary> /// Returns a listing of all files in a music directory. Typically used to get list of albums for an artist, or list of songs for an album. /// </summary> /// <param name="connection"></param> /// <param name="id">A string which uniquely identifies the music folder. Obtained by calls to getIndexes or getMusicDirectory.</param> /// <returns>MusicFolder object containing info for the specified directory</returns> public static List<SubsonicItem> GetMusicDirectory(ISubsonicConnection connection, string id) { Dictionary<string, string> theParameters = new Dictionary<string, string> {{"id", id}}; string result = connection.GetResponse("getMusicDirectory", theParameters); if (string.IsNullOrEmpty(result)) { throw new NullReferenceException("resulting xml is null"); } XmlDocument myXml = new XmlDocument(); myXml.LoadXml(result); List<SubsonicItem> theContents = new List<SubsonicItem>(); if (myXml.ChildNodes[1].Name == "subsonic-response") { if (myXml.ChildNodes[1].FirstChild.Name == "directory") { SubsonicItem theParent = new SubsonicItem { Name = myXml.ChildNodes[1].FirstChild.Attributes["name"].Value, ID = myXml.ChildNodes[1].FirstChild.Attributes["id"].Value }; for (int i = 0; i < myXml.ChildNodes[1].FirstChild.ChildNodes.Count; i++) { bool isDir = bool.Parse(myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["isDir"].Value); string title = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["title"].Value; string theId = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["id"].Value; SubsonicItem theItem = new SubsonicItem(title, theId, (isDir ? SubsonicItemType.Folder : SubsonicItemType.Song), theParent); theContents.Add(theItem); } } } return theContents; }
/// <summary> /// Returns a list of SubsonicItems that fall inside the parent object /// </summary> /// <param name="connection"></param> /// <param name="parent"> /// A <see cref="SubsonicItem"/> /// </param> /// <param name="ifModifiedSince"> /// A <see cref="System.String"/> /// </param> /// <returns> /// A <see cref="List<SubsonicItem>"/> /// </returns> public static List<SubsonicItem> GetItemChildren(ISubsonicConnection connection, SubsonicItem parent, string ifModifiedSince) { Dictionary<string, string> parameters = new Dictionary<string, string>(); // Generate the proper request for the parent type string requestType; if (parent.ItemType == SubsonicItemType.Library) { requestType = "getIndexes"; if (parent.ID != "-1") { parameters.Add("musicFolderId", parent.ID); } } else { requestType = "getMusicDirectory"; parameters.Add("id", parent.ID); } // Load the parameters if provided if (!string.IsNullOrEmpty(ifModifiedSince)) { parameters.Add("ifModifiedSince", ifModifiedSince); } // Make the request string result = connection.GetResponse(requestType, parameters); // Parse the resulting XML string into an XmlDocument XmlDocument myXml = new XmlDocument(); myXml.LoadXml(result); // Parse the artist out of the result List<SubsonicItem> children = new List<SubsonicItem>(); if (parent.ItemType == SubsonicItemType.Library) { if (myXml.ChildNodes[1].Name == "subsonic-response") { if (myXml.ChildNodes[1].FirstChild.Name == "indexes") { for (int i = 0; i < myXml.ChildNodes[1].FirstChild.ChildNodes.Count; i++) { // Ignore index name (and entire concept), and move on to parse children for (int j = 0; j < myXml.ChildNodes[1].FirstChild.ChildNodes[i].ChildNodes.Count; j++) { string artist = myXml.ChildNodes[1].FirstChild.ChildNodes[i].ChildNodes[j].Attributes["name"].Value; string id = myXml.ChildNodes[1].FirstChild.ChildNodes[i].ChildNodes[j].Attributes["id"].Value; children.Add(new SubsonicItem(artist, id, SubsonicItemType.Folder, parent)); } } } } } // Parse the directory else if (parent.ItemType == SubsonicItemType.Folder) { if (myXml.ChildNodes[1].Name == "subsonic-response") { if (myXml.ChildNodes[1].FirstChild.Name == "directory") { for (int i = 0; i < myXml.ChildNodes[1].FirstChild.ChildNodes.Count; i++) { bool isDir = bool.Parse(myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["isDir"].Value); string title = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["title"].Value; string id = myXml.ChildNodes[1].FirstChild.ChildNodes[i].Attributes["id"].Value; SubsonicItem theItem = new SubsonicItem(title, id, (isDir ? SubsonicItemType.Folder : SubsonicItemType.Song), parent); children.Add(theItem); } } } } return children; }
public static List<SubsonicItem> GetIndexes(ISubsonicConnection connection) { return GetIndexes(connection, "", ""); }
public static List<SubsonicItem> GetIndexes(ISubsonicConnection connection, string musicFolderId) { return GetIndexes(connection, musicFolderId, ""); }