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)
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.
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.
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.