Get Shoutcast v1/v2 listeners number in VB.NET
Today i was trying to find a solution to grab listeners number from shoutcast servers, but i needed a something simple and to work with both versions of shoutcast. After a few tests i end up using the shoutcast generated XML file.
First, i made a class that will hold all members:
Class StreamServer Public ServerName As String Public ServerAdress As String Public ServerPassword As String Public StreamID As String = 0 Public ListenersCount As Integer Public Sub New(ByVal sName As String, ByVal sAddress As String, ByVal sPassword As String, ByVal ServerStreamID As String) ServerName = sName ServerAdress = sAddress ServerPassword = sPassword StreamID = ServerStreamID End Sub Public Overrides Function ToString() As String Return ServerName & ": " & ListenersCount End Function End Class
And the function that gets the needed data:
Private Function Get_XMLFile(ByVal ServerID As Integer) As Integer Dim wcMozilla As New WebClient Dim strResponse As String = Nothing Try ''Add a compatible header wcMozilla.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") ''if we have a StreamID set, we have a shoutcast V2 server If MyServers.Item(ServerID).StreamID > 0 Then strResponse = wcMozilla.DownloadString(MyServers.Item(ServerID).ServerAdress & "/admin.cgi?pass=" & MyServers.Item(ServerID).ServerPassword & "&sid=" & MyServers.Item(ServerID).StreamID & "&mode=viewxml") Else strResponse = wcMozilla.DownloadString(MyServers.Item(ServerID).ServerAdress & "/admin.cgi?pass=" & MyServers.Item(ServerID).ServerPassword & "&mode=viewxml") End If Catch ex As WebException 'MsgBox(ex.Message) Finally End Try If String.IsNullOrEmpty(strResponse) = False Then Try Dim ds As New DataSet ds.ReadXml(New StringReader(strResponse), XmlReadMode.Auto) MyServers.Item(ServerID).ListenersCount = CInt(ds.Tables(0).Rows(0).Item("CURRENTLISTENERS").ToString) Catch ex As Exception End Try Else MyServers.Item(ServerID).ListenersCount = 0 End If Return MyServers.Item(ServerID).ListenersCount End Function
I added a timer that calls the “Get_XMLFile” function every 15 seconds and counts the total listeners, which is displayed on a label.
This is the entire code:
Imports System.Xml Imports System.Net Imports System.Text.RegularExpressions Imports System.IO Public Class Form1 Class StreamServer Public ServerName As String Public ServerAdress As String Public ServerPassword As String Public StreamID As String = 0 Public ListenersCount As Integer Public Sub New(ByVal sName As String, ByVal sAddress As String, ByVal sPassword As String, ByVal ServerStreamID As String) ServerName = sName ServerAdress = sAddress ServerPassword = sPassword StreamID = ServerStreamID End Sub Public Overrides Function ToString() As String Return ServerName & ": " & ListenersCount End Function End Class Private MyServers As New List(Of StreamServer) Private ListenerCount As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim newServer As New StreamServer("My First Server", "https://127.0.0.1:8000", "changeme", 0) MyServers.Add(newServer) newServer = New StreamServer("My Second V2 Server", "https://127.0.0.1:8050", "changeme", 1) MyServers.Add(newServer) End Sub Private Function Get_XMLFile(ByVal ServerID As Integer) As Integer Dim wcMozilla As New WebClient Dim strResponse As String = Nothing Try ''Add a compatible header wcMozilla.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)") ''if we have a StreamID set, we have a shoutcast V2 server If MyServers.Item(ServerID).StreamID > 0 Then strResponse = wcMozilla.DownloadString(MyServers.Item(ServerID).ServerAdress & "/admin.cgi?pass=" & MyServers.Item(ServerID).ServerPassword & "&sid=" & MyServers.Item(ServerID).StreamID & "&mode=viewxml") Else strResponse = wcMozilla.DownloadString(MyServers.Item(ServerID).ServerAdress & "/admin.cgi?pass=" & MyServers.Item(ServerID).ServerPassword & "&mode=viewxml") End If Catch ex As WebException 'MsgBox(ex.Message) Finally End Try If String.IsNullOrEmpty(strResponse) = False Then Try Dim ds As New DataSet ds.ReadXml(New StringReader(strResponse), XmlReadMode.Auto) MyServers.Item(ServerID).ListenersCount = CInt(ds.Tables(0).Rows(0).Item("CURRENTLISTENERS").ToString) Catch ex As Exception End Try Else MyServers.Item(ServerID).ListenersCount = 0 End If Return MyServers.Item(ServerID).ListenersCount End Function Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ListenerCount = 0 For i As Integer = 0 To MyServers.Count - 1 ListenerCount += Get_XMLFile(i).ToString Next Me.lbStatus.Text = "Current Listeners: " & ListenerCount.ToString End Sub End Class
The only drawnback of this approach is that you need to supply the admin password in order to reach the shoucast XML.