Loading a playlist via HTTP Get

Hi

I want to know if it’s possible to load a playlist somehow via HTTP.

begin
	CurrentPlaylist.LoadFromFile('http://localhost/test.mlp');
end.

I get this error:

Error loading playlist: Cannot open file "C:\Users\test\Desktop\http:\localhost\test.mlp".

I’m assuming that LoadFromFile assumes that the path provided is a local file path, not http, hence it seems to be trying to find a folder called http: etc. (the .mls file was on the desktop)

Is there a way to do purely using a script?

Thanks
George

I believe this is possible. Cannot remember the syntax though (and I’m away from my office computer). I’ll look it up as soon as possible.

Thanks for looking into this Torben… would like to get this working asap.

Thanks
George

For .mlp files, the following script might work:

[code]
var
doc: IXMLDocument;

begin
doc := CreateXMLDoc;
doc.LoadXML(HTTPGet(‘http://someserver/test.mlp’));
CurrentPlaylist.LoadFromXML(doc.GetDocumentElement);
end.
[/b]

I cannot test it from where I am right now. Just give it a try.[/code]

Got an error trying to run it…

Error running script: Runtime error: (7,2) Exception: Interface not supported

I’ll have a play around in the mean time.

Hm. I think I have to test it myself. Will do so and report back.

Any luck yet Torben?

Sorry, it took a while until I figured out what’s going on there - and thanks for the reminder.

You need to add another line to make it work:

var
  doc: IXMLDocument;

begin
  doc := CreateXMLDoc;
  doc.SetPreserveWhitespace(false);
  doc.LoadXML(HTTPGet('http://someserver/test.mlp'));
  CurrentPlaylist.LoadFromXML(doc.GetDocumentElement);
end.

Without the “SetPreserveWhitespace(false)”, the XML parser will treat the line breaks as data, which prevents mAirList from finding the actual PlaylistItem nodes.

Thanks Torben that works… but is there a way to stop it blanking the playlist already loaded if the playlist it loads is either empty or the web server returned a 404…

Currently if the file doesn’t exist on the web server, the old playlist disappears anyway, and I’m assuming it tries to parse the content of the 404 page as an MLP?

It’d be better if I could get it working so that if it 404s then the script just stops running and doesn’t change anything.

Thanks for your help

George

A 404 should usually raise an exception and stop the script execution before the current playlist gets overwritten.

In any case, you could first load the XML into a temporary playlist, and if it’s ok, then copy it over to the actual playlist.

var
  doc: IXMLDocument;
  tempList: IPlaylist;

begin
  doc := CreateXMLDoc;
  doc.SetPreserveWhitespace(false);
  doc.LoadXML(HTTPGet('http://someserver/test.mlp'));

  tempList := Factory.CreatePlaylist;
  tempList.LoadFromXML(doc.GetDocumentElement);

  if tempList.GetCount = 0 then begin
    SystemLog('Playlist loaded from server was empty!');
    exit;
  end;

  CurrentPlaylist.Assign(tempList);
end.

Only just noticed that the 404 was going through as an exception.

What would be the easiest way to (if its possible at all) to catch the exception gracefully and log that there was not a playlist available?

The script is fully functional now, just wanted to see if I could log times when it gets a 404 instead of having exceptions thrown…

Thanks

George

The exception gets logged automatically when the script runs in the background (from the event scheduler etc.).

Alternatively, you can wrap the statement (or the whole code block - whatever you prefer) - into a try…except…end block:

try
  doc.LoadXML(HTTPGet('http://someserver/test.mlp'));
except
  SystemLog('Loading the mlp file failed.');
end;

I believe it is possible to access and display the error message in the except block as well. The syntax is different from Delphi though. I have to look it up in case you’re interested.

Thanks Torben, I’d literally just got a try,except,end block working before I’d seen your response.

Thanks non-the-less :slight_smile: