Jukebox script

Hi,

in case anyone is interested, here’s a small script I wrote that turns mAirList into a “jukebox”. It will wait until there are less than three items in the playlist, and then add random items from one or more mAirList folders.

Note that these are a really purely random items, there is nothing like artist separation, “last use” check etc.

To use this script, you have to look up the IDs of music/jingle folders in your DB Library (folder properties dialog) and adjust the “AddRandomItemsFromFolder” lines at the bottom accordingly. Then save the script as e.g. jukebox.mls and add it as a Notification Script in mAirListConfig.

{
  jukebox.mls

  Adds random items from specific mAirListDB folders when the playlist content
  drops below a certain threshold.

  Written by Torben Weibert <tw@mairlist.com> 2014-02-21

}


// This is the function that will add the requested number of items from
// the specified folder to the playlist - the folder ID can be looked up
// in the DB Library: right-click folder, properties, see dialog window title
function AddRandomItemsFromFolder(FolderID: string; Count: integer): IPlaylistItem;
var
  items: IPersistentArray;
  item: IPlaylistItem;
  i: integer;
begin
  items := IDBConnection(Database(0)).ProcessRequest('GET', '/folders/' + FolderID + '/items/', nil, nil).AsArray;
  if items.GetCount = 0 then begin
    // Folder is empty, insert dummy as error message
    item := Factory.CreateDummyPlaylistItem;
    Result.SetTitle('Folder ' + FolderID + ' is empty!');
    CurrentPlaylist.Add(item);
    exit;
  end;
  for i := 0 to Count - 1 do
    CurrentPlaylist.Add(Database(0).CreatePlaylistItem(items.GetItem(Random(items.GetCount)).AsObject.FindString('_id', 'ID not found!')));
end;

// Executed on load, enables timer
procedure OnLoad;
begin
  EnableTimer(1000); // call OnTimer every second
end;

// This will be executed once per second
procedure OnTimer;
begin
  // If the remaining number of items drops below 3, add new content
  if CurrentPlaylist.GetRemainingCount < 3 then begin
    AddRandomItemsFromFolder('4', 2);   // two items from folder 4 (music)
    AddRandomItemsFromFolder('10', 1);  // one item from folder 1 (jingle)
  end;
end;

begin
end.

Technical background:

The script queries the database for the list of items in the specific folder (GET /folder//items/), and from the list of items returned, picks a random one and uses CreatePlaylistItem to create and add it to the playlist.

Hi Torben, is it possible to pin this to top of thread so its always visible. Ideal for a quick introduction to new starters.

Is it possible to post an update of the “Jukebox” script please? It doesn’t seem to work anymore in 6.1. I’ve tried to find the corresponding objects in the mAirListScript.chm, but i’m a bit lost. Thanks!