Get Artist Info

Ah right ok, I understand now.

We await Torben :slight_smile:

…who is awaiting his hernia repair surgery tommorrow :frowning:

…ah…then i can wait :wink:

Will you have plenty of time while you rest after the operation? haha! :stuck_out_tongue:

Ryan: Watch it, chum! Some of us here have had five or six hernias! Your turn may come one day! :stuck_out_tongue:

OK, looks like there isn’t a ‘string replace’ function in mAirList script, so I’ve written you one:

// StringReplace
// Written by Cad Delworth CEng MBCS CITP
// (Clearances Manager, Leith FM, Edinburgh, Scotland)

// A mAirList script function which replaces characters in a string.

// Copy the function below into any script which requires a string replace function.

function Replace(sSource: string; sFind: string; sReplace: string): String;

var
  iLenFind, iPos: integer;
  sString, sOutput: string;

begin

  // Store the length of the 'find' string
  iLenFind := Length(sFind);

  // The default output string is the input string
  // (in case no matches are present)
  sString := sSource;

  // Now search repeatedly for sFind until it isn't there any more
  repeat

    // Are there any 'find' strings in sString?
    iPos := Pos(sFind, sString);

    if iPos > 0 then
    begin

      // Yes, form a 'replaced' string by string slicing
      sOutput := Copy(sString, 1, iPos - 1) + sReplace;
      sOutput := sOutput + Copy(sString, iPos + Length(sFind), Length(sString) - iPos + Length(sFind));

      // Now copy sOutput back into sString for the next loop
      sString := sOutput;
    end;

  until iPos = 0;

  // We're finished, return the output string
  Result := sString;

end;

begin

  // This is merely a test for the Replace function.
  // Change the line below to verify that it works.
  SystemLog(Replace('This is what you need!', ' ', '%20'));

end.

Since you don’t seem to be familiar with Delphi/Pascal, you need to copy the function (the line beginning function up to the end; line) BEFORE the ‘main’ part of your program, as shown above. You can then call it like any other function, to replace any character/sequence of characters in a string with a different character/sequence of characters. As you’ll see when you run the example, one call will replace ALL the occurrences.

Feel free to remove all the blank lines and comments (// lines) within your copy of the function, once you’ve convinced yourself that it works. You might want to also keep a copy of the original so you can understand it all later. Or not.

Torben: Best of luck with the op.!

BFN
CAD

I just tried that script out Cad. No errors, and the script works still for songs that don’t have apostrophes in so far, which is good.

But, when i played Ben’s Brother as an example, i loaded the page and it shows as blank- a step forward though than showing PHP errors. We are making progress!

Can you just review my script to make sure i haven’t done it wrong (which i must have!!)-Thanks-

[code]
// StringReplace
// Written by Cad Delworth CEng MBCS CITP
// (Clearances Manager, Leith FM, Edinburgh, Scotland)

// A mAirList script function which replaces characters in a string.

// Copy the function below into any script which requires a string replace function.

function Replace(sSource: string; sFind: string; sReplace: string): String;

var
iLenFind, iPos: integer;
sString, sOutput: string;

begin

// Store the length of the ‘find’ string
iLenFind := Length(sFind);

// The default output string is the input string
// (in case no matches are present)
sString := sSource;

// Now search repeatedly for sFind until it isn’t there any more
repeat

// Are there any 'find' strings in sString?
iPos := Pos(sFind, sString);

if iPos > 0 then
begin

  // Yes, form a 'replaced' string by string slicing
  sOutput := Copy(sString, 1, iPos - 1) + sReplace;
  sOutput := sOutput + Copy(sString, iPos + Length(sFind), Length(sString) - iPos + Length(sFind));

  // Now copy sOutput back into sString for the next loop
  sString := sOutput;
end;

until iPos = 0;

// We’re finished, return the output string
Result := sString;

end;

// Intelligent Now Playing Script v2
// by Charlie Davy
// last modified 16th January 2009

// This script examines each audio item played via a Player and performs a task based upon the track’s length.
// The 3 settings below work for most situations, however you can fine-tune them if you wish.

// The last.fm links work for most artist/groups - it adds a nice touch, I think.

// Update: The OnAir switch is considered within this script, if you are “Off Air”, this script does nothing.
// If you are “On Air”, the script runs as normal. Useful in a multi-studio environment where pre-recorded
// shows may interfer with your live “now playing” display!

// IMPORTANT!! You MUST rename this file to .mls when adding it to mAirListConfig

const
IDENT = 900000000; // 90 seconds
MUSIC = 900000000; // 90 seconds
PRE_REC = 4200000000; // 7 minute

procedure OnPlayerStart(PlayerControl: IPlayerControl; Item: IPlaylistItem);
var sl: TStringList;

begin
if Engine.GetOnAir = False then begin
SystemLog(‘mAirList is in production mode, so no action taken…’);
sl := TStringList.Create;
sl.Add(‘LiveWire Radio’);
sl.SaveToFile(‘C:\Users\Ryan\Documents\HOMEWORK_RADIO\site\nowplaying\nowplaying.php’);
sl.Free;

// If the track is over 7 minutes, assume it’s a pre-recorded programme

end else if (Item.GetDuration > PRE_REC) then begin

sl := TStringList.Create;
sl.Add(‘More music soon’);
sl.SaveToFile(‘C:\Users\Ryan\Documents\nowplaying\nowplaying.php’);
sl.Free;
SystemLog(‘This item is a pre-record or longer than 7 minutes…’);

// If the track is more than 90 seconds, assume it’s a song

end else if (Item.GetDuration > MUSIC) then begin

sl := TStringList.Create;
sl.Add(’<?php’);
sl.Add(Replace(‘if (!$xmlObj=simplexml_load_file ( "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=’ + Item.GetArtist + ‘&api_key=097a7ab0f5b56a894eb7a25e1c51b0da" )){’, ‘’’’, ‘%27’));
sl.Add(‘echo “Error reading XML file”;’);
sl.Add(’}’);
sl.Add(‘foreach($xmlObj as $artist){’);
sl.Add(‘echo "Price : " . $artist->name . “
”; ‘);
sl.Add(‘echo "Price : " . $artist->image . “
”;’);
sl.Add(‘echo "Price : " . $artist->bio->summary . “
”;’);
sl.Add(’}’);
sl.Add(’?>’);
sl.SaveToFile(‘C:\Users\Ryan\Documents\HOMEWORK_RADIO\site\nowplaying\artist.php’);
sl.Free;

sl := TStringList.Create;
sl.Add(’<?php’);
sl.Add(‘if (!$xmlObj=simplexml_load_file ( "http://ws.audioscrobbler.com/2.0/?method=track.getinfo&artist=’ + Item.GetArtist + ‘&track=’ + Item.GetTitle + ‘&api_key=097a7ab0f5b56a894eb7a25e1c51b0da" )){’);
sl.Add(‘echo “Error reading XML file”;’);
sl.Add(’}’);
sl.Add(‘foreach($xmlObj as $track){’);
sl.Add(‘echo "Price : " . $track->name . “
”; ‘);
sl.Add(‘echo "Price : " . $track->wiki->content . “
”; ‘);
sl.Add(’}’);
sl.Add(’?>’);
sl.SaveToFile(‘C:\Users\Ryan\Documents\HOMEWORK_RADIO\site\nowplaying\track.php’);
sl.Free;

sl := TStringList.Create;
sl.Add(’’ + Item.GetArtist + ’ - ’ + Item.GetTitle + ‘’);
sl.SaveToFile(‘C:\Users\Ryan\Documents\HOMEWORK_RADIO\site\nowplaying\nowplaying.php’);
sl.Free;
SystemLog('Now Playing… ’ + Item.GetArtist + ’ - ’ + Item.GetTitle);

end;
end;
begin
end.[/code]

Ryan, you’d genuinely be best to ask Charlie or some other Web guru to check out your script at this point. My knowledge of PHP is nil and I plan to keep it that way, thanks. :smiley:

The WWW is genuinely not my area of expertise (I like writing Windows utilities, preferably in Visual Basic, or in Delphi/Pascal when I really have no alternative), so you’ll need to ask someone who knows about 't Web to take it from here.

BFN
CAD

Ah right OK then.

Other than that problem, it works great.

Thanks for the help Cad- and happy 1000 posts!

Ryan.

Thanks, Ryan (1,000 posts, eh?!!).

And sorry I don’t have the expertise to take the thing to conclusion for you.

BFN
CAD

Hi Ryan,
I’m a little busy at the moment and haven’t got a local php server, so I haven’t got the time to try out the script, but if you post the php scripts your mAirlist script generates (the one when it works, as well as the one when you play Ben’s Brother), I should be able to figure out what’s wrong.

For our station we’re taking a slightly different route:
All our mAirlist script does is sending a GET request to our php script, containing artist and title, something like this:
HTTPGet(‘http://www.yourwebpage.com/updatemetadata.php?artist=’ + pi.GetArtist + ‘&title=’ + pi.GetTitle);
All the tidying and querying of external sites is dealt with in the php script, which I personally find a lot easier than using mAirlistscript…

Hi.

Just been having a fiddle now still with the same problem of returning a blank with the PHP script.

If you go to the following link- http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Ben's Brother&api_key=097a7ab0f5b56a894eb7a25e1c51b0da

…you’ll see that it works perfectly and returns all the info from last.fm. But- when opening the PHP script which mAirList generates, it returns the blanks. Her is the PHP file generated-

<?php if (!$xmlObj=simplexml_load_file ( "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Ben%27s Brother&api_key=097a7ab0f5b56a894eb7a25e1c51b0da" )){ echo "Error reading XML file"; } foreach($xmlObj as $artist){ echo "Price : " . $artist->name . "<br />"; echo "Price : " . $artist->image . "<br />"; echo "Price : " . $artist->bio->summary . "<br />"; } ?>

You may notice that the ‘XML’ file is exactly the same link i posted above, which works fine, yet when running the above, it returns:

Price:
Price:
Price:

(Price is just being used for testing purposes at the moment)In theory, it should instead be showing something similar to the following:

Price: Ben’s Brother
Price: [imagehere.jpg]
Price: Some info about the artist here from last.fm blah blah.

Strange! Seems to be the PHP script not recognising something and retuning blanks!

Thanks, Ryan.

My suspicion is that your php server is set up to not allow simplexml_load_string() to connect to external URLs.
Try to go to http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Ben’sBrother&api_key=097a7ab0f5b56a894eb7a25e1c51b0da
save the resulting xml file, and upload it to your web server.
Then try replacing the remote URL with your local URL and see if that works.
If it does, then you’ll need to find a way of locally caching the remote xml file (possibly using fopen() or file_get_contents(), but I’m not too sure myself…)

Hmmm…

It seemed to work when using the following without the %27 replacing the apostrophe, which ironically caused the problem in the frst place. Browsers recognise to replace it with an apostrphe, but the PHP script doesn’t.

http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Ben'sBrother&api_key=097a7ab0f5b56a894eb7a25e1c51b0da

Does the PHP script also need a replace function to replace %27 with an apostophe?

Thanks, Ryan.

you should be able to use the rawurldecode() function to convert URL entities into literal characters.
it might also be worth having a read of this page for some more in-depth explanations.

Still going with this guys haha!

I was wondering if I could go about it another way.

I was thinking maybe using HTTPGet in the mls script, and saving the contents of what it retrieves from last.fm (an xml file), to a file. Is this possible, or am I not quite grasping what HTTPGet would do?

Cheers, Ryan.

We use last.fm and the way we do it is the following…

theres a constantly running php cli script which detects changes to our source, and we use the HTTP GET log function in mAirList to send it stuff.

Then we just use file_get_contents on the last.fm url, which you’ve managed to fiddle with, and with the resulting XML code, I found an XML parser, which converts XML to an Array, and then just deal with the multdimensional array to extract the data I need.

http://www.bin-co.com/php/scripts/xml2array/ is the function I use.

Scrobbling to Last.FM (which we also do) is a completely different kettle of fish though!!

Hi.

(Directed at Cad)

With the replace function you made a while back now…i need to know how it is possible to have more than one replace within the same replace function. With below, it replaces any apostrophes with %27, but i also need to add a replace of any spaces to be %20. I’ve had a fiddle myself multiple times but i simply cannot get it to work. Here is the bit of code i need to add the space replace onto along with the apostrophe replace.

sl.Add(Replace('echo rawurldecode("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&artist=' + Item.GetArtist + '&track=' + Item.GetTitle + '&api_key=097a7ab0f5b56a894eb7a25e1c51b0da");', '''', '%27'));

Thanks, Ryan.

  1. Use a separate string variable (name it something like strOut), then build your big long pre-Replace string there, instead of directly Adding it to your stringlist.
  2. Run all the Replaces you need, one at a time. (strOut := Replace(strOut, ‘’’’, '%27); and so forth)
  3. AFTER all the Replaces are done, Add strOut to sl (sl.Add(strOut):wink:

    BFN
    CAD

Surprisingly that makes some sense to me for once haha!

But…i think i don’t need to add another replace function for %20, because i found out it was a different thing causing the problem, but i was trying to narrow it down!!

Thanks for the muchly appreciated help, Cad!

Cheers, Ryan.