Old 6th December 2001, 15:45   #1
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
getting server status from XML

howdy.. ive been messing around with shoutcast, and i am trying to figure out how to extract the song title of what is currently playing... i thought i had something when i found the scripts at
http://beta.shoutcast.com/~tpepper but i cant figure why they arent grabbing the xml code.

anyone have any suggestions? / hints? / scripts? /corn?

nodlive
nodlive is offline   Reply With Quote
Old 8th December 2001, 18:11   #2
mjbrown
Senior Member
 
Join Date: Aug 2001
Posts: 114
I'm doing it with Python 2.1 CGI scripts, rather than PHP.

2 things to note:

- Shoutcast has a bug where its XML output starts with an extraneous linefeed, thereby producing malformed XML that should be rejected by any compliant parser. PHP's XML parser, and MSXML versions prior to 3.0 (so, versions of MSIE prior to 6.0) have a bug where they do not reject this XML. Had someone bothered to run the XML through a real parser, I bet the linefeed would have been fixed a long time ago. *grumble*

- Shoutcast, for reasons unknown, will mangle track titles, replacing apostrophes with backticks.

I account for these problems in the code below.


Here's getTitle.cgi

#!/usr/local/bin/python -O
from playlistUtils import getTitle
getTitle()


I reference this with a server-side exec directive in my HTML (standard Apache feature; just make sure you have set .html docs to be server-parsed in your httpd.conf):

<!--#exec cgi="getTitle.cgi"-->

And here's the relevant part of my playlistUtils.py. Note that it not only corrects the above-mentioned problems, it also changes "-inch" to a double quote, so you can have titles like

foo (12-inch mix)

render as

foo (12" mix)


#!/usr/local/bin/python -O
import httplib, string, os, sys, codecs
from xml.dom.minidom import parseString
from xml.dom.ext.Printer import TranslateCdata

def fixText(s):
&#160;&#160;&#160;&#160;s = string.replace(s,"`","'")
&#160;&#160;&#160;&#160;s = string.replace(s,"-inch ","\" ")
&#160;&#160;&#160;&#160;return unicode(TranslateCdata(s,'iso-8859-1'),'iso-8859-1')

def getTitle():
&#160;&#160;&#160;&#160;h = httplib.HTTP('my.shoutcast.server:7777')
&#160;&#160;&#160;&#160;h.putrequest('GET','/admin.cgi?pass=abcdefghijk&mode=viewxml&page=1')
&#160;&#160;&#160;&#160;h.putheader('User-Agent','Mozilla/4.0 (compatible; not really)')
&#160;&#160;&#160;&#160;h.endheaders()
&#160;&#160;&#160;&#160;errcode, errmsg, headers = h.getreply()
&#160;&#160;&#160;&#160;data = h.getfile().read()[1:]
&#160;&#160;&#160;&#160;doc = parseString(data)
&#160;&#160;&#160;&#160;currentTrack = string.replace(doc.getElementsByTagName("SONGTITLE")[0].childNodes[0].nodeValue,"`","'")
&#160;&#160;&#160;&#160;doc.unlink()
&#160;&#160;&#160;&#160;print ("Content-Type: text/html\n\n" + fixText(currentTrack)).encode('iso-8859-1')
&#160;&#160;&#160;&#160;return


Indenting is important in Python; I had to use non-breaking spaces to get the blocks above to indent. Replace them with regular spaces in your code.

For speed and security I keep these source files in a separate location and playlist.cgi once from the command line once to produce the .pyo files. Then I use those in the public directory, instead of the .py files.

I also have several other functions defined in playlistUtils.py, to generate the whole playlist, get the current # of listeners, etc.
mjbrown is offline   Reply With Quote
Old 9th December 2001, 23:16   #3
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
dont suppose you would know the PHP equivalent of the above?

nodlive
nodlive is offline   Reply With Quote
Old 10th December 2001, 01:32   #4
nicadams
Junior Member
 
Join Date: May 2001
Location: Detroit MI
Posts: 19
Send a message via ICQ to nicadams Send a message via AIM to nicadams
nodlive,

i've got something written in php that does what i think you want. it does require tom's scxml-grabber.phps from his site. I'm new to PHP and programming. I'm sure there's a more efficient way to do this but this works for me for now.

// New Object for High Bandwidth
$serv1 = new SCXML;

// Set Server vars
$serv1->set_host("someserver");
$serv1->set_port(8000);
$serv1->set_password("somepass");

// Get Server Info
$sngtitle=$serv1->fetchMatchingTag("SONGTITLE");
// turn %20 into spaces
$newtitle=explode("%20",$title);
$newtitle=implode(" ",$newtitle);

// Turn %6o into apostrophe
$newsong=explode("%20",$sngtitle);
$newsong=implode(" ",$newsong);
// Turn %6o into apostrophe
$newsong=explode("%60",$newsong);
$newsong=implode("'",$newsong);
// Turn %2C into comma
$newsong=explode("%2C",$newsong);
$newsong=implode(",",$newsong);
// Turn %26 into amperstand
$newsong=explode("%26",$newsong);
$newsong=implode("&",$newsong);
// Turn %28 into left parens
$newsong=explode("%28",$newsong);
$newsong=implode("(",$newsong);
// Turn %29 into right parens
$newsong=explode("%29",$newsong);
$newsong=implode(")",$newsong);
// Turn %3F into Question Marks
$newsong=explode("%3F",$newsong);
$newsong=implode("?",$newsong);
// Turn %23 into Pound Signs
$newsong=explode("%23",$newsong);
$newsong=implode("#",$newsong);
//build html

// Output Current Song
echo "<font face=\"Tahoma\">Current Song:<br>";
echo "<font color=\"FFFFFF\"><font size=-1>$newsong</font></font>";
echo "<BR>";

implimented at http://sllywhtboy.dhs.org/

hope this helps.

-nick adams
nicadams is offline   Reply With Quote
Old 10th December 2001, 02:57   #5
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
thanks a bunch.. this is exactly what i was looking for!
nodlive is offline   Reply With Quote
Old 10th December 2001, 03:14   #6
Bhala
Junior Member
 
Join Date: May 2001
Posts: 3
This will be much faster

PHP Code:
<?
// Requires tom's stat graber
require("scxml-grabber.php");

// New Object for High Bandwidth 
$serv1 = new SCXML

// Set Server vars 
$serv1->set_host("someserver"); 
$serv1->set_port(8000); 
$serv1->set_password("somepass"); 

// Get Server Info 
$gsong=$serv1->fetchMatchingTag("SONGTITLE"); 

$search =  array("%20","%60","%2C","%26","%28","%29","%3F","%23");
$replace = array(" ""'"",""&""("")""?""#");
 
$song str_replace($search$replace$gsong);

echo 
"<font face=\"Tahoma\">Current Song:<br>"
echo 
"<font color=\"FFFFFF\"><font size=-1>$song</font></font>"
echo 
"<br>"
?>
Bhala is offline   Reply With Quote
Old 10th December 2001, 03:52   #7
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
ok i am starting to go batty...
i cant figure out why neither of those scripts will work...

any chance you would point me towards to tom's source?

maybe email it to me at nodlive@canada.com

it should work, but i cant figure out why it doesnt.. its driving me crazy!!!

nodlive
nodlive is offline   Reply With Quote
Old 10th December 2001, 06:00   #8
nicadams
Junior Member
 
Join Date: May 2001
Location: Detroit MI
Posts: 19
Send a message via ICQ to nicadams Send a message via AIM to nicadams
Thanks for the 'array' idea.

Peppers page:

1/31/2001................

XML log parser for PHP 0.2 (objectified)
Object code: here. (http://beta.shoutcast.com/~tpepper/scxml-obj.phps)
Sample implementation: here.

the scxml-obj.phps will extract the xml data from the shoutcast server. There are two scripts. one: xml-grabber (****, above) two: your php code for your web page.

What does your code look like? It might be easier for us to help you..

-nick
nicadams is offline   Reply With Quote
Old 10th December 2001, 14:18   #9
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
right now my code looks exactly like tom's object file, and i have tried both of the above scripts, modifying the password and url parameters. when i run the script it comes up blank.. (or says Current Song

i cant for the life of me figure out why it doesnt work..

i would appreciate any help you can offer

nodlive
nodlive is offline   Reply With Quote
Old 10th December 2001, 15:21   #10
mjbrown
Senior Member
 
Join Date: Aug 2001
Posts: 114
When you say things don't work, are you getting any error messages at all? Is it as if the HTML renders and the PHP code is not processed?

This is a dumb question, but we must start with the basics... do you actually have PHP enabled in your web server?

If so, is it PHP 3 or 4? If it's 3, your server might only be configured to treat .php3 files as special.

Do you have access to the server's logs? They can give you clues too.
mjbrown is offline   Reply With Quote
Old 10th December 2001, 15:43   #11
nicadams
Junior Member
 
Join Date: May 2001
Location: Detroit MI
Posts: 19
Send a message via ICQ to nicadams Send a message via AIM to nicadams
can you get into the server admin pages & see the xml data if you click the "view xml data" ?

also, in html, 'ffffff' font color is white, so you might not be able to see it on a white background.

try '000000' (black) for a font color and see if any data comes up.

-nick
nicadams is offline   Reply With Quote
Old 10th December 2001, 17:31   #12
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
ok, for starters, i have php 4.something installed.. it runs other scripts no problem-o.

info :
apache server
win xp opperating system
php 4

for output i am not getting any errors. with the one script it should output

Current Song : Stayin Alive

what it does output is

Current Song :

its like its not finding any of the info, but not erroring out either.. which doesnt make a whole lot of sense when i look at the code.

i even checked the color thing, because stuff like that just seems to happen.

currently i am running a plugin called dosomethingnow. which will go grab the xml stats, and place it into a file on my hd, i then use the contents of the file to grab the current song. so it is possible to access the xml stats, (i can do it manually by going through the admin interface as well)..

i cant for the life of me figure out why its not working.. i was hoping it was something simple.. ergh!

nodlive
nodlive is offline   Reply With Quote
Old 10th December 2001, 17:57   #13
nicadams
Junior Member
 
Join Date: May 2001
Location: Detroit MI
Posts: 19
Send a message via ICQ to nicadams Send a message via AIM to nicadams
let's test the code.

before the output, set $newsong = "test text";

Then it should look like:

Current Song: test text

try commmenting out the html->txt array if that works... get this thing back to basics..

are you getting any errors at all? can you post your code?
nicadams is offline   Reply With Quote
Old 10th December 2001, 18:21   #14
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
yeah, if i put
$newsong = "test this bucko";

right before the echo out i get

Current Song:test this bucko

which is what i should get..

here is the code... (ive changed the password.. thats it)

<?

require("scxml-grabber.php");

// New Object for High Bandwidth
$serv1 = new SCXML;

// Set Server vars
$serv1->set_host("24.82.34.152");
$serv1->set_port(8000);
$serv1->set_password("PASSWORD");

// Get Server Info
$sngtitle=$serv1->fetchMatchingTag("SONGTITLE");
// turn %20 into spaces
$newtitle=explode("%20",$title);
$newtitle=implode(" ",$newtitle);

// Turn %6o into apostrophe
$newsong=explode("%20",$sngtitle);
$newsong=implode(" ",$newsong);
// Turn %6o into apostrophe
$newsong=explode("%60",$newsong);
$newsong=implode("'",$newsong);
// Turn %2C into comma
$newsong=explode("%2C",$newsong);
$newsong=implode(",",$newsong);
// Turn %26 into amperstand
$newsong=explode("%26",$newsong);
$newsong=implode("&",$newsong);
// Turn %28 into left parens
$newsong=explode("%28",$newsong);
$newsong=implode("(",$newsong);
// Turn %29 into right parens
$newsong=explode("%29",$newsong);
$newsong=implode(")",$newsong);
// Turn %3F into Question Marks
$newsong=explode("%3F",$newsong);
$newsong=implode("?",$newsong);
// Turn %23 into Pound Signs
$newsong=explode("%23",$newsong);
$newsong=implode("#",$newsong);
//build html

// Output Current Song
echo "Current Song:";
echo "$newsong";
echo "<BR>";

?>
nodlive is offline   Reply With Quote
Old 10th December 2001, 18:43   #15
nicadams
Junior Member
 
Join Date: May 2001
Location: Detroit MI
Posts: 19
Send a message via ICQ to nicadams Send a message via AIM to nicadams
// Get Server Info
$sngtitle=$serv1->fetchMatchingTag("SONGTITLE");
// turn %20 into spaces
$newtitle=explode("%20",$title); /*try $sngtitle as a variable, since it's the one you've called out above*/
$newtitle=implode(" ",$newtitle);

Make sure all your variable names are correct.. when i originally copied & pasted all that code here, I scissored out some stuff for other servers. (lazy.) aparently i cant copy & paste right... i end up eating some paste.

-nick
nicadams is offline   Reply With Quote
Old 10th December 2001, 20:53   #16
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
as far as i can tell all my variables are correct...

any chance of getting your exact script?

nodlive
nodlive is offline   Reply With Quote
Old 10th December 2001, 21:40   #17
Tom
Moderator
 
Join Date: Apr 2000
Posts: 4,491
If all you want is the current title it may be easier to just write a script to grab it from http://IP:PORT/ and not mess with the XML. Also if Apache is on the same box as the DNAS check out the Extended Logging part of the config for the DNAS.

Tom

Tom is offline   Reply With Quote
Old 10th December 2001, 21:51   #18
nicadams
Junior Member
 
Join Date: May 2001
Location: Detroit MI
Posts: 19
Send a message via ICQ to nicadams Send a message via AIM to nicadams
Here's my exact code, without my passwords. It's damn near the same thing that I posted before. The conversion of html junk to plain text is different, thanks to BHala.

Get me on AIM or ICQ (In profile) if you're havin more problems.

-Nick

<?
/* Nick Adams
05.16.01
*/

require "scxml-obj.phps";

// New Object for High Bandwidth
$serv1 = new SCXML;

// Set Server vars
$serv1->set_host("localhost");
$serv1->set_port(8000);
$serv1->set_password("somepass");

// Bomb if errors
if (!$serv1->retrieveXML()) DIE ("Server Down. Cannot fetch XML, exiting\n");


// Get High-Bandwidth Server Info
$cur_listen=$serv1->fetchMatchingTag("CURRENTLISTENERS");
$peak_listen=$serv1->fetchMatchingTag("PEAKLISTENERS");
$title=$serv1->fetchMatchingTag("SERVERTITLE");
$sngtitle=$serv1->fetchMatchingTag("SONGTITLE");
$totaltime=$serv1->fetchMatchingTag("AVERAGETIME");

$con_title=$serv1->fetchMatchingArray("TITLE");

if ($cur_listen = "")
$cur_listen = 0;

// Calculate Bandwidth used.
$streaming = ($cur_listen * 6) + 6;
$maxband = 15;
$precentused = ($streaming / $maxband) * 100;

// Calculate User Counts
$lowcount = ($peak_listen /3);
$medcount = ($peak_listen / 3) * 2;

// code to change user count colors based on usage
if (($cur_listen >= 0) && ($cur_listen <= $lowcount))
$coloruser = "009900";
elseif (($cur_listen > $lowcount) && ($cur_listen <= $medcount))
$coloruser = "FF6600";
else
$coloruser = "FF0000";

// code to change bandwidht colors based on usage
if (($precentused >= 0) && ($precentused < 20))
$colorband = "009900";
elseif (($precentused >= 20) && ($precentused <= 75))
$colorband = "FF6600";
else
$colorband = "FF0000";


// Calculate Hrs, Min, and Sec. Listening Time
$hours = $totaltime / 3600;
$temptime = $totaltime % 3600;
$minutes = $temptime / 60;
$seconds = $temptime % 60;

// Convert to INTs
settype($hours, integer);
settype($minutes, integer);
settype($seconds, integer);

$search = array("%20","%60","%2C","%26","%28","%29","%3F","%23","%5D","%5B","%21","%7E");
$replace = array(" ", "'", ",", "&", "(",")", "?", "#","]","[","!","~");

$sngtitle = str_replace($search, $replace, $sngtitle);

//build html

// Output Current Song
echo "<font face=\"Tahoma\">Current Song:<br>";
echo "<font color=\"FFFFFF\"><font size=-1>$sngtitle</font></font>";
echo "<BR>";

// Output Listener counts
echo "Listener Count: <font color=\"$coloruser\"><font size=-1>$cur_listen Current. $peak_listen Peak.</a></font></font>";
echo "<BR>";

// Output Listener Time
echo "Average Listener Time: <font color=\"FFFFFF\"><font size=-1>$hours Hr. $minutes Min.</a></font></font>";
echo "<BR>";

// Output Bandwidth Used
echo "Bandwidth Used: <font color=\"$colorband\"><font size=-1>$precentused %</a></font></font>";
echo "<BR>";
// ~fin~
?>
nicadams is offline   Reply With Quote
Old 10th December 2001, 22:11   #19
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
nada.. doesnt work... AGH!

could it be something in my config file?

i dunno.. im thinking that i wont worry about it...
nodlive is offline   Reply With Quote
Old 10th December 2001, 23:38   #20
Jay
Moderator Alumni
 
Jay's Avatar
 
Join Date: May 2000
Location: Next Door
Posts: 8,942
Try this, it's much simpler and doesn't require pasing passwords, which can be unsecure.
http://www.phpcast.com/source-viewer.php?code=cs.php
Jay is offline   Reply With Quote
Old 13th December 2001, 00:54   #21
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
ok..
the pointer to the xml page ($sp from tom's code) comes up as Resource id #1

what is that?

nodlive
nodlive is offline   Reply With Quote
Old 13th December 2001, 02:12   #22
nodlive
Junior Member
 
Join Date: Dec 2001
Posts: 22
happy happy happy happy!

ok! ive figured it out...

it works! yeah! yAHOO!!!

did my own little thing.. and it works!

<?

$host = "ze host";
$port = "8000";
$password = "la password-o";

//connect to the socket
$sp=fsockopen($host,$port,&$errno,&$errstr,10);


// point sp at the file
fputs($sp,"GET /admin.cgi?pass=$password&mode=viewxml HTTP/1.1\nUser-Agent:Mozilla\n\n");

//read the file
$sp_data=fread($sp,31337);

//ADD 54 TO YOUR INDEX IN NUMBER
// the first 54 lines are junkish...

// seperate the rest of the file into a line array
$info = explode("\n", $sp_data);

// grab the song info
$SongInfo = explode("-", $info[63]);


/echo it out!
echo "<br>Song title line : $SongInfo[1]";
echo "<br>Song artist line : $SongInfo[0]";


// save useful bits into a txt file
$fname = "c:\web\\test\songinfo.txt";

$fp = fopen ($fname, "w+");
for($i=54; $i<125; $i++) {
fwrite ($fp, "$info[$i]\r\n");
}
fclose ($fp);

?>

probably not the most elegant solution, since it will have to change if the xml stats change, but it works. and its simple.. ahh..

thanks everyone for your help!

nodlive
nodlive is offline   Reply With Quote
Old 22nd October 2003, 11:40   #23
kolobok
Junior Member
 
Join Date: Oct 2003
Location: Dance Planet
Posts: 12
I'm use this code for generate HTML playlist,

<?

require("scxml-grabber.php");

// New Object for High Bandwidth
$serv1 = new SCXML;

// Set Server vars
$serv1->set_host("xxx");
$serv1->set_port(8000);
$serv1->set_password("xxx");

// Get Server Info
$sngtitle=$serv1->fetchMatchingTag("SONGTITLE");
// turn %20 into spaces
$newtitle=explode("%20",$title);
$newtitle=implode(" ",$newtitle);

// Turn %6o into apostrophe
$newsong=explode("%20",$sngtitle);
$newsong=implode(" ",$newsong);
// Turn %6o into apostrophe
$newsong=explode("%60",$newsong);
$newsong=implode("'",$newsong);
// Turn %2C into comma
$newsong=explode("%2C",$newsong);
$newsong=implode(",",$newsong);
// Turn %26 into amperstand
$newsong=explode("%26",$newsong);
$newsong=implode("&",$newsong);
// Turn %28 into left parens
$newsong=explode("%28",$newsong);
$newsong=implode("(",$newsong);
// Turn %29 into right parens
$newsong=explode("%29",$newsong);
$newsong=implode(")",$newsong);
// Turn %3F into Question Marks
$newsong=explode("%3F",$newsong);
$newsong=implode("?",$newsong);
// Turn %23 into Pound Signs
$newsong=explode("%23",$newsong);
$newsong=implode("#",$newsong);
//build html

// Output Current Song
echo "Current Song:";
echo "$newsong";
echo "<BR>";

?>

but I receive from server this error:

Fatal error: Allowed memory size of 15728640 bytes exhausted at (null):0 (tried to allocate 11520 bytes in /home/virtual/site373/fst/var/www/html/scxml-grabber.php on line 26

Help PLS!!!
kolobok is offline   Reply With Quote
Old 22nd October 2003, 11:54   #24
kolobok
Junior Member
 
Join Date: Oct 2003
Location: Dance Planet
Posts: 12
I'm use this code for generate HTML playlist,

<?

require("scxml-grabber.php");

// New Object for High Bandwidth
$serv1 = new SCXML;

// Set Server vars
$serv1->set_host("xxx");
$serv1->set_port(8000);
$serv1->set_password("xxx");

// Get Server Info
$sngtitle=$serv1->fetchMatchingTag("SONGTITLE");
// turn %20 into spaces
$newtitle=explode("%20",$title);
$newtitle=implode(" ",$newtitle);

// Turn %6o into apostrophe
$newsong=explode("%20",$sngtitle);
$newsong=implode(" ",$newsong);
// Turn %6o into apostrophe
$newsong=explode("%60",$newsong);
$newsong=implode("'",$newsong);
// Turn %2C into comma
$newsong=explode("%2C",$newsong);
$newsong=implode(",",$newsong);
// Turn %26 into amperstand
$newsong=explode("%26",$newsong);
$newsong=implode("&",$newsong);
// Turn %28 into left parens
$newsong=explode("%28",$newsong);
$newsong=implode("(",$newsong);
// Turn %29 into right parens
$newsong=explode("%29",$newsong);
$newsong=implode(")",$newsong);
// Turn %3F into Question Marks
$newsong=explode("%3F",$newsong);
$newsong=implode("?",$newsong);
// Turn %23 into Pound Signs
$newsong=explode("%23",$newsong);
$newsong=implode("#",$newsong);
//build html

// Output Current Song
echo "Current Song:";
echo "$newsong";
echo "<BR>";

?>

but I receive from server this error:

Fatal error: Allowed memory size of 15728640 bytes exhausted at (null):0 (tried to allocate 11520 bytes in /home/virtual/site373/fst/var/www/html/scxml-grabber.php on line 26

Help PLS!!!
kolobok is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Shoutcast > Shoutcast Discussions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump