Asked by:
ShoutCast Server Stats Success!!!!

Question
-
User-131283606 posted
EUREKA!!!
For those of you that are down with the whole Internet radio biz you know what a pain retriveing shoutcast XML stats from a Shoutcast server can be on ASP.NET. As simple of a thing as this is it is CRUCIAL for the ineternet radio station website to be able to display how many listeners, current song playing, playlist history ETC.. FINALLY I have figured it out and here is my function. It returns the XML file in a string (with leading whitespace trimmed of course since ASP.NET cant seem to handle that). How you decide to parse thru the XML is up to you but I've added that portion too after the retrieval function.
I have used this to populate the modules on www.ORCradio.com so if you are interested in seeing them in action and/or you like good HipHop then check it out.
Public Const XML_FILE = "http://yourradio.com:8000/admin.cgi?pass=changeme&mode=viewxml"
Public Shared Function Get_XMLFile() As String
Dim strRemoteFile As String = XML_FILE
Dim wcMozilla As New WebClient
Dim strResponse As StringTry
wcMozilla .Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
strResponse = wcMozilla .DownloadString(strRemoteFile)
Catch ex As Exception
Return ex.Message
End Try'Finally we must remove all carriage returns so that XML.Load does not fail
strResponse = Regex.Replace(strResponse, "\n", "")
Return strResponse
End FunctionThe reason you use WebClient instead of WebRequest is because you can set the "user-agent" header to Mozilla in a WebClient where as you cannot in a WebRequest. You need to do this so that when you connect to the server it knows to send you the XML stats page and not the music stream. I cant tell you how frustrating this was dealing with. I kept getting a "404 Page not found error" when trying to access the stats page. Also dont forget to change the password to your proper password in the query string (pass=changeme)
For stepping thru the XML stats data i have a function the returns a HashTable as follows. Please note that when using XmlDocument.Load() you can't pass just the string you must convert the string to a string reader:
Public Shared Function Get_RadioInfo() As Hashtable
Dim htReturn As New Hashtable
Dim xdInfo As XmlDocument
Dim xnlMain, xnlHistory As XmlNodeList
Dim xnMain As XmlNode
Dim dtHistory As New DataTable
Dim dr As DataRow
Dim blnSkipFirstHistory As Boolean = False
Try
'Create the XML Document
xdInfo = New XmlDocument()
'Load the Xml file using our new function :-)
xdInfo.Load(New StringReader(Get_XMLFile()))
'Get the list of name nodesh
xnlMain = xdInfo.GetElementsByTagName("SONGTITLE")
xnMain = xnlMain.Item(0)
htReturn.Add("currently_playing", xnMain.InnerText)
xnlMain = xdInfo.GetElementsByTagName("STREAMHITS")
xnMain = xnlMain.Item(0)
htReturn.Add("stream_hits", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("AVERAGETIME")
xnMain = xnlMain.Item(0)
htReturn.Add("average_listen_time", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("CURRENTLISTENERS")
xnMain = xnlMain.Item(0)
htReturn.Add("current_listeners", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("MAXLISTENERS")
xnMain = xnlMain.Item(0)
htReturn.Add("max_listeners", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("PEAKLISTENERS")
xnMain = xnlMain.Item(0)
htReturn.Add("peak_listeners", xnMain.InnerText)dtHistory.Columns.Add("TITLE", GetType(String))
xnlMain = xdInfo.SelectNodes("/SHOUTCASTSERVER/SONGHISTORY/SONG")For Each xnMain In xnlMain
If blnSkipFirstHistory Then 'Make sure skips the first history track
dr = dtHistory.NewRow()
xnlHistory = xnMain.ChildNodes
dr("TITLE") = xnlHistory.Item(1).InnerText
dtHistory.Rows.Add(dr)
End If
blnSkipFirstHistory = True
NexthtReturn.Add("playlist_history", dtHistory)
Catch ex As Exception
htReturn.Add("ErrorMessage", ex.Message)
Return htReturn
End TryReturn htReturn
End FunctionHappy Coding and I hope this helped some people.
Wednesday, August 9, 2006 11:58 AM
All replies
-
User-232086107 posted
Thanks bro. How do you get the xml file and all. I'm buiding one for a friend. And I don't know the shoutcast thing and how to set it up. Can you explain more if you don't mind?Thursday, December 7, 2006 1:00 PM -
User-131283606 posted
Thanks bro. How do you get the xml file and all. I'm buiding one for a friend. And I don't know the shoutcast thing and how to set it up. Can you explain more if you don't mind?
Sure man. The XML file is automatically generated. Its a feature of shoutcast. Get the address of the shoutcast server and http://<ADDRESSHERE>:8000/admin.cgi?pass=changeme&mode=viewxml" and swap it out with <ADDRESSHERE> "changeme" needs to be your admin password. Default is "changeme". Mode=viewxml makes the server return the Dynamic XML stats page. Put that link in your browser and if you did it correctly you'll see an XML page returned. Once you've done this you are good to go.
Wednesday, December 13, 2006 4:25 PM -
User-1774918959 posted
Thanks a lot dude!!
Suggestion:
We can even use ds.ReadXml(New StringReader(strResponse), XmlReadMode.Auto) instead of xdInfo.Load(New StringReader(strResponse)), where ds is DataSet.
We can get all the statistics by accessing different tables in the dataset.
ds.Tables(5).DefaultView. Table 5 has song history.
Thursday, March 13, 2008 10:47 AM -
User1177197882 posted
Guys I really hate to sound as Newbie as I am - But I need Help.
Example:
Webpage that will show station status will be http://tmvausa.com/stationstatus.asp
The address of my shoutcast station is : http://tmva.dnsalias.com:8000All I want to be displayed is:
Station Status: (shows the station status) - like broadcasting or not broadcasting
Currently Playing: (show the name of the song currently playing)
(My ShoutCast server is config'd to retain a 20 song history so)
Last 20 Songs Played: (and then list those 20 songs).I am totally new to this ASP - but since I am using a Frontpage Extensions Service Provider and Own MS Expression Web, and Don't understand (or believe my hosting server allows) PHP at all, I am trying to do this with ASP.
So please any information that you can help me with will be greatly appreciated.
1>
Public Const XML_FILE = "http://tmva.dnsalias.com:8000/admin.cgi?pass=letmein&mode=viewxml"
Public Shared Function Get_XMLFile() As String
Dim strRemoteFile As String = XML_FILE
Dim wcMozilla As New WebClient
Dim strResponse As StringTry
wcMozilla .Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
strResponse = wcMozilla .DownloadString(strRemoteFile)
Catch ex As Exception
Return ex.Message
End Try'Finally we must remove all carriage returns so that XML.Load does not fail
strResponse = Regex.Replace(strResponse, "\n", "")
Return strResponse
End FunctionAs you can see - I understand changing the line with my url and password, whay I don't understand is where does this statement (to be correct - function) go on my asp web page (I'm Using MS Expression Web (2006)).
2>
Public Shared Function Get_RadioInfo() As Hashtable
Dim htReturn As New Hashtable
Dim xdInfo As XmlDocument
Dim xnlMain, xnlHistory As XmlNodeList
Dim xnMain As XmlNode
Dim dtHistory As New DataTable
Dim dr As DataRow
Dim blnSkipFirstHistory As Boolean = False
Try
'Create the XML Document
xdInfo = New XmlDocument()
'Load the Xml file using our new function :-)
xdInfo.Load(New StringReader(Get_XMLFile()))
'Get the list of name nodesh
xnlMain = xdInfo.GetElementsByTagName("SONGTITLE")
xnMain = xnlMain.Item(0)
htReturn.Add("currently_playing", xnMain.InnerText)
xnlMain = xdInfo.GetElementsByTagName("STREAMHITS")
xnMain = xnlMain.Item(0)
htReturn.Add("stream_hits", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("AVERAGETIME")
xnMain = xnlMain.Item(0)
htReturn.Add("average_listen_time", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("CURRENTLISTENERS")
xnMain = xnlMain.Item(0)
htReturn.Add("current_listeners", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("MAXLISTENERS")
xnMain = xnlMain.Item(0)
htReturn.Add("max_listeners", Convert.ToInt32(xnMain.InnerText))
xnlMain = xdInfo.GetElementsByTagName("PEAKLISTENERS")
xnMain = xnlMain.Item(0)
htReturn.Add("peak_listeners", xnMain.InnerText)dtHistory.Columns.Add("TITLE", GetType(String))
xnlMain = xdInfo.SelectNodes("/SHOUTCASTSERVER/SONGHISTORY/SONG")For Each xnMain In xnlMain
If blnSkipFirstHistory Then 'Make sure skips the first history track
dr = dtHistory.NewRow()
xnlHistory = xnMain.ChildNodes
dr("TITLE") = xnlHistory.Item(1).InnerText
dtHistory.Rows.Add(dr)
End If
blnSkipFirstHistory = True
NexthtReturn.Add("playlist_history", dtHistory)
Catch ex As Exception
htReturn.Add("ErrorMessage", ex.Message)
Return htReturn
End TryReturn htReturn
End Function"Where do I put his at in my .asp Page also?"
I just need to get Song Information and Station Status to Appear on A Webpage.
So please any information that you can help me with will be greatly appreciated.
Thanks.
Friday, July 23, 2010 4:14 PM -
User-131283606 posted
Hmmm NO clue at all how I would do that using Front Page extensions since i've never used them. I had all of those functions in my App_Code folder in a .vb class file using ASP.NET. Thats kind of like an include file in ASP
Wednesday, July 28, 2010 8:17 PM