Script voor automatisch inlezen categorien

Voor vragen over Playlist Automation Language.
Plaats reactie
Ski

Script voor automatisch inlezen categorien

Ongelezen bericht door Ski »

FiPo,

Mijn eerste post hier als newby :D Ik heb hier XBMC als media center maar ik vond dat ik de instellingen niet genoeg naar eigen wens kon instellen dus ik heb besloten om maar eens aan de gang te gaan met SAM.

Als eerste wil ik vertellen dat ik hier heeeel veel informatie vandaan heb kunnen halen en SAM voldoet precies aan mijn verwachtingen!

Ik heb al mijn MP3'tjes volledig getagged met genre, jaar etc . en mijn MP3'tjes staan in één map, dus ik wilde een script maken dat automatisch kijkt of er nieuwe MP3's staan in die map en automatisch de juiste categorie daarvoor aanmaakt. Ook wilde ik dat als er nieuwe, zeg actuele "hits" waren dat die in een aparte high rotation categorie kwamen. Nou is dat redelijk gelukt en vanwege het feit dat ik hier veel informatie heb gevonden wilde ik het graag met jullie delen en ik hoop dat jullie er wat aan hebben!

Code: Selecteer alles

// The music directory.
var MusicDirectory : string = 'C:\Muziek';

// The name of the Current Hits category.
var CurrentHitsCategoryName : string = 'Current Hits';

// The maximum age for adding to the current hits category.
var MaximumAgeOfCurrentHits : integer = 30;

// Make the PAL run fast!
PAL.LockExecution;

// Rescan music directory.
DIR[MusicDirectory].Rescan;

// Declare variables
var genreData, decadeYears, theSongList, categoryData, currentHits : TDataSet;

// Variable needed of we want the new songs added to the hits category;
var addToCurrentHits : boolean = false;

// Used to hold  the decade.
var decade, categoryName, actualCategoryName : string;

// Set the decades to use.
var YearList		: TStringList;
YearList := TStringList.Create;

// We will add a 0 later to it so we can create the 70, 80, 90, 00 and the 10.
YearList.Add('197');
YearList.Add('198');
YearList.Add('199');
YearList.Add('200');
YearList.Add('201');
// Ugly hack!
YearList.Add('210');

var YearListIndex : integer;

// Required for getting the current year and day.
var theYear : Integer = 0;
var theMonth : Integer = 0;
var theDay : Integer = 0;
DecodeDate(now, theYear, theMonth, theDay);

// First create/get the CurrentHitsCategoryName ID.
Writeln('Category name = ' + QuotedStr(CurrentHitsCategoryName));

begin
currentHits := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(CurrentHitsCategoryName), [], True);
currentHits.First;
if not currentHits.EOF then
   begin
      Writeln('Found ID: ' + QuotedStr(IntToStr(currentHits['id'])));
      // Remove the category items from SAM.
      Writeln('Clearing category ' +  QuotedStr(CurrentHitsCategoryName));
      ExecSQL('DELETE FROM categorylist WHERE categoryID = ' + IntToStr(currentHits['id']),[]);
   end
else
   begin
      currentHits.Free;
      Writeln('Category does not exists, creating category ' + QuotedStr(CurrentHitsCategoryName));
      ExecSQL('INSERT INTO category (name) VALUES (' + QuotedStr(CurrentHitsCategoryName) + ')', []);
      currentHits.Free;
      currentHits := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(CurrentHitsCategoryName), [], True);
   end
end;


// Second get all genres.
genreData := Query('SELECT DISTINCT genre FROM songlist WHERE genre IS NOT NULL AND genre IS NOT NULL AND genre <> ""', [], True);

genreData.First;
while not genreData.EOF do
   begin
      Writeln('Current genre = ' + QuotedStr(genreData['genre']));

          // Create the decades from each genre.
          YearListIndex := 0;
          while YearListIndex < YearList.Count - 1 do
             begin
                 // Remember that we do while < so here it is not -1 but -2!
                 addToCurrentHits := (YearListIndex = (YearList.Count - 2));

                 decade := CharAt(YearList.Get(YearListIndex), 3) + '0';
                 Writeln('Current decade = ' + QuotedStr(decade));
                 
                 // Get the combined category name.
                 categoryName := genreData['genre'] + ' ' + decade;
                 Writeln('Category name = ' + QuotedStr(categoryName));
                 
                 // Try to find the ID of the category.
                 categoryData := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(categoryName), [], True);
                 categoryData.First;
                 if not categoryData.EOF then
                 begin
                    Writeln('Found ID: ' + QuotedStr(IntToStr(categoryData['id'])));

                    // Remove the category items from SAM.
                    Writeln('Clearing category ' +  QuotedStr(categoryName));
                    ExecSQL('DELETE FROM categorylist WHERE categoryID = ' + IntToStr(categoryData['id']),[]);
                 end
                 else
                 begin
                    categoryData.Free;
                    Writeln('Category does not exists, creating category ' + QuotedStr(categoryName));
                    ExecSQL('INSERT INTO category (name) VALUES (' + QuotedStr(categoryName) + ')', []);
                    categoryData.Free;
                    categoryData := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(categoryName), [], True);
                 end;

                 // Find all songs with the genre and the right decade.
                 begin
                 theSongList := Query('SELECT songlist.ID, date_added, albumyear FROM songlist WHERE songlist.genre LIKE ' + QuotedStr(genreData['genre']) + ' AND albumyear >= ' + QuotedStr(YearList.Get(YearListIndex)) + ' AND albumyear < ' + QuotedStr(YearList.Get(YearListIndex + 1)), [], True);
                 end;
                 
                 begin
                 if  addToCurrentHits and (StrToInt(theSongList['albumyear']) = theYear) and StrToDateTime(theSongList['date_added']) > (Now - MaximumAgeOfCurrentHits) then
                    begin
                    Writeln('Find new hit.');
                    actualCategoryName := IntToStr(currentHits['id']);// CurrentHitsCategoryName;
                    end
                 else
                    actualCategoryName := IntToStr(categoryData['id']);
                 end;
                 
                    WriteLn('Updating category ' + QuotedStr(categoryName));
                    theSongList.First;
                    while not theSongList.EOF do
                          begin
                          ExecSQL('INSERT into categorylist (songID, categoryID) VALUES (' + IntToStr(theSongList['ID']) +  ', ' + actualCategoryName + ')',[]);
                          theSongList.Next;
                          end;


                 // Clear the dataset.
                 theSongList.Free;
                 categoryData.Free;
                 YearListIndex := YearListIndex + 1;
                 WriteLn('');
             end;

     genreData.Next;
   end;

// Cleanup.
genreData.Free;
currentHits.Free;
YearList.Destroy;

PAL.UnlockExecution;
Groet,
Ski
drOhimself

Re: Script voor automatisch inlezen categorien

Ongelezen bericht door drOhimself »

Ski,

Bedankt voor het delen.
Vraag me een ding af : waarom zou je alle muziek in één map plaatsen ?
Ski

Re: Script voor automatisch inlezen categorien

Ongelezen bericht door Ski »

drOhimself schreef:Ski,

Bedankt voor het delen.
Vraag me een ding af : waarom zou je alle muziek in één map plaatsen ?
Historisch gegroeid [smilie=blush2.gif]
Gebruikersavatar
Wout
Beheerder
Berichten: 3501
Lid geworden op: 03 dec 2007, 14:17
SAM-versie: 2019.3
Database: MariaDB
Windows: 11
Locatie: West-Brabant
Contacteer:

Re: Script voor automatisch inlezen categorien

Ongelezen bericht door Wout »

Bedankt voor het delen!
Afbeelding
Ski

Re: Script voor automatisch inlezen categorien

Ongelezen bericht door Ski »

Hierbij de laatste aangepast versie en tevens de playlist PAL

Code: Selecteer alles

// The music directory.
var MusicDirectory : string = '\\corp.wppd.info\data\Muziek';

// The name of the Current Hits category.
var CurrentHitsCategoryName : string = 'Current Hits';

// The maximum age for adding to the current hits category.
var MaximumAgeOfCurrentHits : integer = 30;

// Make the PAL run fast!
PAL.LockExecution;

// Rescan music directory.
DIR[MusicDirectory].Rescan;

// Declare variables
var genreData, decadeYears, theSongList, categoryData, currentHits : TDataSet;

// Variable needed of we want the new songs added to the hits category;
var addToCurrentHits : boolean = false;

// Used to hold  the decade.
var decade, categoryName, actualCategoryName : string;

// Set the decades to use.
var YearList		: TStringList;
YearList := TStringList.Create;

// We will add a 0 later to it so we can create the 70, 80, 90, 00 and the 10.
YearList.Add('197');
YearList.Add('198');
YearList.Add('199');
YearList.Add('200');
YearList.Add('201');
// Ugly hack!
YearList.Add('210');

var YearListIndex : integer;

// Required for getting the current year and day.
var theYear : Integer = 0;
var theMonth : Integer = 0;
var theDay : Integer = 0;
DecodeDate(now, theYear, theMonth, theDay);

// First create/get the CurrentHitsCategoryName ID.
Writeln('Category name = ' + QuotedStr(CurrentHitsCategoryName));

begin
currentHits := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(CurrentHitsCategoryName), [], True);
currentHits.First;
if not currentHits.EOF then
   begin
      Writeln('Found ID: ' + QuotedStr(IntToStr(currentHits['id'])));
      // Remove the category items from SAM.
      Writeln('Clearing category ' +  QuotedStr(CurrentHitsCategoryName));
      ExecSQL('DELETE FROM categorylist WHERE categoryID = ' + IntToStr(currentHits['id']),[]);
   end
else
   begin
      currentHits.Free;
      Writeln('Category does not exists, creating category ' + QuotedStr(CurrentHitsCategoryName));
      ExecSQL('INSERT INTO category (name) VALUES (' + QuotedStr(CurrentHitsCategoryName) + ')', []);
      currentHits.Free;
      currentHits := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(CurrentHitsCategoryName), [], True);
   end
end;


// Second get all genres.
genreData := Query('SELECT DISTINCT genre FROM songlist WHERE genre IS NOT NULL AND genre <> ""', [], True);

genreData.First;
while not genreData.EOF do
   begin
      Writeln('Current genre = ' + QuotedStr(genreData['genre']));

          // Create the decades from each genre.
          YearListIndex := 0;
          while YearListIndex < YearList.Count - 1 do
             begin
                 // Remember that we do while < so here it is not -1 but -2!
                 addToCurrentHits := (YearListIndex = (YearList.Count - 2));

                 decade := CharAt(YearList.Get(YearListIndex), 3) + '0';
                 Writeln('Current decade = ' + QuotedStr(decade));
                 
                 // Get the combined category name.
                 categoryName := genreData['genre'] + ' ' + decade;
                 Writeln('Category name = ' + QuotedStr(categoryName));

                 // Let see of we have existing numbers, otherwise we don't create the category.
                 theSongList := Query('SELECT songlist.ID, date_added, albumyear FROM songlist WHERE songlist.genre LIKE ' + QuotedStr(genreData['genre']) + ' AND albumyear >= ' + QuotedStr(YearList.Get(YearListIndex)) + ' AND albumyear < ' + QuotedStr(YearList.Get(YearListIndex + 1)), [], True);
                 theSongList.First;
                 
                 if not theSongList.EOF then
                     begin
                    // Try to find the ID of the category.
                    categoryData := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(categoryName), [], True);
                    categoryData.First;
                    if not categoryData.EOF then
                    begin
                       Writeln('Found ID: ' + QuotedStr(IntToStr(categoryData['id'])));

                       // Remove the category items from SAM.
                       Writeln('Clearing category ' +  QuotedStr(categoryName));
                       ExecSQL('DELETE FROM categorylist WHERE categoryID = ' + IntToStr(categoryData['id']),[]);
                    end
                    else
                    begin
                       categoryData.Free;
                       Writeln('Category does not exists, creating category ' + QuotedStr(categoryName));
                       ExecSQL('INSERT INTO category (name) VALUES (' + QuotedStr(categoryName) + ')', []);
                       categoryData.Free;
                       categoryData := Query('SELECT id FROM category WHERE name LIKE ' + QuotedStr(categoryName), [], True);
                    end;

                    begin
                    if  addToCurrentHits and (StrToInt(theSongList['albumyear']) = theYear) and StrToDateTime(theSongList['date_added']) > (Now - MaximumAgeOfCurrentHits) then
                       begin
                       Writeln('Find new hit.');
                       actualCategoryName := IntToStr(currentHits['id']);// CurrentHitsCategoryName;
                       end
                    else
                       actualCategoryName := IntToStr(categoryData['id']);
                    end;
                 
                    WriteLn('Updating category ' + QuotedStr(actualCategoryName));
                    while not theSongList.EOF do
                          begin
                          ExecSQL('INSERT into categorylist (songID, categoryID) VALUES (' + IntToStr(theSongList['ID']) +  ', ' + actualCategoryName + ')',[]);
                          theSongList.Next;
                          end;

                 end;

                 // Clear the dataset.
                 theSongList.Free;
                 categoryData.Free;
                 YearListIndex := YearListIndex + 1;
                 WriteLn('');
             end;

     genreData.Next;
   end;

// Cleanup.
genreData.Free;
currentHits.Free;
YearList.Destroy;

PAL.UnlockExecution;
Playlist PAL:

Code: Selecteer alles

const minimumNumbersInQueue : integer = 20;


var isStarting       	 : boolean;

isStarting := false;
if ActivePlayer = nil then
   begin
      // First adjust the pitch.
      DeckA.SetPitch(+1);
      Writeln('Deck A pitch = ' + QuotedStr(IntToStr(DeckA.Pitch)));
      //DeckA.Eject;

      DeckB.SetPitch(+1);
      Writeln('Deck B pitch = ' + QuotedStr(IntToStr(DeckB.Pitch)));
      //DeckB.Eject;

      isStarting := true;
end;

var GenreDance, GenreFunk, GenreItalo, GenreDisco, GenreSoul, GenreHiNRG, GenreHouse, GenreCurrentHits : String;
var Year70, Year80, Year90, Year00, Year10: String;

var CategoryList       	                             : TStringList;
var YearList		          : TStringList;
var CatlistIndex       	                             : integer;
var SonginfoT          	                             : TSongInfo;
var CategoryToPullFrom 	          : string;

GenreDance		:= 'Dance';
GenreFunk 		:= 'Funk';
GenreItalo 		:= 'Italo';
GenreDisco 		:= 'Disco';
GenreSoul 		:= 'Soul';
GenreHiNRG 		:= 'Hi-NRG';
GenreHouse		:= 'House';
GenreCurrentHits	                   := 'Current Hits';

Year70 := '70';
Year80 := '80';
Year90 := '90';
Year00 := '00';
Year10 := '10';

CategoryList := TStringList.Create;

// Create the playlist.
// Only 80's in Italo
CategoryList.add (GenreItalo + ' ' + Year80);
CategoryList.add (GenreDance + ' ' + Year80);
CategoryList.add (GenreItalo + ' ' + Year80);
CategoryList.add (GenreDance + ' ' + Year90);
CategoryList.add (GenreHiNRG);
CategoryList.add (GenreDance + ' ' + Year00);
CategoryList.add (GenreFunk);
//CategoryList.add (GenreDisco);
CategoryList.add (GenreItalo + ' ' + Year80);
CategoryList.add (GenreDance + ' ' + Year70);
CategoryList.add (GenreCurrentHits);

//PAL.LockExecution;

CatListIndex := RandomInt(CategoryList.Count);

while true do
   begin
   If CatListIndex >= CategoryList.Count then
      begin
         CatListIndex := 0;
   end;

      CategoryToPullFrom := CategoryList.Get(CatListIndex);
      Writeln('Current category = ' + QuotedStr(CategoryToPullFrom));
      SonginfoT := Cat[CategoryToPullFrom].ChooseSong(smRandom,EnforceRules);

      if SongInfoT = nil then
         begin
            Writeln('No song selected, try to get random song.');
            var category : TDataSet;
            category := Query('SELECT name FROM category WHERE  INSTR(name, ' + QuotedStr(CategoryToPullFrom) + ' )> 0 ORDER BY RAND() LIMIT 1', [], True);
            category.First;

            if not category.EOF then
               begin
                  CategoryToPullFrom := category['name'];
                  Writeln('Find random category ' + QuotedStr(CategoryToPullFrom));
                  SonginfoT := Cat[CategoryToPullFrom].ChooseSong(smRandom,EnforceRules);
                  category.Free;
               end;

            if SongInfoT = nil then
               begin
                  Writeln('No song find, going to another category.');
                  CatListIndex := RandomInt(CategoryList.Count);
               end
      end;

      if SongInfoT <> nil then
         begin
            // Add the song to the queue.
            Writeln('Add song ' + QuotedStr(SongInfoT['artist'] + ' ' +  SongInfoT['title']) + ' to the queue.');
            Queue.Add(SonginfoT, ipBottom);
            CatListIndex := CatListIndex + 1;
            if Queue.Count > minimumNumbersInQueue then
               begin

               if isStarting then
                 begin
                    DeckA.QueueSong(Queue.NextInqueue);
                    DeckA.Play;
                    isStarting := false;
                end;

               PAL.WaitForPlayCount(1);
            end;
      end;
end;

//PAL.UnlockExecution;

Categorylist.Destroy;
Plaats reactie
  • Vergelijkbare Onderwerpen
    Reacties
    Weergaves
    Laatste bericht