Skip to content

Instantly share code, notes, and snippets.

@andybarilla
Created July 21, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybarilla/6f2b3a26806ff759f0b7ab1415bb0885 to your computer and use it in GitHub Desktop.
Save andybarilla/6f2b3a26806ff759f0b7ab1415bb0885 to your computer and use it in GitHub Desktop.
#!/bin/env python
import os
base_url = 'http://mydomain.com/mp3s'
root_dir = '/home/username/mp3s'
m3u_name = '000_all_songs.m3u'
audio_exts = ['.mp3', '.ogg', '.m4a']
def handle_dir(base_dir, root_dir):
files = []
for path in os.listdir(root_dir):
full_path = os.path.join(root_dir, path)
if os.path.isdir(full_path):
handle_dir(base_dir, full_path)
continue
filename, ext = os.path.splitext(full_path)
if ext in audio_exts:
files.append(full_path)
if len(files) == 0:
return
with open(os.path.join(root_dir, m3u_name), 'w') as playlist:
for fil in files:
url = '{}{}'.format(
base_url,
'/'.join(os.path.split(fil[len(base_dir):]))
)
playlist.write('{}\n'.format(url))
handle_dir(root_dir, root_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment