Shoutcast Stats
Posted by rayeh
Saturday, March 06, 2004
Description: This is a simple tutorial to get basic statistics from a shoutcast server without using a password and using /7.html
Basically all we are doing is going to connect to the shoutcast server and do the following things:
In the on text event we need to do a few things:
Next we need to tell mIRC what to do when the socket opens. So here we will use the on sockopen event.
In the on sockopen event we will need to do the following:
Now comes the fun part... Parsing the data we get back.
Here we will need to do the following:
After this we probably want to send everything back to the channel right?
This is how I did it, I added some extra stuff like calculating the bandwidth used by server-
Heres the full code of the script
Basically all we are doing is going to connect to the shoutcast server and do the following things:
- Parse the data from the on text and open the socket
- Sockwrite to the server
- Parse the data we need from the data it gives
- Send it back to the channel
In the on text event we need to do a few things:
- Set the channel it was triggered to a variable for later use
- Close the socket inczase there was an error the last time and it didn't close
- Parse the text given to get the host and port (In the example it will be "-scstat host:port")
- Open the socket to the server
on *:TEXT:-scstat*:#:{
set %scstat.chan $chan
sockclose scstat
sockopen scstat $gettok($2,1,58) $gettok($2,2,58)
}
Next we need to tell mIRC what to do when the socket opens. So here we will use the on sockopen event.
In the on sockopen event we will need to do the following:
- Tell the server we want /7.html
- Tell the server we are using mozilla
- Tell the server we are done socket writing
on *:SOCKOPEN:scstat:{
sockwrite -n $sockname GET /7.html HTTP/1.0
sockwrite -n $sockname User-Agent: Mozilla
sockwrite -n $sockname $crlf
}
Now comes the fun part... Parsing the data we get back.
Here we will need to do the following:
- Strip the excess HTML from the
- Parse the data we want and set it to variables
<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>45,1,91,150,42,192,DJ Tiesto - In Search of Sunrise 3</body></html>- The '45' is the current listeners
- The '1' is binary, it is telling us whether there is a live dj or not
- The '91' is the peak listeners
- The '150' is the max listeners
- The '42' is reported listeners, we don't really need that
- The '192' is the selected bitrate
- Everything after that is the current song
on *:sockread:scstat:{
if ($sockerr > 0) return
:nextread
sockread -f %scasttemp
if ($sockbr == 0) return
if (%scasttemp == $null) %scasttemp = empty
set %scasttemp $remove(%scasttemp,<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>,</body></html>)
if ((HTTP/1.* !iswm %scasttemp) && (content-type* !iswm %scasttemp) && (%scasttemp != empty)) {
set %scstat.song $gettok(%scasttemp,7-,44)
set %scstat.bitrate $gettok(%scasttemp,6,44)
set %scstat.listeners $gettok(%scasttemp,1,44)
set %scstat.maxlist $gettok(%scasttemp,4,44)
set %scstat.peak $gettok(%scasttemp,3,44)
if ($gettok(%scasttemp,2,44) == 1) set %scstat.livedj connected
else set %scstat.livedj not connected
; changing some of the html codes back to regular characters
set %scast.song $replace(%scast.song,&,$chr(38),',$chr(39))
}
goto nextread
}
After this we probably want to send everything back to the channel right?
This is how I did it, I added some extra stuff like calculating the bandwidth used by server-
on *:sockclose:scstat:{
msg %scstat.chan DSP [^U] $+ %scstat.livedj $+ [^U] and playing [^U] $+ %scstat.song $+ [^U]
msg %scstat.chan [^U] $+ %scstat.listeners $+ [^U] out of [^U] $+ %scstat.maxlist $+ [^U] listeners at [^U] $+ $+(%scstat.bitrate,Kb) $+ [^U] using up $+([^U],$round($calc(((%scstat.bitrate *1024) * %scstat.listeners)/ 1024 / 1024),2),Mb/s[^U]) or $+([^U],$round($calc(((%scstat.bitrate *1024) * %scstat.listeners)/ 1024 ),2),Kb/s[^U]) or $+([^U],$round($calc(((%scstat.bitrate *1024) * %scstat.listeners)/ 1024 / 8),2),KB/s[^U])
msg %scstat.chan You can listen at $+([^U]http://,%scstat.host,:,%scstat.port,/listen.pls[^U])
}
Heres the full code of the script
on *:TEXT:-scstat*:#:{
set %scstat.chan $chan
sockclose scstat
sockopen scstat $gettok($2,1,58) $gettok($2,2,58)
}
on *:SOCKOPEN:scstat:{
sockwrite -n $sockname GET /7.html HTTP/1.0
sockwrite -n $sockname User-Agent: Mozilla
sockwrite -n $sockname $crlf
}
on *:sockread:scstat:{
if ($sockerr > 0) return
:nextread
sockread -f %scasttemp
if ($sockbr == 0) return
if (%scasttemp == $null) %scasttemp = empty
set %scasttemp $remove(%scasttemp,<HTML><meta http-equiv="Pragma" content="no-cache"></head><body>,</body></html>)
if ((HTTP/1.* !iswm %scasttemp) && (content-type* !iswm %scasttemp) && (%scasttemp != empty)) {
set %scstat.song $gettok(%scasttemp,7-,44)
set %scstat.bitrate $gettok(%scasttemp,6,44)
set %scstat.listeners $gettok(%scasttemp,1,44)
set %scstat.maxlist $gettok(%scasttemp,4,44)
set %scstat.peak $gettok(%scasttemp,3,44)
if ($gettok(%scasttemp,2,44) == 1) set %scstat.livedj connected
else set %scstat.livedj not connected
; changing some of the html codes back to regular characters
set %scast.song $replace(%scast.song,&,$chr(38),',$chr(39))
}
goto nextread
}
on *:sockclose:scstat:{
msg %scstat.chan DSP [^U] $+ %scstat.livedj $+ [^U] and playing [^U] $+ %scstat.song $+ [^U]
msg %scstat.chan [^U] $+ %scstat.listeners $+ [^U] out of [^U] $+ %scstat.maxlist $+ [^U] listeners at [^U] $+ $+(%scstat.bitrate,Kb) $+ [^U] using up $+([^U],$round($calc(((%scstat.bitrate *1024) * %scstat.listeners)/ 1024 / 1024),2),Mb/s[^U]) or $+([^U],$round($calc(((%scstat.bitrate *1024) * %scstat.listeners)/ 1024 ),2),Kb/s[^U]) or $+([^U],$round($calc(((%scstat.bitrate *1024) * %scstat.listeners)/ 1024 / 8),2),KB/s[^U])
msg %scstat.chan You can listen at $+([^U]http://,%scstat.host,:,%scstat.port,/listen.pls[^U])
}