Friday, June 20, 2008

Django mini-series

I'm running a Django mini-series on Will Larson's blog, called Wielding Django. So far I have written:

  1. Minimalism

  2. Up to Speed

  3. JSON, Object-Oriented Views, and Starting a Real App



I'm mainly posting this to provide a place for discussion in the comments.

Saturday, June 7, 2008

mp3rename

I had a sizable collection of sermons that were named very badly (e.g., just the name). I was unsatisfied with all the mp3 renaming tools I found; they wouldn't get the output I wanted, or wouldn't read id3v2 tags, or etc. So, using python-eyeD3, I wrote my own.


#!/usr/bin/env python

import eyeD3, os, sys

default_subs = {
':': ' -',
}

bad_chars = ['?','/',':']

def clean(x, subs=default_subs, bad_chars=bad_chars):
for a,b in subs.iteritems():
x = x.replace(a,b)
for c in bad_chars:
x = x.replace(c,'')
return x

def do_rename_file(fname):
tag = eyeD3.Tag()
tag.link(fname)

name = []

num = tag.getTrackNum()[0]
if num:
name.append('%02d' % num)
name.append('-')

artist = tag.getArtist()
if artist:
name.append(clean(artist))
name.append('-')

title = tag.getTitle()
if title:
name.append(clean(title))

name = ''.join(name)

if not title:
print 'Not renaming %s - not enough tag data' % fname
return

name = name+'.mp3'

print 'Renaming "%s" to "%s": (OK,q,sub_name)' % (fname, name)
x=raw_input()
if len(x) > 2:
name = x
elif len(x) > 0:
print 'OK, skipping.'
return

os.rename(fname, name)

if __name__=='__main__':
for f in sys.argv[1:]:
do_rename_file(f)


You can modify the output format by... changing the code. But python is pretty readable.