public SubsonicItem FindItemById(string id) { SubsonicItem foundItem = null; // If the current item is the item we are looking for, return it if (this.id == id) { foundItem = this; } // Otherwise, we check the children if they exist else if (_children != null) { foreach (SubsonicItem child in _children) { // If this child is the item we are looking for, return it if (child.id == id) { foundItem = child; break; } else { foundItem = child.FindItemById(id); if (foundItem != null) { break; } } } } return(foundItem); }
/// <summary> /// Takes parameters for server, username and password to generate an auth header /// and Pings the server /// </summary> /// <param name="theServer"></param> /// <param name="user"></param> /// <param name="password"></param> /// <returns>Resulting XML (Future boolean)</returns> public static string LogIn(string theServer, string user, string password) { string result = "Nothing Happened"; server = theServer; authHeader = user + ":" + password; authHeader = Convert.ToBase64String(Encoding.Default.GetBytes(authHeader)); // Store user and encoded password for alternate authentication username = user; Byte[] passwordBytes = Encoding.Default.GetBytes(password); for (int i = 0; i < passwordBytes.Length; i++) { encPass += passwordBytes[i].ToString("x2"); } Stream theStream = MakeGenericRequest("ping", null); StreamReader sr = new StreamReader(theStream); result = sr.ReadToEnd(); /// TODO: Parse the result and determine if logged in or not _MyLibrary = new SubsonicItem("LibraryRoot", "-1", SubsonicItem.SubsonicItemType.Library, null); return(result); }
public SubsonicItem(string name, string id, SubsonicItemType itemType, SubsonicItem parent) { this.name = name; this.id = id; this.itemType = itemType; this.parent = parent; this.lastAccessed = DateTime.Now.ToString(); }
public Song(string title, string artist, string album, string id, SubsonicItem parent) { this.artist = artist; this.title = title; this.album = album; this.name = title; this.id = id; this.itemType = SubsonicItem.SubsonicItemType.Song; this.parent = parent; this.lastAccessed = DateTime.Now.ToString(); }
public SubsonicItem GetChildByName(string childName) { SubsonicItem theItem = null; if (_children != null) { theItem = _children.Find( delegate(SubsonicItem itm) { return(itm.name == childName); } ); } return(theItem); }
/// <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="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(string id) { Dictionary <string, string> theParameters = new Dictionary <string, string>(); theParameters.Add("id", id); Stream theStream = MakeGenericRequest("getMusicDirectory", theParameters); StreamReader sr = new StreamReader(theStream); string result = sr.ReadToEnd(); 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(); theParent.name = myXML.ChildNodes[1].FirstChild.Attributes["name"].Value; theParent.id = myXML.ChildNodes[1].FirstChild.Attributes["id"].Value; int i = 0; for (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 ? SubsonicItem.SubsonicItemType.Folder : SubsonicItem.SubsonicItemType.Song), theParent); theContents.Add(theItem); } } } return(theContents); }
/// <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="query">The Term you want to search for</param> /// <returns>A List of SubsonicItem objects</returns> public static List <SubsonicItem> Search(string query) { Dictionary <string, string> parameters = new Dictionary <string, string>(); Version apiV = new Version(apiVersion); 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 Stream theStream = MakeGenericRequest(request, parameters); // Read the response as a string StreamReader sr = new StreamReader(theStream); string result = sr.ReadToEnd(); // 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; if (isDir) { theItem = new SubsonicItem(title, theId, SubsonicItem.SubsonicItemType.Folder, null); } else { theItem = new Song(title, artist, album, theId); } searchResults.Add(theItem); } } } return(searchResults); }
/// <summary> /// Returns a list of SubsonicItems that fall inside the parent object /// </summary> /// <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(SubsonicItem parent, string ifModifiedSince) { Dictionary <string, string> parameters = new Dictionary <string, string>(); // Generate the proper request for the parent type string requestType, musicFolderId; if (parent.itemType == SubsonicItem.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 Stream theStream = MakeGenericRequest(requestType, parameters); // Read the response as a string StreamReader sr = new StreamReader(theStream); string result = sr.ReadToEnd(); // Parse the resulting XML string into an XmlDocument XmlDocument myXML = new XmlDocument(); myXML.LoadXml(result); List <SubsonicItem> children = new List <SubsonicItem>(); // Parse the artist if (parent.itemType == SubsonicItem.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++) { 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, SubsonicItem.SubsonicItemType.Folder, parent)); } } } } } // Parse the directory else if (parent.itemType == SubsonicItem.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 ? SubsonicItem.SubsonicItemType.Folder : SubsonicItem.SubsonicItemType.Song), parent); children.Add(theItem); } } } } return(children); }
/// <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="query">The Term you want to search for</param> /// <returns>A List of SubsonicItem objects</returns> public static List<SubsonicItem> Search(string query) { Dictionary<string, string> parameters = new Dictionary<string, string>(); Version apiV = new Version(apiVersion); 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 Stream theStream = MakeGenericRequest(request, parameters); // Read the response as a string StreamReader sr = new StreamReader(theStream); string result = sr.ReadToEnd(); // 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; if (isDir) theItem = new SubsonicItem(title, theId, SubsonicItem.SubsonicItemType.Folder, null); else theItem = new Song(title, artist, album, theId); searchResults.Add(theItem); } } } return searchResults; }
/// <summary> /// Takes parameters for server, username and password to generate an auth header /// and Pings the server /// </summary> /// <param name="theServer"></param> /// <param name="user"></param> /// <param name="password"></param> /// <returns>Resulting XML (Future boolean)</returns> public static string LogIn(string theServer, string user, string password) { string result = "Nothing Happened"; server = theServer; authHeader = user + ":" + password; authHeader = Convert.ToBase64String(Encoding.Default.GetBytes(authHeader)); // Store user and encoded password for alternate authentication username = user; Byte[] passwordBytes = Encoding.Default.GetBytes(password); for (int i = 0; i < passwordBytes.Length; i++) encPass += passwordBytes[i].ToString("x2"); Stream theStream = MakeGenericRequest("ping", null); StreamReader sr = new StreamReader(theStream); result = sr.ReadToEnd(); /// TODO: Parse the result and determine if logged in or not _MyLibrary = new SubsonicItem("LibraryRoot", "-1", SubsonicItem.SubsonicItemType.Library, null); return result; }
/// <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="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(string id) { Dictionary<string, string> theParameters = new Dictionary<string, string>(); theParameters.Add("id", id); Stream theStream = MakeGenericRequest("getMusicDirectory", theParameters); StreamReader sr = new StreamReader(theStream); string result = sr.ReadToEnd(); 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(); theParent.name = myXML.ChildNodes[1].FirstChild.Attributes["name"].Value; theParent.id = myXML.ChildNodes[1].FirstChild.Attributes["id"].Value; int i = 0; for (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 ? SubsonicItem.SubsonicItemType.Folder : SubsonicItem.SubsonicItemType.Song), theParent); theContents.Add(theItem); } } } return theContents; }
/// <summary> /// Returns a list of SubsonicItems that fall inside the parent object /// </summary> /// <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(SubsonicItem parent, string ifModifiedSince) { Dictionary<string, string> parameters = new Dictionary<string, string>(); // Generate the proper request for the parent type string requestType, musicFolderId; if (parent.itemType == SubsonicItem.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 Stream theStream = MakeGenericRequest(requestType, parameters); // Read the response as a string StreamReader sr = new StreamReader(theStream); string result = sr.ReadToEnd(); // Parse the resulting XML string into an XmlDocument XmlDocument myXML = new XmlDocument(); myXML.LoadXml(result); List<SubsonicItem> children = new List<SubsonicItem>(); // Parse the artist if (parent.itemType == SubsonicItem.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++) { 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, SubsonicItem.SubsonicItemType.Folder, parent)); } } } } } // Parse the directory else if (parent.itemType == SubsonicItem.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 ? SubsonicItem.SubsonicItemType.Folder : SubsonicItem.SubsonicItemType.Song), parent); children.Add(theItem); } } } } return children; }