Last active
July 9, 2016 19:07
-
-
Save Perlence/6417658 to your computer and use it in GitHub Desktop.
Generate playlist from random albums in your collection
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
from __future__ import print_function, unicode_literals | |
import codecs | |
import os | |
from path import path | |
import random | |
if os.name == 'nt': | |
import win_unicode_console | |
win_unicode_console.enable() | |
def albums(musiclib): | |
for artist in path(musiclib).dirs(): | |
for album in artist.dirs(): | |
yield { | |
'path': album, | |
'artist': artist.name, | |
'album': album.name, | |
'tracks': album.files, | |
} | |
def main(musiclib): | |
albumdicts = list(albums(musiclib)) | |
random.shuffle(albumdicts) | |
exists = True | |
try: | |
fp = open('playlist.m3u', 'r') | |
fp.close() | |
except IOError: | |
exists = False | |
with codecs.open('playlist.m3u', 'a', encoding='utf-8') as fp: | |
if not exists: | |
fp.write(codecs.BOM_UTF8.decode('utf-8')) | |
i = 0 | |
while i < len(albumdicts): | |
choice = albumdicts[i] | |
try: | |
print('{artist} - {album} '.format(**choice), end='') | |
result = raw_input().lower() | |
if result == 'y': | |
for track in choice['tracks'](): | |
fp.write(track + '\n') | |
fp.flush() | |
i += 1 | |
elif result == 'p' and i > 0: | |
i -= 1 | |
elif result == 'l': | |
for track in choice['tracks'](): | |
print(track.name) | |
elif result == 'q': | |
break | |
else: | |
i += 1 | |
except KeyboardInterrupt: | |
print() | |
break | |
if __name__ == '__main__': | |
musiclib = 'm:\\music' | |
main(musiclib) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment