Created
March 4, 2009 20:23
-
-
Save NaPs/73981 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
#!/usr/bin/env python2.6 | |
# | |
# Riff, rock, and much more !! | |
# | |
# Copyright 2008 Antoine Millet <[email protected]> | |
# | |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | |
# MA 02110-1301, USA. | |
import math | |
import os | |
import re | |
from random import randint | |
# Configuration : | |
POOLS = { | |
# name : (directory, weight) | |
'normales': ('/home/naps/Desktop/zik/', 3), | |
'rares': ('/home/naps/Desktop/zik2/', 1), | |
} | |
M3U_FILENAME = '/home/naps/Desktop/test.m3u' | |
# Regex that is used for file selection | |
MUSIC_FILES_PATTERN = r'(\.mp3|\.ogg)$' | |
# In debug mode, script will speak when it do things | |
DEBUG = True | |
# END OF CONFIGURATION | |
# Program classes, do not edit plzthx !!1 | |
class Pool: | |
''' This is a cool music pool ''' | |
def __init__(self, name, directory, weight): | |
if DEBUG: | |
print 'Creating %s pool in %s (weight = %s)' % (name, directory, weight) | |
self.name = name | |
self.directory = directory | |
self.weight = weight | |
self.files = [] | |
self._list_files() # Retrieve all filenames | |
def _list_files(self): | |
for directory, subdirectories, files in os.walk(self.directory): | |
for file in files: | |
if re.search(MUSIC_FILES_PATTERN, file): | |
self.files.append(os.path.join(directory, file)) | |
if DEBUG: | |
print 'Found %s files for %s' % (len(self.files), self.name) | |
def get_nb_salves_needed(self): | |
return int(math.ceil(len(self)/float(self.weight))) | |
def __len__(self): | |
return len(self.files) | |
def get_filenames(self): | |
return (self.files.pop(randint(0, len(self.files)-1)) for _ in xrange(self.weight)) | |
if __name__ == '__main__': # Main program | |
pools = [] | |
salve = 0 | |
total_tracks = 0 | |
tracks = [] | |
for k, v in POOLS.items(): | |
pools.append(Pool(k, *v)) | |
salve += v[1] | |
total_tracks += len(pools[-1]) | |
salves_nb = min(pools, key=lambda x:x.get_nb_salves_needed()).get_nb_salves_needed() | |
if DEBUG: | |
print 'Salve is %s tracks' % salve | |
print 'There are %s tracks in all pools' % total_tracks | |
print 'There will be %s salves in total' % salves_nb | |
for salve_i in xrange(salves_nb): | |
for pool in pools: | |
tracks += pool.get_filenames() | |
if DEBUG: | |
print 'Playlist is :' | |
for t in tracks: | |
print ' - %s' % t | |
print 'Writing to %s...' % M3U_FILENAME | |
m3u_file = open(M3U_FILENAME ,'w') | |
m3u_file.writelines([t+'\n' for t in tracks]) | |
m3u_file.close() | |
if DEBUG: | |
print 'Writed. Done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment