Created
July 21, 2016 14:44
-
-
Save andybarilla/6f2b3a26806ff759f0b7ab1415bb0885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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