Dave’s MP3 jukebox of his dreams–almost

Edna is a very easy-to-setup up MP3 jukebox written in Python and licensed under the GPL. I think a radio-like mode can be added to it pretty easily. Here’s the routine that selects songs to play:


def make_list(self, fullpath, url, recursive, shuffle, songs=None):
# This routine takes a string for 'fullpath' and 'url', a list for
# 'songs' and a boolean for 'recursive' and 'shuffle'. If recursive is
# false make_list will return a list of every file ending in '.mp3' in
# fullpath. If recursive is true make_list will return a list of every
# file ending in '.mp3' in fullpath and in every directory beneath
# fullpath.
#

if songs is None:
songs = []
for name in sort_dir(fullpath):
base, ext = os.path.splitext(name)
if extensions.has_key(string.lower(ext)):
# add the song's URL to the list we're building
songs.append(self.build_url(url, name) + '\n')

# recurse down into subdirectories looking for more MP3s.
if recursive and os.path.isdir(fullpath + '/' + name):
songs = self.make_list(fullpath + '/' + name,
url + '/' + urllib.quote(name),
recursive, 0, # don't shuffle subdir results
songs)

# The user asked us to mix up the results.
if shuffle:
count = len(songs)
for i in xrange(count):
j = random.randrange(count)
songs[i], songs[j] = songs[j], songs[i]
return songs

A question for Python programmers: I added a few lines to the shuffle routine, right after the line count=len(songs).


# radio mode -- DLF, 9/7
blacklist = open('blacklist', 'rb').readlines()
overplay = open('overplay', 'rb').readlines()
countb = len(blacklist)
counto = len(overplay)
for i in xrange(count):
for j in xrange(countb):
if songs[i] = blacklist[j]:
k = random.randrange(counto)
songs[i] = overplay[k] # end radio mode hack

What it’s supposed to do is load a blacklist and overplay list and, when shuffling, compare the current element against the blacklist, and if it finds a match, randomly substitutes a song from the overplay list.

What it actually does is nothing. Anyone have any ideas?

If you found this post informative or helpful, please share it!

8 thoughts on “Dave’s MP3 jukebox of his dreams–almost

  • September 7, 2003 at 10:05 pm
    Permalink

    Didn’t dig too deeply, but perhaps you meant:

    if songs[i] == blacklist[j]:

  • September 7, 2003 at 10:09 pm
    Permalink

    Note too that sucking in the whole file via readlines() will end every line with a CR, which may not match your song list. You might want to compare blacklist[j][:-1] instead (which will chop off the last character of each line, usually a CR).

    Talk to me tomorrow if it still doesn’t work, I’ve got my Python books a spare moment or two and maybe we can sort it out.

  • September 8, 2003 at 1:21 pm
    Permalink

    The == was the start of the problem. Getting the strings into the right format was another. It’s crudely working now but definitely in the only-works-for-me stage (hard-coded IP addresses in places, stuff like that, definitely not ready for prime time). Thanks much–it would have taken me a long time to figure that out.

  • September 8, 2003 at 7:39 pm
    Permalink

    Joseph, thanks for the link to the story. The biography of Scott and Zelda Fitzgerald I just read argued that Zelda was every Fitzgerald female antagonist, but the elements of his first love in Daisy Buchanan (from Gatsby) in particular are undeniable.

    OK, programmers, another question for you: My hack works. But it’s slow. It’s fine with a half-dozen songs, but throw a hundred at it and it takes minutes to sort it all out on the Celeron-366 I’m using as a server. Any thoughts on a better sort algorithm to use?

  • September 8, 2003 at 11:54 pm
    Permalink

    While not addressing the need for a better sort, as soon as you have determined that song[i] matches blacklist[j] and adjusted it, you know that there is no need to to compare song[i] to anyother blacklist entry…

    So, try adding a “break” after the assignment to song[i] to terminate the loop through the blacklist, and move on to the next song.

    # radio mode — DLF, 9/7
    blacklist = open(‘blacklist’, ‘rb’).readlines()
    overplay = open(‘overplay’, ‘rb’).readlines()
    countb = len(blacklist)
    counto = len(overplay)
    for i in xrange(count):
    for j in xrange(countb):
    if songs[i] == blacklist[j]:
    k = random.randrange(counto)
    songs[i] = overplay[k]
    break
    # end radio mode hack

  • September 8, 2003 at 11:55 pm
    Permalink

    That was meant to have been formatted to show the break at the same indent level as the previous statement…

  • September 9, 2003 at 9:08 am
    Permalink

    I just switched from IIS 5.1 to Apache 2.0 and needed something besides the ASP server I was using. Reading about Edna here made it pretty darned simple. One Python install, one Edna install, one config file edit, and Edna even displays my ‘cover.jpg’ graphic files by default. Simple, clean, and quick to setup. Thanks, guys!!

Comments are closed.