@@ -329,6 +329,8 @@ def item(
329
329
quality_audio : Quality | None = None ,
330
330
quality_video : QualityVideo | None = None ,
331
331
is_parent_album : bool = False ,
332
+ list_position : int = 0 ,
333
+ list_total : int = 0 ,
332
334
) -> (bool , pathlib .Path ):
333
335
try :
334
336
if media_id and media_type :
@@ -363,7 +365,9 @@ def item(
363
365
metadata_tags = [] if isinstance (media , Video ) else media .media_metadata_tags ,
364
366
is_video = isinstance (media , Video ),
365
367
)
366
- file_name_relative : str = format_path_media (file_template , media , self .settings .data .album_track_num_pad_min )
368
+ file_name_relative : str = format_path_media (
369
+ file_template , media , self .settings .data .album_track_num_pad_min , list_position , list_total
370
+ )
367
371
path_media_dst : pathlib .Path = (
368
372
pathlib .Path (self .path_base ).expanduser () / (file_name_relative + file_extension_dummy )
369
373
).absolute ()
@@ -732,7 +736,7 @@ def items(
732
736
return
733
737
734
738
# Create file name and path
735
- file_name_relative : str = format_path_media (file_template , media , self . settings . data . album_track_num_pad_min )
739
+ file_name_relative : str = format_path_media (file_template , media )
736
740
737
741
# Get the name of the list and check, if videos should be included.
738
742
list_media_name : str = name_builder_title (media )
@@ -756,7 +760,12 @@ def items(
756
760
)
757
761
758
762
is_album : bool = isinstance (media , Album )
763
+ # TODO: Refactor strings to constants (also in cfg.py)
764
+ sort_by_track_num : bool = (
765
+ True if "album_track_num" in file_name_relative or "list_pos" in file_name_relative else False
766
+ )
759
767
result_dirs : [pathlib .Path ] = []
768
+ list_total : int = len (items )
760
769
761
770
# Iterate through list items
762
771
while not progress .finished :
@@ -771,8 +780,10 @@ def items(
771
780
quality_video = quality_video ,
772
781
download_delay = download_delay ,
773
782
is_parent_album = is_album ,
783
+ list_position = count + 1 ,
784
+ list_total = list_total ,
774
785
)
775
- for item_media in items
786
+ for count , item_media in enumerate ( items )
776
787
]
777
788
778
789
# Report results as they become available
@@ -800,11 +811,13 @@ def items(
800
811
801
812
# Create playlist file
802
813
if self .settings .data .playlist_create :
803
- self .playlist_populate (set (result_dirs ), list_media_name , is_album )
814
+ self .playlist_populate (set (result_dirs ), list_media_name , is_album , sort_by_track_num )
804
815
805
816
self .fn_logger .info (f"Finished list '{ list_media_name } '." )
806
817
807
- def playlist_populate (self , dirs_scoped : [pathlib .Path ], name_list : str , is_album : bool ) -> [pathlib .Path ]:
818
+ def playlist_populate (
819
+ self , dirs_scoped : [pathlib .Path ], name_list : str , is_album : bool , sort_alphabetically
820
+ ) -> [pathlib .Path ]:
808
821
result : [pathlib .Path ] = []
809
822
810
823
# For each dir, which contains tracks
@@ -821,8 +834,11 @@ def playlist_populate(self, dirs_scoped: [pathlib.Path], name_list: str, is_albu
821
834
for extension_audio in AudioExtensions :
822
835
path_tracks = path_tracks + list (dir_scoped .glob (f"*{ extension_audio !s} " ))
823
836
824
- # If it is not an album sort by modification time
825
- if not is_album :
837
+ # Sort alphabetically, e.g. if items are prefixed with numbers
838
+ if sort_alphabetically :
839
+ path_tracks .sort ()
840
+ elif not is_album :
841
+ # If it is not an album sort by modification time
826
842
path_tracks .sort (key = lambda x : os .path .getmtime (x ))
827
843
828
844
# Write data to m3u file
0 commit comments