This page is loading weirdly (Seems like it is firing 3 loadFinished
events! Anyhow, the following code works:
// "Normal" JS
function waitForMetadata() {
// Initialize global meta
var meta = page.evaluate(function() {
return document.getElementById("metadata_player")
});
var txt = meta.innerHTML;
console.log("meta: '" + meta.outerHTML + "'")
if (txt != "") {
phantom.exit(0);
} else {
setTimeout(waitForMetadata, 1000);
}
}
// PhantomJS
var page = require('webpage').create();
page.open('http://player.rockfm.fm/')
page.onLoadFinished = function(status) {
console.log("Status: " + status);
if(status !== "success") {
console.log("FAIL!")
phantom.exit(1);
}
waitForMetadata();
};
The first part is a function that checks the contents of the div
and if it is empty it schedules itself, else prints and exits. The second part is straight out of phantomJS tutorial: declares a page, registers an onLoad function and loads it.
Example output:
urban@kde-2:/tmp$ phantomjs ./test.js
Status: success
meta: '<div id="metadata_player"></div>'
Status: success
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player"></div>'
meta: '<div id="metadata_player">GUNS N' ROSES<br><span id="artist">KNOCKIN' ON HEAVEN'S DOOR</span></div>'
NOTE: Once the content is loaded, with JS you can do whatever you like (instead of printing). Also, I think you want to use the span id=artist
later on...
UPDATE 1:
This made me stubborn... I could not make it with with phantomjs
however, I inspected the ajax call this page makes and it seems that you can get the currently playing song with:
$ curl 'http://bo.cope.webtv.flumotion.com/api/active?format=json&podId=78'
{"id": null, "uuid": "DFLT", "value": "{\"image\": \"\", \"author\": \"AEROSMITH\", \"title\": \"AMAZING\"}"}
This means you can do use any language you like and json_decode
twice: (1) for the outer map having id
, uuid
and value
and (2) decode the value
. My only concern would be if podId
changes... but is seems static.
Hope it helps