Changeset 9


Ignore:
Timestamp:
Mar 28, 2022, 6:00:50 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Split search and generation phase.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r8 r9  
    4444    Playlist.SearchBaseDir := ParamStr(1);
    4545    Playlist.RemoteBaseDir := ParamStr(2);
     46    Playlist.Search;
    4647    Playlist.GenerateXSPF;
    47     Close;
     48    Application.Terminate;
    4849  end;
    4950end;
  • trunk/UPlaylist.pas

    r8 r9  
    1010type
    1111  TPlaylistItem = class
    12 
    13   end;
     12    Time: TDateTime;
     13    Title: string;
     14    RemoteName: string;
     15    LocationOriginal: string;
     16    Location: string;
     17    Creator: string;
     18    TrackNum: string;
     19    Album: string;
     20  end;
     21
     22  { TPlaylistItems }
    1423
    1524  TPlaylistItems = class(TObjectList<TPlayListItem>)
     25    function AddNew: TPlaylistItem;
    1626  end;
    1727
     
    2030  TPlaylist = class
    2131  public
     32    Items: TPlaylistItems;
    2233    SearchBaseDir: string;
    2334    RemoteBaseDir: string;
    2435    FilteredFileExtensions: array of string;
     36    procedure Search;
    2537    procedure GenerateM3U;
    2638    procedure GenerateXSPF;
    2739    constructor Create;
     40    destructor Destroy; override;
    2841  end;
    2942
     
    5063begin
    5164  Path := IncludeTrailingPathDelimiter(PathName);
    52   //WriteLn('Dir: ' + PathName);
    5365  if FindFirst(Path + '*', faAnyFile, Rec) = 0 then
    5466  try
    5567    repeat
    56       //WriteLn(Rec.Name);
    5768      if Rec.Name = '..' then continue
    5869      else if Rec.Name = '.' then continue
    5970      else if (Rec.Attr and faDirectory) > 0 then FileSearch(Files, Path + Rec.Name, ExtList)
    6071      else if InStrArray(LowerCase(ExtractFileExt(Rec.Name)), ExtList) then begin
    61         //WriteLn('File: ' + Rec.Name);
    6272        Files.Add(Path + Rec.Name);
    6373      end;
    6474    until FindNext(Rec) <> 0;
    6575  finally
    66     FindClose(Rec) ;
    67   end;
    68 end;
    69 
    70 procedure TPlaylist.GenerateM3U;
    71 var
    72   Files: TStringList;
    73   I: Integer;
    74 begin
    75   Files := TStringList.Create;
    76   WriteLn('#EXTM3U');
    77   FileSearch(Files, SearchBaseDir, FilteredFileExtensions);
    78   for I := 0 to Files.Count - 1 do
    79     WriteLn(Files[I]);
    80   Files.Free;
     76    FindClose(Rec);
     77  end;
    8178end;
    8279
     
    9289end;
    9390
     91
     92{ TPlaylistItems }
     93
     94function TPlaylistItems.AddNew: TPlaylistItem;
     95begin
     96  Result := TPlaylistItem.Create;
     97  Add(Result);
     98end;
     99
     100procedure TPlaylist.Search;
     101var
     102  Files: TStringList;
     103  I: Integer;
     104  PlaylistItem: TPlaylistItem;
     105  NameRemoved: Boolean;
     106  Part: string;
     107  OutInt: Integer;
     108begin
     109  Files := TStringList.Create;
     110  try
     111    FileSearch(Files, SearchBaseDir, FilteredFileExtensions);
     112    for I := 0 to Files.Count - 1 do begin
     113      PlaylistItem := Items.AddNew;
     114      with PlayListItem do begin
     115        RemoteName := RemoteBaseDir + Copy(Files[I], Length(ParamStr(1)) + 2, High(Integer));
     116        LocationOriginal := StringReplace(RemoteName, '&', '&amp;', [rfReplaceAll]);
     117        Location := StringReplace(LocationOriginal, '_', ' ', [rfReplaceAll]);
     118        Album := ExtractFileName(ExtractFileDir(Location));
     119        NameRemoved := False;
     120
     121      // Detect Creator from directory name
     122      if Pos(' - ', Album) > 0 then begin
     123        Creator := Copy(Album, 1, Pos(' - ', Album) - 1);
     124        Delete(Album, 1, Length(Creator) + 3);
     125      end else Creator := Album;
     126      Title := Copy(ExtractFileName(Location), 1, Length(ExtractFileName(Location)) - Length(ExtractFileExt(Location)));
     127
     128      // Remove starting with album name from title
     129      Part := Creator + ' - ';
     130      if Copy(Title, 1, Length(Part)) = Part then begin
     131        Delete(Title, 1, Length(Part));
     132        NameRemoved := True;
     133      end;
     134
     135      // Try to load Creator from name
     136      if (Pos(' - ', Title) > 0) and (not NameRemoved) then begin
     137        Part := Copy(Title, 1, Pos(' - ', Title) - 1);
     138        // Avoid track number which can be first title part
     139        if not TryStrToInt(Part, OutInt) then begin
     140          Creator := Part;
     141          Delete(Title, 1, Length(Creator) + 3);
     142        end;
     143      end;
     144        // Detect track number from title
     145        if Pos(' - ', Title) > 0 then begin
     146          TrackNum := Copy(Title, 1, Pos(' - ', Title) - 1);
     147          if TryStrToInt(TrackNum, OutInt) then
     148            Delete(Title, 1, Length(TrackNum) + 3)
     149            else TrackNum := '';
     150        end else TrackNum := '';
     151
     152        Time := FileDate(Files[I]);
     153      end;
     154    end;
     155  finally
     156    Files.Free;
     157  end;
     158end;
     159
     160procedure TPlaylist.GenerateM3U;
     161var
     162  I: Integer;
     163begin
     164  WriteLn('#EXTM3U');
     165  for I := 0 to Items.Count - 1 do
     166  with Items[I] do begin
     167    WriteLn(LocationOriginal);
     168  end;
     169end;
     170
    94171procedure TPlaylist.GenerateXSPF;
    95172var
    96   Files: TStringList;
    97   I: Integer;
    98   Creator: string;
    99   Album: string;
    100   Location: string;
    101   LocationOriginal: string;
    102   Title: string;
    103   Part: string;
    104   TrackNum: string;
    105   Out: Integer;
    106   NameRemoved: Boolean;
    107   RemoteName: string;
    108 begin
    109   Files := TStringList.Create;
     173  I: Integer;
     174begin
    110175  WriteLn('<?xml version="1.0" encoding="UTF-8"?>');
    111176  WriteLn('<playlist version="1" xmlns="http://xspf.org/ns/0/">');
    112177  WriteLn('<trackList>');
    113   FileSearch(Files, SearchBaseDir, FilteredFileExtensions);
    114   for I := 0 to Files.Count - 1 do begin
    115     RemoteName := RemoteBaseDir + Copy(Files[I], Length(ParamStr(1)) + 2, High(Integer));
    116     LocationOriginal := StringReplace(RemoteName, '&', '&amp;', [rfReplaceAll]);
    117     Location := StringReplace(LocationOriginal, '_', ' ', [rfReplaceAll]);
    118     Album := ExtractFileName(ExtractFileDir(Location));
    119     NameRemoved := False;
    120 
    121     // Detect Creator from directory name
    122     if Pos(' - ', Album) > 0 then begin
    123       Creator := Copy(Album, 1, Pos(' - ', Album) - 1);
    124       Delete(Album, 1, Length(Creator) + 3);
    125     end else Creator := Album;
    126     Title := Copy(ExtractFileName(Location), 1, Length(ExtractFileName(Location)) - Length(ExtractFileExt(Location)));
    127 
    128     // Remove starting with album name from title
    129     Part := Creator + ' - ';
    130     if Copy(Title, 1, Length(Part)) = Part then begin
    131       Delete(Title, 1, Length(Part));
    132       NameRemoved := True;
    133     end;
    134 
    135     // Try to load Creator from name
    136     if (Pos(' - ', Title) > 0) and (not NameRemoved) then begin
    137       Part := Copy(Title, 1, Pos(' - ', Title) - 1);
    138       // Avoid track number which can be first title part
    139       if not TryStrToInt(Part, Out) then begin
    140         Creator := Part;
    141         Delete(Title, 1, Length(Creator) + 3);
    142       end;
    143     end;
    144     // Detect track number from title
    145     if Pos(' - ', Title) > 0 then begin
    146       TrackNum := Copy(Title, 1, Pos(' - ', Title) - 1);
    147       if TryStrToInt(TrackNum, Out) then
    148         Delete(Title, 1, Length(TrackNum) + 3)
    149         else TrackNum := '';
    150     end else TrackNum := '';
    151 
     178  for I := 0 to Items.Count - 1 do
     179  with Items[I] do begin
    152180    WriteLn('<track>');
    153181    WriteLn('<title>' + Title + '</title>');
     
    156184    WriteLn('<creator>' + Creator + '</creator>');
    157185    WriteLn('<trackNum>' + TrackNum + '</trackNum>');
    158     WriteLn('<annotation>' + FormatDateTime('yyyy-mm-dd', FileDate(Files[I])) + '</annotation>');
     186    WriteLn('<annotation>' + FormatDateTime('yyyy-mm-dd', Time) + '</annotation>');
    159187    WriteLn('</track>');
    160188  end;
    161189  WriteLn('</trackList>');
    162190  WriteLn('</playlist>');
    163   Files.Free;
    164191end;
    165192
     
    167194begin
    168195  FilteredFileExtensions := ['.mp3', '.ac3', '.flac', '.it', '.m4a', '.wma', '.s3m', '.ogg'];
     196  Items := TPlaylistItems.Create;
     197end;
     198
     199destructor TPlaylist.Destroy;
     200begin
     201  FreeAndNil(Items);
     202  inherited;
    169203end;
    170204
Note: See TracChangeset for help on using the changeset viewer.