Example #1
0
def collect_song(dirname, filename):
    """
    Collect a single song.

    dirname  -- name of the containing directory
    filename -- name of the song file

    Throws ValueError if the file doesn't exist, isn't a song,
    or lacks metadata information 'name' and 'artist'.
    """
    filepath = os.path.join(dirname, filename)

    # TODO check if song must be updated instead of added
    try:
        raw = mutagen.File(filepath)
        song = Song(original=filename,
                    name=raw.get('title')[0],
                    artist=raw.get('artist')[0],
                    genre=raw.get('genre', [''])[0],
                    album=raw.get('album', [''])[0],
                    mime='audio/ogg')
        song.save()
    except Exception as e:
        raise ValueError('Could not collect file %s' % filepath)

    target_url = os.path.join(TARGET_DIR, song.filename())
    shutil.copy(filepath, target_url)