Mijn eerste post hier als newby
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;
Ski

