Playlist Maker
#6
I have made some changes to the way this script works after seeing some discussion about sorting from Issue 118 on Codeberg . Ailrux posted about the fatsort utility. This takes a FAT filesystem and puts it in alphabetical order. Since the biggest issue I was solving with this script was that nothing was sorted, this is a fantastic solution to that problem. 

The main drawback on my previous script was that while the separate folder for playlists were navigable, there was no way to look back at the file list. For instance, if you wanted to see what the next or previous track was, you could only skip to it rather than look at a list while the music still played. My new version resolves this by adding the playlists to the directory with the files, so there is only one directory tree to navigate. I left the sorting functions in the script so that it will still properly sequence the playlist, and then after running the script, you run the sorting utility to sequence everything on the card (the playlist names start with "000" now, so it will appear before any of the files assuming that the sorter is run after the script). I used the Mac interface Ailrux linked to, and later explained is just a nice wrapping for the command line program that runs on a variety of *NIXen. Someday Issue 124 might get resolved, which would solve the other issue that my script, but in the meantime this works way better than my previous system, so I thought others might find it useful.

Code:
#playlister.py version 1.2
#This version adds a playlist to each folder in the /Music directory instead of creating. This works as long as the drive is handled by fatsorter

import os

def folder_dive (source_path, items, i):
  sub_items=[]
 
  while i < len(items):
    sub_path = f'{source_path}/{items[i]}'
 
    if os.path.isdir(sub_path): 
      next_level_items= [f for f in os.listdir(sub_path) if not f.startswith('.') if not f.endswith('.jpg') if not f.endswith('.jpeg') if not f.endswith('.bmp') if not f.endswith('.png') if not f.endswith('.pdf') if not f.endswith('.log') if not f.endswith('.cue') if not f.endswith('.txt') if not f.endswith('.txt#') if not f.endswith('.rtf')  if not f.endswith('.md5')]

      next_level_items.sort()
      folder_dive (sub_path, next_level_items, 0)

      i=i+1
 
    if os.path.isfile(sub_path):           
      playlist_path ='/' + sub_path.lstrip(local_dir)
      sub_items.append(playlist_path)
      sub_items.sort()
     
      i=i+1

    if i is len(items) and sub_items:
      divvied_path=sub_path.rsplit('/',2)
      playlist_name = f'{divvied_path[0]}/{divvied_path[1]}/000_{divvied_path[1]}.playlist'
     
      if not os.path.isfile(playlist_name):
        print(f'Writing Playlist for {divvied_path[1]} at\n{playlist_name}')
       
        j=0
        while j <len(sub_items):
          with open(playlist_name, 'a', encoding='utf8') as wf:
            wf.write(f'{sub_items[j]}\n')
            j=j+1

      else:
        print('Playlist File exists. See error log')
        with open(error_log, 'a', encoding='utf8') as wf:
          wf.write(f'The playlist file: {divvied_path[1]}.playlist\nalready existed. It has not been updated, even if the items in {divvied_path[0]}/{divvied_path[1]} have changed.\n\n')

      i=i+1

local_dir=os.path.dirname(os.path.realpath(__file__))

source_folder = f'{local_dir}/Music'
print(f'The source directory for music is located at:\n{source_folder}')

error_log =f'{local_dir}/error_log.txt'
print(f'Playlists that already exist will be printed in the error log\n{error_log}')

source_folder_items = [f for f in os.listdir(source_folder) if not f.startswith('.')]
source_folder_items.sort()

if source_folder_items:
  folder_dive(source_folder,source_folder_items,0)

print (f'\n\n\n\n\n\n\n\nThere were {len(source_folder_items)} items in {source_folder} that were indexed.')

if os.path.isfile(error_log):
  print(f'\nSome playlists already existed. They are listed in \n{error_log}')
  Reply


Messages In This Thread
Playlist Maker - by tofuvavohu - 2025-02-07, 07:59 PM
RE: Playlist Maker - by almond - 2025-02-12, 06:58 AM
RE: Playlist Maker - by taivlam - 2025-03-10, 12:53 AM
RE: Playlist Maker - by taivlam - 2025-03-14, 01:22 AM
RE: Playlist Maker - by ailurux - 2025-03-14, 03:17 AM
RE: Playlist Maker - by tofuvavohu - 2025-04-26, 10:16 PM

Forum Jump: